Currying is the technique of translating the evaluation of a function that takes N arguments into evaluating a sequence of N functions, each with a single argument.
This process is done by calling the function with one parameter, and return a new function with the parameter already bound inside a closure.
For example, let’s say we have an
add function that takes two parameters
// The native function definition would be to have a and b as parameters: add(3, 5) // After currying the function, we can then apply it like so: curryAdd(3)(5)
This is an interesting technique allowing to partially call a function, leaving the rest of the call for later.
For instance, with our previous
var add3 = curryAdd(3); var add10 = curryAdd(10); // Then we can call add3(5) // => 8 add10(5) // => 15
Thus, the following are equivalent:
foo(1)(2)(3)(4)(5); foo(1, 2, 3, 4, 5); foo(1)(2, 3, 4, 5); foo(1, 2)(3)(4, 5);
It’s “syntactic sugar” that most libraries use, and for them the end result is the same as when you call