Hello everyone,
If you don't know about currying so this blog is very helpful for beginner. After reading this blog you know about currying.
so the first question is
What is currying
Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument.
In other words, currying transforms a function with multiple arguments into a sequence/series of functions each taking a single argument.
Let's make a simple example that adds two numbers. But first, without currying:
const addNum = ( a, b, c ) => a + b + c
Now let's build an example with currying function addNumber that takes three arguments creates three functions:
const addNum = num1 => num2 => num3 => num1 + num2 + num3
const firstFun = addNum(1)
const secondFun = firstFun(2)
const thirdFun = secondFun(3)
console.log(thirdFun)
What we have done here is a nested function, so each of these functions takes one argument that returns another argument and the function doesn’t complete until it receives all parameters.
Let's see another example of currying
Example-2
This example explains the currying technique with the help of closures.We are exposing our function from another function, so the closure will be created. Closure always contains the function definition along with the lexical environment of the parent, both things remain connected as a bundle. Hence, it does not matter where we invoke them, the all inner functions will always hold access to the variable of their parent
function calculateVolume(length) {
return function (width) {
return function (height) {
return length * width * height;
}
}
}
console.log(calculateVolume(3)(3)(3));
Output: 27
How does currying work?
Currying works by natural closure.The closure created by the nested functions to retain access to each of the arguments.So inner function have access to all arguments.
Use of currying
Currying is useful when you have to frequently call a function with a fixed argument. It helps to avoid passing same variable again and again.
Why it’s useful ?
Currying helps you to avoid passing the same variable again and again.
It helps to create a higher order function. It extremely helpful in event handling. See the blog post for more information.
Little pieces can be configured and reused with ease.
Why currying?
We can use its returned functions to make a lighter version of an existing function. It’s helpful when we have many places that use a function with exactly the same way. Our implementation will be shorter and more readable. Considering, for example, the following function: If we want to define the function error , warn , and info , for every type, we have two options. Currying provides a shorter, concise, and more readable solution.
Currying versus partial application
The difference between the two is: Currying always produces nested unary (1-ary) functions. The transformed function is still largely the same as the original.
Partial application produces functions of arbitrary arity. The transformed function is different from the original – it needs less arguments.
Interestingly, with a curried function and curried invocation, it is easy to achieve the same effect as binding one argument (performing this operation several times yields the general case): To bind the first argument to a value, you simply apply the outer-most of the nested functions to that value, which returns the desired new function.
Conclusion:
I hope you enjoyed to reading this blog and clear understanding of what is currying, how it is working with different example. you also know about why currying is useful. There are probably better applications for currying, but personally, I use it as a checking tool — especially when things need to be done in a particular sequence.