Wednesday, November 23, 2016

javascript Currying

function accumulator() {
    if (arguments.length > 0) {
        var arr = Array.prototype.slice.call(arguments, 0);
        var sum = Array.prototype.reduce.call(arr, function (a, b) { return a + b; });
        return sum;
        }
        else return 0;
}

function curryIt(fn) {
    if (arguments.length === 0 || typeof fn !== 'function') return 0;
    var args = Array.prototype.slice.call(arguments, 1);
    var newa = [];
    return function () {
         newa = Array.prototype.slice.call(arguments, 0);
         if (!newa.length) {
             return fn.apply(this, args);
         }
         else {
               args = args.concat(newa);
               return arguments.callee;
         }
   };
}
----------------------------------------
keywords: Closure, partially applied function / 闭包, 偏(应用)函数
@reference_1 @reference_2  @reference_3  @reference_4
@reference_5

No comments:

Post a Comment