Currying in JavaScript

Written by on February 27th, 2007 in Ajax News.

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:

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

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

Source: Ajaxian
Original Article: http://ajaxian.com/archives/currying-in-javascript

Leave a Reply

You must be logged in to post a comment.



Site Navigation