By jober
via artfulcode.nfshost.com
Published: Dec 05 2007 / 14:54
Currying, known in Python land as partial application, is a technique in which a function taking multiple arguments composes a function that takes fewer arguments (in most languages, reducing to one, although this is not the case in Python) by partially applying it to given parameters. For example, a function, sum, might be used to compose a new function called "plus_one" by currying it with the value of one. The composed function is not evaluated; it is returned as a function object which may then be applied to other parameters.
Comments
reido56 replied ago:
The Javascript example doesn't appear to do what the blog says it does. Test code:
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);
};
}
function alertTwoArg(firstArg,secondArg) {
alert(firstArg + ',' + secondArg);
}
var oneCur = curry(alertTwoArg,window,"firsty");
var twoCur = curry(oneCur,window,"secondy");
twoCur();
reido56 replied ago:
Blog has been updated.
jober replied ago:
Thanks for pointing out the problem with the function!
Voters For This Link (15)
Voters Against This Link (0)