Arity & Currying

Home / Blogs / Arity & Currying

This is a series of blogs where I collect some interesting programming concepts, new things I learn

Arity represents the number of arguments we pass to a function (or module). Say you may pass N args to a function.

Currying is the process of reducing the number of arguments you pass to 1 but increase the number of functions to N.

For Eg. To add three variables a, b and c you may write func like below

function add (a,b,c){
    return a+b+c;
}

add (45, 46, 47);

Currying the same above function is then like below

function add (a){
    return function add (b){
       return function add (c){
           return a+b+c;
       };
    };
}

add (45) (46) (47);

Notice how each function returns another function until the last function does the final operation of addition.

As and when you have some arguments of the functions in the main program, you can call the initial set of functions, and when you have all arguments, then the final bit of function completes your program.

Leave a Reply

Your email address will not be published. Required fields are marked *