admin管理员组文章数量:1430083
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
1 Answer
Reset to default 8From 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.
本文标签: javascriptDifference between curry and curryRight in LodashStack Overflow
版权声明:本文标题:javascript - Difference between curry and curryRight in Lodash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745445419a2658642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论