最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between curry and curryRight in Lodash - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jan 5, 2018 at 12:48 Roman 5,2503 gold badges24 silver badges33 bronze badges asked Jan 5, 2018 at 10:47 JulJul 15114 bronze badges 4
  • 2 Your understanding is correct. It is also the conceptually same as _.curry(_.flip(f));. – Jared Smith Commented Jan 5, 2018 at 11:12
  • The documentation has some examples. Is there is any particular example that you are confused by? – fredrik.hjarner Commented Jan 5, 2018 at 13:32
  • I guess if one is the right way of currying, the other must the wrong one :D – Bergi Commented Jan 5, 2018 at 13:59
  • Bergi, you made my day – Mulan Commented Jan 5, 2018 at 17:45
Add a ment  | 

1 Answer 1

Reset to default 8

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 parison 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.

发布评论

评论列表(0)

  1. 暂无评论