The earliest curry function was a bit polymorphic, that is, branches were selected internally based on function parameters:
//http://www.openlaszlo.org/pipermail/laszlo-user/2005-March/000350.html
// ★★On 8 Mar 2005, at 00:06, Steve Albin wrote:
function add(a, b) {
if (arguments.length < 1) {
return add;
} else if (arguments.length < 2) {
return function(c) { return a c }
} else {
return a b;
}
}
var myadd = add( 2 );
var total = myadd(3);
A pioneer in Japan may have used very complex regular expressions and eval to come up with a method that is more similar to eval before he figured out that arguments can also be converted into arrays using Array’s native method. Modern currying means function.
function curry(fun) {
if (typeof fun != 'function') {
throw new Error("The argument must be a function.");
}
if (fun.arity == 0) {
throw new Error( "The function must have more than one argument.");
}
var funText = fun.toString();
var args = /function .*((.*))(. *)/.exec(funText)[1].split(', ');
var firstArg = args.shift();
var restArgs = args.join(', ');
var body = funText.replace(/function .*(.*) /, "");
var curriedText =
"function (" firstArg ") {"
"return function (" restArgs ")" body
"}";
eval("var curried =" curriedText);
return curried;
}
]
Then the popularity of closures , and with the discovery of the technology of array conversion arguments, the modern currying function finally made its debut. Just like the geographical discovery of the Age of Discovery in the 15th to 17th centuries, the world of JavaScript suddenly opened up a lot.
The code is as follows:
//A simple modern currying function
function curry (fn, scope) {
var scope = scope || window;
var args = [];
for (var i=2, len = arguments.length; i < len; i) {
args.push(arguments[i]);
};
return function() {
fn.apply(scope, args);
};
}
General currying functions only have two functions. The execution situation is as follows. The first execution has insufficient parameters and returns to the internal function, and the second execution is finally completed. However, we can still do some articles regarding this parameter. Look at the following function:
The code is as follows:
function sum(){
var result=0;
for(var i=0, n=arguments.length; iresult = arguments[i];
}
return result;
}
alert(sum(1,2,3,4,5)); // 15
There is no so-called insufficient parameter problem. If you pass in a parameter, it will also calculate . But what if no parameters are passed in? Correct, the difference is whether there are parameters or not. We can make it execute itself continuously if the parameters are present. Finally, it is executed once without parameters. In other words, the previous steps are used to store parameters.
The code is as follows:
var sum2= curry(sum);
sum2 = sum2(1)(2)(3)(4)(5);
sum2(); // 15
Compared with the general currying function, this is a bit difficult . See the comments for details:
The code is as follows:
var curry= function(fn){//原函数的参数为函数
return function(args){//内部函数的参数为数组,由于立即执行,因此直接到第三重去
//args是相对于第三重内部函数可是全局变量
var self= arguments.callee;//把自身保存起来(就是那个数组为参数的第二重函数)
return function(){ //这才是第二次调用的函数
if(arguments.length){//如果还有要添加的参数
[].push.apply(args,arguments);//apply把当前传入的所有参数放进args中
return self(args);
}else{
return fn.apply(this,args);//apply的第二参数为数组
}
}
}([]);
};
function sum(){
var result=0;
for(var i=0, n=arguments.length; iresult += arguments[i];
}
return result;
};
var curry = function(fn){//原函数的参数为函数
return function(args){//内部函数的参数为数组,由于立即执行,因此直接到第三重去
var self= arguments.callee;//把自身保存起来
return function(){ //这才是第二次调用的函数
if(arguments.length){//如果还有要添加的参数
[].push.apply(args,arguments);
return self(args);
}
else return fn.apply(this,args);//执行
}
}([]);
};
var sum2= curry(sum);
sum2= sum2(1)(2)(3)(4)(5);
alert(sum2());
或者每次传入多个参数:
function sum(){
var result=0;
for(var i=0, n=arguments.length; iresult += arguments[i];
}
return result;
};
var curry = function(fn){//原函数的参数为函数
return function(args){//内部函数的参数为数组,由于立即执行,因此直接到第三重去
var self= arguments.callee;//把自身保存起来
return function(){ //这才是第二次调用的函数
if(arguments.length){//如果还有要添加的参数
[].push.apply(args,arguments);
return self(args);
}
else return fn.apply(this,args);//执行
}
}([]);
};
var sum2= curry(sum);
sum2= sum2(1,2,3);
sum2= sum2(4,5,6);
sum2= sum2(7,8,9);
alert(sum2());
但上面的函数有不足之处,最后怎么也要放个括号,我们想只要参数足够就返回结果,多出的参数忽略。改进如下:
function curry(f) {
if (f.length == 0) return f;
function iterate(args) {
if (args.length <= f.length)
return f.apply(null, args);
return function () {
return iterate(args.concat(Array.prototype.slice.call(arguments)));
};
}
return iterate([]);
}