Here's a function that I came across:
function curry(fn) {
var args = [].slice.call(arguments, 1);
return function() {
return fn.call(this, args.concat([].slice.call(arguments)));
};
}
I always thought this was the correct way for the function to look and work like this:
function add(a, b, c, d) {
return a+b+c+d;
}
curry(add, 1, 2)(3, 4);
However, after reading the information on Wikipedia Article, it seems that
it can be called as a chain of functions, each with a single argument
This means the curry function should actually look like this:
function curry(fn) {
var args = [];
return function curring() {
args = args.concat([].slice.call(arguments));
if (args.length >= fn.length) {
return fn.apply(this, args);
} else {
return curring;
}
};
}
And it should be used in this manner:
function add(a, b, c, d) {
return a+b+c+d;
}
curry(add)(1)(2)(3)(4);
Is this approach correct?