def c = { arg1, arg2-> println "${arg1} ${arg2}" }
def d = c.curry("foo")
d("bar")
function sum(a, b) {
return a + b;
}
sum(10, 5) // 15
var addTen = sum.curry(10);
addTen(5) // 15
function curry(f, args) {
var thisF = f;
var thisArgs = Array.prototype.slice.apply(args);
return function(args) {
thisArgs = thisArgs.concat(args);
if (thisF.length <= thisArgs.length) {
return thisF.apply(thisF, thisArgs);
} else {
return this;
}
}
}
function sum(a, b, c) {
return a + b + c;
}
function test() {
sum(1, 2, 3); // 6
x = curry(sum, [1]);
x([2]);
x([3]); // 6
x = curry(sum, [1]);
x([2, 3]); // 6
}
[ updated 17 Jun 2009 ]
Cleaner implementation from #Javascript on IRC:
Function.prototype.curry = function() {
var func = this, a = Array.prototype.slice.call(arguments, 0);
return function() {
return func.apply(this, a.concat(Array.prototype.slice.call(arguments, 0)));
}
};