函数式编程--curry化

需要实现的效果:

add4(1)(2,3)(4)=10

//我的实现

function add(){
  var t=0;
Array.prototype.forEach.call(arguments,function(item,index)
{t+=item;});
return t;
}

add.curry=function(time){//time代表四次运算
var flag=0,total=0;
return function core(){
flag++;
total+=add(...Array.prototype.slice.apply(arguments));
if(time==flag){
flag=0;
return total
}else{
return core;
}
}
};

var add4=add.curry(4);

//演示效果
console.log(add4(123,123,3)(1321,4,1,2)(2)(1231,55));

原文地址:https://www.cnblogs.com/kiik/p/14221053.html