function currying(fn) {
return function curry(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args)
} else {
return function (...args2) {
return curry.apply(this, args.concat(args2))
}
}
}
}
// 测试
const add = (a, b, c) => {
return a + b + c
}
let addCurrying = currying(add)
console.log(addCurrying(1, 2, 3))
console.log(addCurrying(1)(2, 3))
console.log(addCurrying(1, 2)(3))
console.log(addCurrying(1)(2)(3))
简化版
function curry(fn, ...args) {
args.length >= fn.length
? fn(...args)
: (..._args) => curry(fn, ...args, ..._args)
}