Saturday, March 22, 2014

Currying In JavaScript

You may also like to see:

Currying is technique mostly mentioned in functional programming as function currying.

What is Functional Programming?


Functional programming is a programming paradigm which primarily uses functions as means for building abstractions and expressing computations that comprise a computer program.

Confused! ok you do not need to understand what functional programming is for understanding currying. so ignore it.

What is currying?


Currying is to transform a function with many parameters to a function with less parameters or in other words, process of transforming a function with N parameters to a function that takes less than N parameters.

For example, a function takes A, B, C arguments and returns R as result i.e { A , B , C => R } currying allow you to change it to a function which takes only A  as argument and returns you a function which take B as argument and this second function takes B as argument and returns a function which takes C as arguments and this last method takes the argument C and returns you the Result R.

{A => { B => { C => R } } }

The curried effect is achieved by providing some of the arguments of the main function at first invocation, so that those values are fixed for the next invocation.

Lets see an example (Here is Demo)

var teacher = function(teacherName){
    return function(subject){
    console.log(teacherName+' teaches '+subject);
    }
}

var aliceTeach= teacher('Alice');

aliceTeach('JavaScript');
aliceTeach('Data Structures');


Here we used the concept of JavaScript Closure  (Closure allows you to use Parent scope variable in inner functions. ), first function takes the parameter teacherName and use this as closure in inner-function and return the function which has fixed value for teacher name but expecting subjects name. Since teacherName is prefilled, now calling function with subject has fixed teacher name with it.

Currying is helpful where your some arguments are fixed for certain module or series of function. For example you write a function that add two numbers (see demo):

function add(x,y){
    console.log('Sum is : '+(x+y));
}

add(4,2);

but what if there is situation you need to add 4 to different numbers, means your first argument is fixed. If you implemented without currying like above you need to write:

add(4,10);
add(4,21);
add(4,83);
...

There is a chance of mistake in passing some other argument instead of 4 if not still it is redundant to pass same argument many times.

If you implemented the function currying. (see demo here)

function add(x) {
    return function (y) {
        console.log('Sum is : ' + (x + y));
    }
}

var addFourTo = add(4);

addFourTo(10);
addFourTo(21);
addFourTo(83);

Now simply you need to pass one argument after binding first argument to a fixed value of 4 to a curried function.

You may also like to see:
Life insurance policy, bank loans, software, microsoft, facebook,mortgage,policy,