What is currying?
Currying is defined as changing a function having multiple arguments into a sequence of functions with a single argument. It is a process of converting a function with more arity into a function having less arity. The term arity means the number of parameters in a function.
It is a transformation of functions that translate a function from callable as add(1, 2, 3) into callable as add(1)(2)(3). It only transforms a function instead of calling it. It returns a new function until all arguments get exhausted.
Suppose a function with some arguments so instead of taking all arguments once, it takes the first one, and returns a new function which takes the second one and returns a new function that takes the third one, and it will keep going until all arguments are fulfilled.
For instance, suppose a function fun with three arguments A, B, and C, and it returns value res. When it is curried, it will be broken down into three functions let be X, Y, and Z. The new function X will take A as an argument and will return the function Y, which will take B as an argument and return the function Z, which takes C as an argument and finally returns the required value res.
Benefits of Currying
Currying using bind() method
So, in this way we can reuse our function using currying.
Currying using closures
How to convert an existing function into a curried version?
Now, let's see the conversion of an existing function into its curried version.
Here, there is a simple function, in which we are not using currying.
In the above code, we have used currying as the parameters passed one by one until the last function calls the last parameter. So, before and after currying on the same function with the same parameters, the output will remain the same.
So, that's all about the article. Hope you understand the concept of currying and also the process of achieving it.