# Functional programming – all you need to know about currying.

Currying is the process of taking a function that accepts n arguments and turning it into n functions that each accepts a single argument. The arity of a function is the number of arguments that it accepts. A function that accepts a single argument is unary, two arguments binary, three arguments ternary, and n arguments is n-ary. Therefore, we can define currying as the process of taking an n-ary function and turning it into n unary functions.
Currying is NOT named after curry the food; instead, it’s named after Haskell Curry, a 20th century mathematician and logician. Haskell Brooks Curry also happens to have three programming languages named after him – Haskell, Brook, and Curry. The more you know.
Let’s first look at a curried function:
const addThreeNumbers = a => b => c => a + b + c;

console.log(addStep1); // b => c => a + b + c
console.log(addStep2); // c => a + b + c

However, what if we wanted the best of both worlds?
const addThreeNumbers = (a, b, c) => a + b + c;

In such a case, we would need a currying function! The following is a basic example:
const curry = func => {
// this is yet another closure!
const args = [];
const keepCurrying = arg => {
args.push(arg);
if (args.length === func.length) return func(…args);
return keepCurrying;
};

return keepCurrying;
}

Now we can do:
const addThreeNumbers = (a, b, c) => a + b + c;

Most times however, you can use a pre-made curry powder function in a library such as Lodash or Ramda:
const addThreeNumbers = (a, b, c) => a + b + c;

As you can tell, these functions are quite a bit more advanced. The above code doesn’t even cover all the use cases – check out their respective docs for more information.
Now for the question you might really be wondering.

Why?

You might be thinking:
Currying seems like a total waste of time.
Why run a function three times when you can just run it once?