Tuesday, February 27th, 2007
Currying in JavaScript
Dustin Diaz has a nice entry on currying in JavaScript a technique used often in languages such as LISP, Perl, and many others.
Dustin's curry implementation:
-
-
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);
-
};
-
}
-
This should look somewhat similar to Prototype bind and of course Dojo has dojo.lang.curry() which we mentioned awhile ago.












Ive posted about my experiments with currying ,including a stress test that reveals that currying in javascript is best avoided for larger recursions.
http://bosky101.blogspot.com/2006/10/edited-currying-in-javascript-stress.html
Having a curried UI function might well be an overkill . Instead i have another function called the IdempotentEvent which might be most suited for UI and DOM hacking . Its over at :
http://bosky101.blogspot.com/2007/02/adding-idempotent-event-handling-to.html
Keep Clicking,
Bhasker V Kode
Always a pleasure seeing one of my posts on Ajaxian. You rock Dion. Curried functions are everywhere in JavaScript so I figured it would be a nice and simple little task to write and share my own curry function. And obviously as you can tell, many other people have gone down this same path and its enlightening to see.
I don’t *really* want to split hairs, but this isn’t a curry, this is a partial. it’s still highly useful, and I think we’ll actually be removing Dojo’s curry() in favor of a new partial() function at some point. That said, the difference is that curry() functions traditionally return *accumulators*, not direct application functions. The accumulators return new “partials” for every call to the curry or previous accumulator that doesn’t meet the entire arity of the function (as originally declared).
Regards
is this not the same as prototype’s bind?
Alex, thanks for clearing that out. I was absolutely sure it was a partial, and since the only time I have come across the concept of currying is when reading the MochiKit documentation on partial, which says “this is not currying” I was a bit confused.
Partial is fantastic, curry I don’t understand. Could it be said that the partial above could be the result of a curry? That is, does a curry function spit out functions that have a similar effect to a partial?
Not much newsworthy there really!
You’ve posted functionality like Ext’s createDelegate (but a little less capable - createDelegate can insert your override argument list at a certain point in the real argument list if you don’t want your argument list to completely take over).
That’s one of my favourite functions in that library, and ultra useful for things like creating callback functions to handle DWR responses.
http://localhost:8080/ext/docs/output/Function.html#createDelegate
Ext has other Function extensions too. (And it deletes them from Function’s prorotype on page unload just for the buggy browsers out there)
I must be blind, but where is “arguments” getting set at?
@Hang On a Sec
In JavaScript, arguments is an object that exists in every function that is basically an iterable array of the passed arguments.
function add() {
var sum = 0;
for (var i = 0; i
blah that got destroyed
function add() {
var sum = 0;
for (var i = 0; i < arguments.length; i++) sum += arguments[i];
return sum;
}
add(1, 2); // returns 3
add(1, 2, 3, 4, 5); // returns 15