These are chat archives for ramda/ramda

2nd
Oct 2018
Julien Gonzalez
@customcommander
Oct 02 2018 15:28

Hello everybody,

Some methods have in their documentation a sentence that says “dispatch to the X method of the second argument, if present”. I haven’t been able to find a clear explanation of what that means and what the transformation sequence would look like

e.g. https://ramdajs.com/docs/#chain
How do I know for example that head has a chain method?

Or here https://ramdajs.com/docs/#take
How do I know that a thing has a take method?

Any help would be greatly appreciated :)

Thanks,

Brad Compton (he/him)
@Bradcomp
Oct 02 2018 15:56
@customcommander Let's use take as an example. If you have a custom type with a take method, the function will use that instead of the default implementation for arrays. Here is an example of a stack, where take pops from the end instead of takes from the beginning: https://goo.gl/19ZSF3
As for chain, it has a default implementation for Array and Function, which is why it works on head, that doesn't have to do with dispatching. It will dispatch to custom types too though
Julien Gonzalez
@customcommander
Oct 02 2018 16:03

Hey @Bradcomp, thanks for this :)
Your code snippet makes sense. Altough I’m not sure I fully understand when you say

As for chain, it has a default implementation for Array and Function, which is why it works on head

What do you mean by that? Does Ramda add a chain method to both Array and Function prototypes?

Brad Compton (he/him)
@Bradcomp
Oct 02 2018 16:05
It doesn't add to the prototype, it just knows how to perform the chain operation on Functions and Arrays.
Julien Gonzalez
@customcommander
Oct 02 2018 16:05
Yeah I just figured it out by myself, stupid question :)
Brad Compton (he/him)
@Bradcomp
Oct 02 2018 16:07
You can think of the dispatching as extending the utility of the function beyond what it "knows" how to do. So the function "knows" what to do for Functions and Arrays, but not for anything else. If it doesn't know what to do it checks to see if there is a matching method, and uses that if it exists.
Julien Gonzalez
@customcommander
Oct 02 2018 16:16

Okay. Do you know what’s the default implementation for functions in chain?
For example I understand why I get this:

R.chain(R.append)([1, 2, 3]);
//=> [ [Function: f1], [Function: f1], [Function: f1] ]

But it still doesn’t click how I get this:

R.chain(R.append, R.head)([1, 2, 3]);
//=> [ 1, 2, 3, 1 ]
Brad Compton (he/him)
@Bradcomp
Oct 02 2018 16:17
If second argument is a function, chain(f, g)(x) is equivalent to f(g(x), x).
Other than that, I don't have much for you. I've never really used chain for functions
Julien Gonzalez
@customcommander
Oct 02 2018 16:19
@Bradcomp Your quote just cleared it up! :) That’s the bit I was missing. Where does it come from?
Brad Compton (he/him)
@Bradcomp
Oct 02 2018 16:19
It's in the new docs
but they aren't released yet :(
Julien Gonzalez
@customcommander
Oct 02 2018 16:19
Aaaah okay :)
That was useful, thanks a lot mate
Brad Compton (he/him)
@Bradcomp
Oct 02 2018 16:20
:bowtie: