Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between curry and curryRight in Lodash

What is the difference between curry and curryRight in Lodash?

Is just the application order of the provided arguments switched from f(a,b,c) which applies to f(a) -> f(b) -> f(c) to f(a,b,c) which then applies to f(c) -> f(b) -> f(a)?

I already looked into the Lodash documentation but that didn‘t helped me.

like image 854
Jul Avatar asked Jan 05 '18 10:01

Jul


1 Answers

From the documentation:

var abc = function(a, b, c) {
  return [a, b, c];
};

var curried = _.curryRight(abc);

curried(3)(2)(1);
// => [1, 2, 3]

curried(2, 3)(1);
// => [1, 2, 3]

curried(1, 2, 3);
// => [1, 2, 3]

The first example is simple. The order of the arguments is reversed (in comparison to _.curry).

The second and third one can perhaps be confusing.

In the third example we see that the order of the arguments is NOT reversed. That is because only the currying is applied in reverse. In other words the parentheses are applied in the reverse order, but what is inside of the parentheses sustains the original order.

Compare this to the result of _.curry(_.flip(f)):

var abc = function(a, b, c) {
  return [a, b, c];
};

var curried = _.curry(_.flip(abc), 3);
    
curried(3)(2)(1);
// => [1, 2, 3]

curried(3, 2)(1);
// => [1, 2, 3]

curried(3, 2, 1);
// => [1, 2, 3]

As you can see the result is different. Now the order of the arguments is reversed totally in all the examples.

Btw notice that for some reason I needed to specify the arity to 3 in _.curry(_.flip(abc), 3);. I don't know why but it causes an exception without that.

like image 97
fredrik.hjarner Avatar answered Sep 29 '22 03:09

fredrik.hjarner