Advance Javascript series: Currying
Currying is a technique in functional programming that involves transforming a function with multiple arguments into a series of functions that take one argument each. Currying has become a popular technique in modern functional programming languages such as JavaScript and Haskell.
In this blog post, we’ll explore the concept of currying, its benefits, and how to use it in JavaScript.
What is Currying?
Currying is a process of transforming a function that takes multiple arguments into a series of functions that each take a single argument. The resulting series of functions can be called one after the other to achieve the same result as calling the original function with all its arguments. Here’s an example of a curried function in JavaScript:
function multiply(a) {
return function(b) {
return a * b;
}
}
// Calling the curried function
const multiplyByTwo = multiply(2);
console.log(multiplyByTwo(5)); // Output: 10
In the example above, the multiply
function is curried. It takes an argument a
and returns another function that takes an argument b
. The returned function multiplies a
and b
and returns the result.
To call the curried function, we first call the multiply
function with the first argument 2
. This returns a new function that we assign to the variable multiplyByTwo
. We can then call this new function with the second argument 5
, which multiplies 2
and 5
and returns the result 10
.
Benefits of Currying
Currying offers several benefits over traditional function calls with multiple arguments. Here are a few:
- Reusability: Curried functions can be reused more easily than functions with multiple arguments. Once you have a curried function, you can use it to create new functions with specific arguments without modifying the original function.
- Flexibility: Currying allows you to create more flexible functions that can be partially applied. This means that you can call the function with some of its arguments and return a new function that can be called with the remaining arguments later.
- Code readability: Curried functions are often easier to read and understand than functions with multiple arguments. Each function takes a single argument, which makes it clear what the function does.
Using Currying in JavaScript
JavaScript has become one of the most popular programming languages in recent years, thanks in part to its support for functional programming techniques such as currying. Here are a few ways to use currying in JavaScript:
- Partial Application: Partial application is a technique where you call a function with some of its arguments and return a new function that can be called with the remaining arguments later. Here’s an example of a partially applied curried function:
function add(a) {
return function(b) {
return function(c) {
return a + b + c;
}
}
}
const addTwo = add(2); // returns a function that takes b and c
const addFive = addTwo(3); // returns a function that takes c
console.log(addFive(5)); // Output: 10
In the example above, we have a curried function called add
that takes three arguments. We first call the add
function with the argument 2
, which returns a new function that takes the arguments b
and c
. We then call this new function with the argument 3
, which returns another new function that takes the argument c
. Finally, we call this third function with the argument 5
, which adds 2 + 3 + 5
and returns the result `10