These are chat archives for ramda/ramda

23rd
Feb 2017
Brian McKenna
@puffnfresh
Feb 23 2017 01:02
yes, Pursuit is useful
Galileo Sanchez
@galileopy
Feb 23 2017 12:07
what are monads functors applicatives alts and alternatives?? categories? typeclasses?
Galileo Sanchez
@galileopy
Feb 23 2017 12:13
fold is to get a summary value of any foldable value? I get that Arrays are foldable, are Maybes and Either also foldable?
does lift comes from the elevating a function to another context?
James Forbes
@JAForbes
Feb 23 2017 12:17

@galileopy do you want to know what all those things are, or just one of them?

E.g.

  • what is a monad?
  • what is a functor?
  • what is an applicative?
  • etc

Or am I misreading the question?

Galileo Sanchez
@galileopy
Feb 23 2017 12:17
@JAForbes all of them, where do they come from, in a manner of speaking?
cars, bikes, carts are all transportation mediums, what are monads, functors in that sense?
James Forbes
@JAForbes
Feb 23 2017 12:19
right I see
algebras?
Galileo Sanchez
@galileopy
Feb 23 2017 12:20
So, we write algebras to abstract some common interface that has to abide by some rules?
James Forbes
@JAForbes
Feb 23 2017 12:24
I think all the things you listed are ways to move/morph between categories. But I'm really likely saying something inaccurate here.
Galileo Sanchez
@galileopy
Feb 23 2017 12:25
SICP states that to write a meta-language we need at least Abstraction - Combination - Closure, and it always escaped me how to get an intuition that you're going the right way
I'm kind of getting the intuition that this algebras are a good tool to gain that intuition in a formal way first
I'm just trying to joing those worlds, :), I see there is some correlation
James Forbes
@JAForbes
Feb 23 2017 12:26
yeah that seems fair
well I think "joining worlds" is what makes category theory applicable/fascinating/useful
it can be applied in so many contexts
Galileo Sanchez
@galileopy
Feb 23 2017 12:29
so FP is no just about higher order functions and pattern matching, is about applying concepts from different fields? Category Theory, Combinatoric Logic, and Lambda Calculus come to mind
am I going somewhere here?
Bravi
@Bravilogy
Feb 23 2017 12:32
I think FP is all about function compositions and creating generic, small, reusable functions while maintaining their purity; controlling side effects and data flow; and avoiding state in application
those abstractions just help you achieve this
Galileo Sanchez
@galileopy
Feb 23 2017 12:33
@Bravilogy thanks! you put my thoughts in words
made me remember
Bravi
@Bravilogy
Feb 23 2017 12:34
for example, in a simple function composition compose(doThis, doThat, andDoAnotherThing);there are so many things that can go wrong and those abstractions just help you control this and write much safer code
@galileopy yeah that's what I think anyway and how I'd describe FP to someone :D
Galileo Sanchez
@galileopy
Feb 23 2017 12:38
made me remember of a joke that goes almost like this, "The way to get correct information from an engineer you wont' get it by asking, you just write whatever comes to mind and let them correct you"
:)
Bravi
@Bravilogy
Feb 23 2017 12:38
haha yeah :D
Galileo Sanchez
@galileopy
Feb 23 2017 12:38
I think it was an xkcd comic, but I can't find it
Bravi
@Bravilogy
Feb 23 2017 12:40
I started looking into these abstractions about 2 - 3 months ago and I find them like an addiction :D it changed my thinking / reasoning about code in general. I tend to think about things in terms of dependencies and little pieces of problems
Galileo Sanchez
@galileopy
Feb 23 2017 12:41
I'd like to be able to write an algebra someday :(
Bravi
@Bravilogy
Feb 23 2017 12:42
have you worked with Haskell?
or purescript
Galileo Sanchez
@galileopy
Feb 23 2017 12:43
I remember the first time I programmed something, I basically copy-pasted some snippets I found on the internet to get a contact form inside a flash container and I was wondering if I could learn enough to be able to come up with a solution like that without having to find a ready made solution
I'm feeling that, all over again
I started Learn some Haskell for Great Good
Bravi
@Bravilogy
Feb 23 2017 12:45
nice! I was just about to type 'learn you a Haskell for great good!'
Galileo Sanchez
@galileopy
Feb 23 2017 12:45
But haven't advanced too far, I know there are a lot of concepts from there, and usually read that as an easy to understand reference to know why stuff work on js
so anything that can be summarized to a single value can be made a foldable?
are Maybes foldables?
Bravi
@Bravilogy
Feb 23 2017 12:46
I think all the answers are here:
https://github.com/fantasyland/fantasy-land
for example:
Monad A value that implements the Monad specification must also implement the Applicative and Chain specifications.
Galileo Sanchez
@galileopy
Feb 23 2017 12:48
So they're not
but a Maybe can implement more than one spec, right?
Bravi
@Bravilogy
Feb 23 2017 12:48
yeah of course
just like any other type
Task, List or whatever
or your own custom type
Galileo Sanchez
@galileopy
Feb 23 2017 12:49
So the first step to be able to write an algebra like those, is to be able to write instances of those algebras first?
Bravi
@Bravilogy
Feb 23 2017 12:50
by algebras do you mean Maybe, Task and stuff?
Galileo Sanchez
@galileopy
Feb 23 2017 12:50
aren't those instances of an algebra?
Bravi
@Bravilogy
Feb 23 2017 12:58
in terms of JS, they're just objects aren't they?
you can create any type of object like that
and implement some unique functionality and then turn them into
monads or whatever
Galileo Sanchez
@galileopy
Feb 23 2017 13:08
I can't watch that
it's distracting being talked too by an animal
what is it? a hedgehog?
Galileo Sanchez
@galileopy
Feb 23 2017 13:14
:scream: there is an identity functor
that hedgehog knows stuff
Bravi
@Bravilogy
Feb 23 2017 13:27
:D
Galileo Sanchez
@galileopy
Feb 23 2017 13:28
he strats right from eithers! I like that, I was having trouble trying to use them
Bravi
@Bravilogy
Feb 23 2017 13:44
the different between Maybe, Either and Validation is that Maybe has a value stored only in theRight part of it. Either has value stored in both - Right and Left and therefore this will be useful if you want to output the error to the user. However, Either will only catch 1 error as it skips all the map and chain stuff. If you want to output multiple errors, then you use Validation. It concats errors together and therefore you can pass any semigroup to the Failure / Fail / Left side of it. Be it an Array or any other semigroup type
Drew
@dtipson
Feb 23 2017 14:38
Either doesn't necessarily have to model errors though no? Sometimes it could be the reverse: Rights are the values that need further transformation while Lefts are values that are good as is.
Bravi
@Bravilogy
Feb 23 2017 14:42
@dtipson well data.either Left can hold a value:
function read(path) {
  return exists(path)?     Either.Right(fread(path))
  :      /* otherwise */   Either.Left("Non-existing file: " + path)
}
and that's what I meant
whereas Maybe can only be Maybe.Just or Maybe.Nothing
not sure how it is in Haskell though :D
Serg Nesterov
@cust0dian
Feb 23 2017 14:45
@dtipson Either is the most basic example of a sum type, i.e. Either a b is type made out of as plus bs. The way it's used for error handling is just a byproduct :) If you want to dig deeper: http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of-algebraic-data-types/
And Maybe is like Either a (), i.e. it's as plus one value that we can always get a hold of
Drew
@dtipson
Feb 23 2017 14:49
right, I'm just saying that I don't think you have to think of the Left value as an error, per se. It's just a value of a type that you don't want to or can't do work on. I think of Maybe as expressing an ambiguity of having a value or not, while Either better expresses an ambiguity about the inner value's type
Drew
@dtipson
Feb 23 2017 14:54
You "enter" a Maybe by having a value or not, but would like to continue composing behavior regardless. When and if you fold back out of it, you generally do so to resolve the ambiguity by simply returning the inner value or providing an alternative of the same type. Whereas with Either, you generally have a value but "enter" by having an ambiguity about whether its of the right type or some other type. And often you "exit" an Either (if you ever do) by doing something that unifies the value's type.
Tushar Mathur
@tusharmath
Feb 23 2017 16:13
is there a simple way to append a value to an array in an object's property :) ?
Brad Compton (he/him)
@Bradcomp
Feb 23 2017 16:20
@ram-bot
evolve({a: append(4)})({a: [1, 2, 3]})
ram-bot
@ram-bot
Feb 23 2017 16:20
{ a: [ 1, 2, 3, 4 ] }
Brad Compton (he/him)
@Bradcomp
Feb 23 2017 16:20
Like that?
Drew
@dtipson
Feb 23 2017 16:27
@ram-bot
over(R.lensProp('a'), append(4))({a: [1, 2, 3]})
ram-bot
@ram-bot
Feb 23 2017 16:27
{ a: [ 1, 2, 3, 4 ] }
Bravi
@Bravilogy
Feb 23 2017 17:15

hey guys, I've got this array:

"address_components" : [
         {
            "long_name" : "281",
            "short_name" : "281",
            "types" : [ "street_number" ]
         },
         {
            //
         }

what I want to do is get the first object, which has let's say street_number in types array. My solution is this at the moment:

const fetchByType = prop => filter(compose(lt(-1), indexOf(prop), prop('types')), address.address_components)
but I can feel that there's a better solution :D
and plus I need to call head afterwards in my case
I could change it to find instead and that solves the head issue
but the rest .. not sure
Jonah
@jonahx
Feb 23 2017 17:38
@Bravilogy have a look at the example using find in the docs. it’s quite similar to your problem and is def more appropriate than filter
@Bravilogy you might also have a look at where since a simple propEq won’t work (as you need to test that the prop types contains “street_number"
your final solution would be a find(where…
Bravi
@Bravilogy
Feb 23 2017 17:47
yeah find works
I'll have a look at where now
Jonah
@jonahx
Feb 23 2017 17:49
it will be something like const fetchByType = type => find(where({types: contains(type)}))
Bravi
@Bravilogy
Feb 23 2017 17:50
yep, that's exactly it
thanks!
that's awesome :D
Jonah
@jonahx
Feb 23 2017 17:50
:thumbsup:
Tushar Mathur
@tusharmath
Feb 23 2017 19:16
@Bradcomp Yes this is perfect! it should be in the cook book :)
@dtipson I wanted to avoid lenses
Bernhard Wang
@zwc
Feb 23 2017 20:45
I'm stuck trying to use map and objOf to create an array by using the mapped key value. Does anyone know how to make this work?
const c = {
    KEY: 'value',
        KEY2: 'skipit',
    KEY3: 'secret'
};

const keys = [ 'KEY', 'KEY3' ];

const mappedJS = keys.map(key => ({ [key]: c[key] }) );
const mappedRamda = R.map(R.objOf(???), keys);

// desired output would be [ { KEY: 'value' }, { KEY3: 'secret' } ]
Jonah
@jonahx
Feb 23 2017 20:55
@zwc pick(keys, c)
Bernhard Wang
@zwc
Feb 23 2017 20:55
That's a lot simpler, thanks! :D
Jonah
@jonahx
Feb 23 2017 20:57
I want to do notEquals, opposite of equals. Is there a better shortcut than complement(equals)? It seems like notEquals should exist as a convenience method.
Drew
@dtipson
Feb 23 2017 21:19
@tusharmath lenses are great though!
Brian McKenna
@puffnfresh
Feb 23 2017 22:46
never avoid lenses
Galileo Sanchez
@galileopy
Feb 23 2017 23:05
I don't understand why List.of(Sum(1),Sum(2)).fold(Sum.empty()) === Sum(3)
Does fold have 2 interfaces?
James Forbes
@JAForbes
Feb 23 2017 23:10

presumably Sum.empty simply returns Sum(0), empty is a hint that gives fold/reduce an initial value in the reducer.

Just like in JS [1,2].reduce(sum, []) could easily become [1,2].reduce(sum, empty()) where empty returns []. By abstracting the specific empty value for a given type, you can have a shared interface for any monoid.

https://github.com/fantasyland/fantasy-land#monoid

Galileo Sanchez
@galileopy
Feb 23 2017 23:10
@JAForbes it is not passing a reducer function
so if fold just receives the empty value does it just .concat it's values?
I though fold should receive a function
James Forbes
@JAForbes
Feb 23 2017 23:15
Internally, fold is calling Type.concat which is the reducer
Galileo Sanchez
@galileopy
Feb 23 2017 23:16
so it has two interfaces then?
one with a reducer function and another with just the first value?
James Forbes
@JAForbes
Feb 23 2017 23:17

So a semigroup is a type that has concat, and obeys some laws.

A monoid is a semigroup that also has an empty.

When you have a monoid, there's no function to pass in anymore, because we have concat, and even the empty could forseeably be prefilled.

Galileo Sanchez
@galileopy
Feb 23 2017 23:18
so when you have a foldable of monoids you can do that?
James Forbes
@JAForbes
Feb 23 2017 23:20
Yeah, I'm actually surprised empty is required there, seeing as .fold is sitting on the type. But maybe that's for interop with libraries.
Galileo Sanchez
@galileopy
Feb 23 2017 23:21
but you can still pass a long a reducer to foldable event if it is a foldable of monoids?
can you?
James Forbes
@JAForbes
Feb 23 2017 23:25

You could define a fold function that accepts a custom reducer and an empty. There's nothing stopping you. It's just the the type is encapsulating how to concat, which saves a lot of boilerplate.

If you watch this video https://egghead.io/lessons/javascript-combining-things-with-semigroups and then the following one, you'll see a great example of this.

Galileo Sanchez
@galileopy
Feb 23 2017 23:26
I watched that one, and was in https://egghead.io/lessons/javascript-unboxing-things-with-foldable that I got lost
James Forbes
@JAForbes
Feb 23 2017 23:27
when I saw the 2nd video I felt like I'd taken a mind altering drug - I've written code that does exactly what he is doing, the exact situation, and it was at least a 150 line function. It was a more involved data model, but relying on specifc types to define how things should be merged, and then recursively merging children according to their own concat/empty rules. Its incredible.
Galileo Sanchez
@galileopy
Feb 23 2017 23:29
ok, I just want to know if fold has that behavior always, calling fold on a foldable of monoids will always fold with the concat of the monoid?, no need to pass a reducer, I'm just asking that
:(
or fold !== reduce?
reduce takes an fn, fold takes one too? or doesn't? or has 2 interfaces?
what am I not getting?
professor frisby doesn't really explain the rules of fold
James Forbes
@JAForbes
Feb 23 2017 23:32

fold doesn't necessarily equate to reduce, but in order to fold a type that holds multiple values, it tends to involve a reducer, and often the body of that function is simply calling concat. So in the case of Sum's fold, its just doing that for you to save time.

In the video, you'll see Box(3).fold( n => n ) //=> 3 doesn't use reduce, because there's only 1 value.

Rick Medina
@rickmed
Feb 23 2017 23:33
sooo I think eg, in haskell, there is fold which takes a monoid and foldr and foldl which behaves like js' reduce
Galileo Sanchez
@galileopy
Feb 23 2017 23:34
ok I'm getting it now, fold and reduce are different functions, fold is a method defined for monoids, and reduce is something else
James Forbes
@JAForbes
Feb 23 2017 23:34

but if we want to fold over a list, we need to accept the previous value, and the next. So we end up with a signature like ( a,b ) => ...

In the video: "whenever you see fold, think removal of a type"

Rick Medina
@rickmed
Feb 23 2017 23:35
I mean, the intuition is the same, is just that "fold" expects a monoid so it uses it to "reduce" it, foldr is more similar to js' reduce
Syaiful Bahri
@syaiful6
Feb 23 2017 23:35
fold doesn't take monoid, it take foldable that has monoid inside it, eg take an array of string..
Galileo Sanchez
@galileopy
Feb 23 2017 23:36
^^^ that's confusing I thought professor Frisby was on the same page than the fantasy land guys
Rick Medina
@rickmed
Feb 23 2017 23:36

fold doesn't take monoid, it take foldable that has monoid inside it, eg take an array of string..

^^

Galileo Sanchez
@galileopy
Feb 23 2017 23:37
fantasy land doesn't talk about that kind of fold
It doesn't even talk about fold, just reduce
where is fold defined?
Syaiful Bahri
@syaiful6
Feb 23 2017 23:41
fantasy land decided not to include it, fold came from foldMap
Galileo Sanchez
@galileopy
Feb 23 2017 23:41
so fold(foldable monoid) === reduce(concat, foldable monoid), that's what I need to know?
Syaiful Bahri
@syaiful6
Feb 23 2017 23:42
so in haskell foldable has methods, foldr, foldl, foldMap
fantasy land decide only use foldl, they call it reduce. maybe because it convinient in js
fold :: (Foldable f, Monoid m) => f m -> m
Galileo Sanchez
@galileopy
Feb 23 2017 23:45
so neither ramda or fantasy land talk about fold that way, is this just documented in haskell?
Syaiful Bahri
@syaiful6
Feb 23 2017 23:46
yes, they dont have it, but you can implements it in term reduce too
just reduce it using concat and empty as starting value
Galileo Sanchez
@galileopy
Feb 23 2017 23:47
I know, I just wanted to know where was it specified, what was it's interface
know I know what it is, I used to think it was just another name for reduce, but is reduce that's just another name for foldr
Syaiful Bahri
@syaiful6
Feb 23 2017 23:48
try search haskell foldable, they have a wiki for it i think (haskell)
Galileo Sanchez
@galileopy
Feb 23 2017 23:54
it doesn't talk about the special case of folding a foldable of monoids
fold = foldMap id