广义咖喱-Javascript [英] Generalised Curry - Javascript
问题描述
在阅读文章有关在Javascript中实现通用咖喱的过程时,我迷迷糊糊在这段代码上.
While reading an article on implementation of a generalised curry in Javascript, I stumbled upon this piece of code.
function curry(fn) {
return (...xs) => {
if (xs.length === 0) {
throw Error('EMPTY INVOCATION');
}
if (xs.length >= fn.length) {
return fn(...xs);
}
return curry(fn.bind(null, ...xs));
};
}
我无法理解说明的一部分
I am unable to grok the part of the explanation which states
我们创建一个fn的副本,该副本绑定了前k个参数 (部分应用),并将其作为下一个fn传递给咖喱,其N-k减小.
We create a copy of fn that has the first k arguments bound (partially applied) and pass it to curry as the next fn, with its reduced arity of N - k.
在随后的通话中,如何将fn
的Arity降低为N-k?具有k个参数的绑定函数应该具有k的对数吗?
How does the arity of fn
reduced to N-k in the subsequent calls? A bound function with k arguments should have an arity of k right?
推荐答案
绑定函数返回的函数带有部分应用的参数,因此f(a, b, c)
变为f.bind(null, a).bind(null, b)(c)
.
A bound function returns a function with partially applied arguments, so f(a, b, c)
becomes f.bind(null, a).bind(null, b)(c)
.
这篇关于广义咖喱-Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!