Is there a way to chain a curried function such that you can have an arbitrary number of invocations? The following example only allows you to apply the function twice, the first time calling the curry function, and the second calling the add function which returns a numerical value.
var slice = Array.prototype.slice;
function curry(fn) {
var storedArgs = slice.call(arguments, 1);
return function () {
var newArgs = slice.call(arguments),
args = storedArgs.concat(newArgs);
return fn.apply(null, args);
}
}
function add () {
var numbersToAdd = slice.call(arguments);
var total = 0;
for (var i = 0, numberCount = numbersToAdd.length; i < numberCount; i++) {
total += numbersToAdd[i];
}
return total;
}
console.log(curry(add, 1000)(1, 1, 1, 1, 1, 1, 1, 1));
//does not work because number is returned rather than a function after second invocation
//console.log(curry(add, 1000)(1, 1, 1, 1, 1, 1, 1, 1)(1));