编辑代码

// function helper() {
//     let fn = [].shift.call(arguments)
//     let arr = [].slice.call(arguments)
//     return function () {
//         fn.apply(this, arr.concat([].slice.call(arguments)))
//     }
// }
// function curry(fn, len) {
//     len = len || fn.length
//     return function () {
//         if (arguments.length >= len) {
//             return fn.apply(this, arguments)
//         } else {
//             let arr = [fn].concat([].slice.call(arguments))
//             // 注意这里helper函数调用要用helper.apply,不能只写helper(arr),切记
//             return curry(helper.apply(this, arr), len - arguments.length)
//         }
//     }
// }

// 简洁版
// const curry = (fn, ...args) => 
//     // 函数的参数个数可以直接通过函数数的.length属性来访问
//     args.length >= fn.length // 这个判断很关键!!!
//     // 传入的参数(实参)大于等于原始函数fn的参数(形参)个数,则直接执行该函数
//     ? fn(...args)
//     /**
//      * 传入的参数小于原始函数fn的参数个数时
//      * 则继续对当前函数进行柯里化,返回一个接受所有参数(当前参数和剩余参数) 的函数
//     */
//     : (..._args) => curry(fn, ...args, ..._args);

const curry = (fn, ...args) => 
    args.length >= fn.length
    ? fn(...args)
    : (..._args) => curry(fn, ...args, ..._args) 

function add(a, b, c) {
    console.log([a, b, c])
}

let test = curry(add)
test(1)(2)(3)
test(1, 2)(3)
test(1)(2)(3)