Member-only story
Javascript- Currying VS Partial Application
A lot of people get confused in between currying and partial application and many of us do not know what, where and when we should use them. So this post will cover the practical usage and difference in them.
So lets' start with definitions.
Currying
Is a technique for converting function calls with N arguments into chains of N function calls with a single argument for each function call?
Currying always returns another function with only one argument until all of the arguments have been applied. So, we just keep calling the returned function until we’ve exhausted all the arguments and the final value gets returned.
// Normal function
function addition(x, y) {
return x + y;
}// Curried function
function addition(x) {
return function(y) {
return x + y;
}
}
Note: Curry takes a binary function and returns a unary function that returns a unary function. Its JavaScript code is
function curry(f) {
return function(x) {
return function(y) {
return f(x, y);
}
}
}