JavaScript - Currying
JavaScript - Currying
In JavaScript,curryingis a functional programming technique that is used to transform a function that takes multiple arguments into a sequence of functions that each takes a single argument. Currying is mainly used in event handling and to avoid passing the same variable as a function argument multiple times.
How to achieve currying in JavaScript?
There are two different ways to achieve currying in JavaScript, as given below.
- Using the closures function
- Using the bind() method
Currying using closures
In JavaScript, closures is a technique in which inner functions can access the variables of the outer functions. To achieve currying using the closures technique, we can use the sequence of functions, each taking a single argument.
Syntax
Users can follow the syntax below to achieve currying using the closures.
function funcName(a) {
return function (b) {
// add more functions
// OR
return a * b;
}
}
funcName(a)(b);In the above syntax, βfuncName()β function takes a single parameter, and it contains the inner function. The inner function also takes 1 parameter, and we use parameters of the outer and inner functions in the body of the inner function.
The above function given in the syntax is similar to the below function.
function funcName(a, b) {
return a * b;
}
funcName(a, b);Letβs understand currying via examples.
Example
In the code below, we have created the mul() function, which takes the single value as a parameter and returns the function taking βbβ as a parameter. The inner function also returns another function, which takes the βcβ as a parameter and returns the multiplication of the βa, βb, and βc.
When we call mul(2) function, it returns the whole inner function as shown below.
return function (b) {
return function (c) {
return a * b * c;
}
}When we call the mul(2)(3) function,
return function (c) {
return a * b * c;
}When we call mul(2)(3)(4), it returns the result from the second inner function.
In the output, you can observe that the function returns the result 24, which is the multiplication of 3 values.
Output
The result is: 24This way, currying helps to make code more modular and reusable as it uses higher-order functions. Whenever it is a must to pass a number of arguments equal to the parameters of the function to get accurate results, currying is useful. For example, if you donβt pass 3 arguments in the above example, it wonβt return the result.
Currying using bind() method
In JavaScript, the bind() method is used to create a new function and store it in the variable. The bind() is often used to partially prepend arguments to the current arguments of the function, effectively allowing you to curry functions.
Syntax
Users can follow the syntax below to use the bind() method to achieve currying in JavaScript.
let multiplyByTwo = multiply.bind(null, 2);
let multiplyByTwoAndThree = multiplyByTwo.bind(null, 3);
multiplyByTwoAndThree(4); // Outputs: 24In the above syntax, βmultiplyβ can be a function taking multiple arguments. We prepend arguments one by one using the bind() method and achieve currying.
Example
In the code below, the multiply() function takes 3 arguments and returns the multiplication result. The βmultiply.bind()β, adds one argument to the multiply() function, and returns the updated function. Similarly, the βmultiplyByTwoAndThree()β function stores the multiply function having two predefined arguments bound to it.
When we call the βmultiplyByTwoAndThree()β function with the single argument, it returns the 60, multiplication of all 3 arguments.
Output
The result is: 60Use cases of Currying
The currying technique is used in a lot of scenarios in real-time software development as it allows code to be more modularized and reusable. Here, we have given some real-time use cases of currying.
- Currying can be used to handle asynchronous operations, in which functions return the promises.
- It is even helpful in handling situations where we need to partially apply functions with specific arguments that can represent the current context of the event.
- It allows the creation of highly configurable middleware functions that can be used across different parts of the code.