Currying takes a function that receives more than one parameter and breaks it into a series of unray (one parameter) functions. Hence, the currying function takes only one parameter at a time.
Uses of Currying Function
- Currying is helpful in Event handling.
- By using the currying function, we can avoid passing the same variable many times.
How does Currying Work?
Example One: A Simple, Three-Parameter Function
In this example we will see a simple function that will accept three parameters:
Explanation: In the above example, we have given three parameters of which addition is performed and the result is printed as the output.
Example two: Converting an Existing Function into a Curried Function
In this example, we will use see the implementation of the currying function where one argument will be accepted by the function and it will return a series of function.
Explanation: In the above example, we '1' is passed to the function "sum". It will return a function like:
Now, the above function definition will be treated as "sum1" and takes "b" as an argument. Then we will call the function "sum1" and pass this in 2. Then the sum1 will return the third function as-
Now, the returned function will be stored in the variable "sum2". Then the function "sum2" will be called and it will take '3' as the parameter.
Now, the calculation will be performed with the previous parameters x,y, and z as 1,2, and 3 respectively and it will return 6.
Example Three: Creating a Friend Request Curry Function
In this example, we will create a curry function where a friend user can send friend requests to his/her friend.
Explanation: In the above example, a function "sendReq" is created, and "greet" is its only argument. This function will return the name of the person and the message for the person as specified by the user. The result will print as output when the function is invoked.
Basic vs. Advanced Currying Techniques
- Basic Currying
- Advanced Currying
In Basic currying, a single argument is taken by the function and returns a series of functions under which all the other parameters are contained.
In this currying, the function is incomplete until all the parameters are received by the function. Otherwise, the function will not give any fruitful results.
While in Advanced currying, there is a fixed number of parameters in the function. In it, the function is received as an outer function which is a wrapper function.
- Basic currying:
Explanation: In the above code, a function "getCakeIngredients" is created whose argument is "ingred_1". This "ingred_1" will return a series of functions as other ingredients that we required for making the cake. Thi function will be incomplete until it receives all the ingredients as a parameter.
- Advanced currying:
Explanation: In the above example, the "Adv_curry" function is received as the outer function which is a 'wrapper function'. Here one more function is 'curried' returned which receives arguments with an operator named 'spread operator'.
The number of arguments passed here will show the length property of the function. The arguments will keep increasing every time the calling of function.
Modern Currying With ES6
Currying vs. Partial Application
- Currying: A function in which multiple arguments are accepted is known as the Currying function. By applying the currying function, a function is divided into a series of many functions and each of these functions will accept a single argument unless all the parameters are accepted.
- Partial Functions: The idea of the partial application comes from the fact that you can take a system and then split them into multiple systems. In partial application, we create a new function by prefilling some of the arguments to the original function.
Let us see an example and understand the partial application.
Explanation: In the above example, a function is created that will add the numbers and return the result as output. A partial function is one that converts a function into another function but a number of arguments is less there.
- Using currying, higher-order functions can be created in functional programming.
- Event listeners can be triggered using currying.