JavaScript / Programmings / Technologies

Currying in JavaScript

What is Currying?

Currying is the technique of evaluating a function with multiple arguments, into evaluating a sequence of functions with single argument. That is partial application.

In other words, currying refers a process of breaking down a function that takes n arguments into n functions that each take single argument.

That is, we turn function sum

sum(1,2,3);

into

sum(1)(2)(3);

Advantage of this technique is, these little pieces can be configured and reused with ease, without clutter.

Writing Curry Function

Take the following simple example,

function sum( a, b, c ) {
  return a + b + c;
}
var curried = curry(sum);
curried(1)(2)(3); // 6

So we need the curry function, to which we pass the function to get curried. Since we don’t have curry function in native JavaScript, we have to create generalized curry function.

You can install curry function package, that is available at npm (https://www.npmjs.com/package/curry). Also you can write your own curry function. One example curry function is shown below,

function curry( fn ) {
  var arity = fn.length;
  return (function resolver() {
    var memory = Array.prototype.slice.call( arguments );
    return function() {
      var local = memory.slice(), next;
      Array.prototype.push.apply( local, arguments );
      next = local.length >= arity ? fn : resolver;
      return next.apply( null, local );
    };
  }());
}

Here arity is the number of arguments that the function has, and resolver function is a closure retains the list of arguments, responsible for checking number of arguments and deciding whether to return another partially applied function or the return value of the original function with all of the arguments applied.

Every time a curried function is called, we add the arguments to the array saved in the closure, when the number of arguments equals to the arguments that original function expects we call it, otherwise return a new anonymus function

lets call pass sum function to this curry function and check,

function sum( a, b, c ) {
  return a + b + c;
}
var curried = curry(sum);
var add1 = curried(1);
var add1and2 = add1(2);
add1and2(3); //returns 6

When we create curried function by passing sum function into curry function, it store arity as 3 and resolver returns an anonymous function. On calling curried function, actually we calling the anonymous function returned by the resolver. And store the arguments on closure. When the number of arguments become 3, that is the number of arguments become equal or greater, then, instead of returning a new function, return the result of calling sum function with all of the arguments we’ve been stored, and that gives us a value of 6

References:

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s