Tuesday, February 27th, 2007

Currying in JavaScript

Category: JavaScript, Library

<>p>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:

javascript
< view plain text >
  1. function curry (fn, scope) {
  2.     var scope = scope || window;
  3.     var args = [];
  4.     for (var i=2, len = arguments.length; i < len; ++i) {
  5.         args.push(arguments[i]);
  6.     };
  7.     return function() {
  8.         fn.apply(scope, args);
  9.     };
  10. }

This should look somewhat similar to Prototype bind and of course Dojo has dojo.lang.curry() which we mentioned awhile ago.

Related Content:

9 Comments »

Comments feed TrackBack URI

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

Comment by Bhasker V Kode — February 27, 2007

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.

Comment by Dustin Diaz — February 27, 2007

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

Comment by Alex Russell — February 27, 2007

is this not the same as prototype’s bind?

Comment by cdude — February 27, 2007

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?

Comment by Theo — February 28, 2007

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)

Comment by Animal — February 28, 2007

I must be blind, but where is “arguments” getting set at?

Comment by Hang On a Sec — February 28, 2007

@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

Comment by Andy Kant — February 28, 2007

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

Comment by Andy Kant — February 28, 2007

Leave a comment

You must be logged in to post a comment.