柯里化 & 求和

柯里化

function currying (fn, ...arg1) {
    if (arg1.length >= fn.length) {
        return fn(...arg1)
    }
    return function (...arg2) {
        return currying(fn, ...arg1, ...arg2)
    }
}

求和

function add (...rest1) {
    const fn = function (...rest2) {
        rest1.push(...rest2)
        return fn
    }
    fn.toString = function () {
        return rest1.reduce((a, b) => a + b)
    }
    return fn
}

参考

发表评论

您的电子邮箱地址不会被公开。