JS柯里化
https://zh.javascript.info/currying-partials
function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
return func.apply(this, args);
} else {
return function(...args2) {
return curried.apply(this, args.concat(args2));
}
}
};
}
function sum(a, b, c) {
return a + b + c;
}
let curriedSum = curry(sum);
console.log(( curriedSum(1, 2, 3) )); // 6,仍然可以被正常调用
console.log(( curriedSum(1)(2,3) )); // 6,对第一个参数的柯里化
// 执行步骤:
// (curriedSum(1))(2,3);
// function(...args2) { return curried.apply(this,[1].concat(args2))}(2,3);
// == curried.apply(this, [1].concat([2,3]))
// == sum.apply(this, [1,2,3])