These are chat archives for ramda/ramda

8th
Jun 2015
Simon Friis Vindum
@paldepind
Jun 08 2015 14:27
I new idea of mine dot-compose. Function composition in JavaScript with . as a composition operator. It started out as a ridiculous idea and an experiment but I think it's turned out to be usable. It allows you to do stuff like this:
r.join(' ').props(['first', 'last']).$({last: 'Bullet-Tooth', first: 'Tony'}); //=> 'Tony Bullet-Tooth'
Another example:
var fiveLightest = r.take(5).sort.map(getWeight).$(someList);
The dots should be interpreted as function composition. Spaces might make that more obvious:
var fiveLightest = r.take(5) . sort . map(getWeight) .$(someList);
Scott Sauyet
@CrossEye
Jun 08 2015 18:02
@paldepind: interesting, as always. I don't know that is actually want to use it. But it's nice to know that it can be done.
Simon Friis Vindum
@paldepind
Jun 08 2015 18:07
@CrossEye I will probably use it in my own pet projects. I should emphasize that in my examples small r is not the same as R. R is normal unmodified Ramda and r is a composeable/chainable collection of all Ramda functions.
Scott Sauyet
@CrossEye
Jun 08 2015 21:04
@paldepind: understood. I once did a version of compose through a fake operator overloading. You can see it here:
Simon Friis Vindum
@paldepind
Jun 08 2015 21:15
@CrossEye Haha! I would never have thought you'd do something like that. The conservative voice of reason ;) I actually kinda like it! I was briefly considering fake operator overloading as well (I was thinking something with +). But a huge advantage of my approach is that you can use all but the first function without R..
The downside, of course, is that functions that are not a part of the namespace will have to be injected.
I don't understand "The Obligatory Compose Joke"?
John-David Dalton
@jdalton
Jun 08 2015 21:22
Your dot idea is looks a lot like chaining
I use chaining under the hood for some composition (convert it into a chain)
Simon Friis Vindum
@paldepind
Jun 08 2015 21:31
@jdalton Yes indeed. The idea was that it should look like chaining but do function composition. Partly because the dot operator means compositing in some languages.
How do you convert it to a chain? And, is that how you achieve laziness for composition? dot-compose works with lodash as well. I'll add it to the Readme later.
John-David Dalton
@jdalton
Jun 08 2015 21:35
Yep this is how I achieve laziness for composition.
lets say your composing from left-right (so pipe or flow) thats how a chain reads, and much like composition a chain starts with a value then the other methods consume the result of the next
so I take the functions that are being composed and see if they have equiv lazy functions
if they do I start the chain (chaining is deferred until an explicit value is requested so sequences can be stored and reused)
then when the composed function is executed instead of a loop over the functions I plan the new seed value and execution the stored chain sequence
for for right-to-left I just compose the chain backwards
Raine Virta
@raine
Jun 08 2015 21:43
our codebase at work is full of _(arr).map(fn).value();, it makes me uneasy
John-David Dalton
@jdalton
Jun 08 2015 21:47

Ya some devs abuse the chaining style a bit.
I've seen a lot of folks coming form Underscore that did _(array).map(fn) a ton instead of _.map(array,fn)

With composition +fp that could be

var myfn = _.flow(
 _.map(fn1),
 _.filter(fn2),
 _.take(3)
);
Scott Sauyet
@CrossEye
Jun 08 2015 22:15
That style is quite readable. I've always hated the style that @raine described.
@paledepind: I always meant to go back and caption that joke. compose(Bach, Rachmaninoff); //=> Bacharach.
Hardy Jones
@joneshf
Jun 08 2015 22:25
Nah, you don't have to do that :P
Asaf
@asaf-romano
Jun 08 2015 22:31
once+curry leads to an interesting issue: http://bit.ly/1QGto63
actually, this shows it even better: http://bit.ly/1QGtBpI
Hardy Jones
@joneshf
Jun 08 2015 22:33
what seems to be the issue?
Asaf
@asaf-romano
Jun 08 2015 22:34
the partially applied function is what is once-d
one would expect the final result to be
(the actual call, that is)
Scott Sauyet
@CrossEye
Jun 08 2015 22:36
once is a real oddity anyway. It's mostly good for side-effect laden functions.
Asaf
@asaf-romano
Jun 08 2015 22:36
so plus10 is actually plus3
Scott Sauyet
@CrossEye
Jun 08 2015 22:36
I don't know if there's really much legitimate use for it.
Asaf
@asaf-romano
Jun 08 2015 22:37
in js world it is useful when you want to return the very same object each time.
Scott Sauyet
@CrossEye
Jun 08 2015 22:37
@asaf-romano: Yes, but what else would you really expect?
Hardy Jones
@joneshf
Jun 08 2015 22:37
@asaf-romano is this what you were expecting: http://bit.ly/1G8dEWz ?
I think this is pretty confusing: http://bit.ly/1G8dRZH
Asaf
@asaf-romano
Jun 08 2015 22:39
I'd expect it to work like this: http://bit.ly/1QGuFtE
Scott Sauyet
@CrossEye
Jun 08 2015 22:40
The trouble is that the output of the first call to the (curried) function is what's cached. If that's a value, fine. If it's another function, also fine.
Asaf
@asaf-romano
Jun 08 2015 22:46
to avoid trouble one has to curry the once-d function rather than once-ing the curried function. This is going to be a problem once(!) compose etc. return an already curried function
at that point, calling once on any composed function will always lead to unexpected bugs
Asaf
@asaf-romano
Jun 08 2015 22:59
So it seems the behavior I imagine for R.once is basically R.compose(R.once): http://bit.ly/1Kly2Xo
Scott Sauyet
@CrossEye
Jun 08 2015 23:00
That's an odd API, to my mind. But if you can come up with a decent description for it, put it in the Cookbook. If people are finding it useful, we can consider adding it to Ramda.
The current once has a very simple contract. You wrap a function in once, and get back a new function. Call that function (with whatever parameters) and get back your result. After that, every call to the wrapped function will return that same result. If you happen to not supply enough parameters to a curried function, then your result is a function, which you can comfortably call with different parameters.
There is also memoize, which might be more what you're looking for. It will not call the function again with the same parameters. This is meant for the case when the function call is expensive.
Michael Hurley
@buzzdecafe
Jun 08 2015 23:06
we gotta upgrade memoize using @davidchambers toString work
Asaf
@asaf-romano
Jun 08 2015 23:06
This reminds me the other discussion we had about curry1 :) One would expect R.compose(just-a-single-function) to basically be a no-op.
but I see your point.