广义咖喱 - Javascript
Generalised Curry - Javascript
在阅读 article 关于在 Javascript 中实现通用 curry 的文章时,我偶然发现了这段代码。
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));
};
}
我无法理解其中说明的部分
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
的元数是如何在后续的调用中降为N-k的?一个有 k 个参数的绑定函数应该有一个 k 对吧?
绑定函数 returns 具有部分参数的函数,因此 f(a, b, c)
变为 f.bind(null, a).bind(null, b)(c)
。
在阅读 article 关于在 Javascript 中实现通用 curry 的文章时,我偶然发现了这段代码。
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));
};
}
我无法理解其中说明的部分
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
的元数是如何在后续的调用中降为N-k的?一个有 k 个参数的绑定函数应该有一个 k 对吧?
绑定函数 returns 具有部分参数的函数,因此 f(a, b, c)
变为 f.bind(null, a).bind(null, b)(c)
。