Currying in javascript
Currying is an advanced technique of working with functions. It’s used not only in JavaScript, but in other languages as well.
What Currying is ?
Currying simply means evaluating functions with multiple arguments and decomposing them into a sequence of functions with a single argument.
In other terms, currying is when a function — instead of taking all arguments at one time — takes the first one and returns a new function, which takes the second one and returns a new function, which takes the third one, etc. until all arguments are completed.
Currying does not call a function. It just transforms it. Let’s understand with example:-
in above example explains the currying technique with the help of closures. During the thread of execution, the curried_sum() function will be invoked. Inside there is anonymous function, receiving a parameter and returning some code.When code is executing from one function to another function closure will be created.
Closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). whenever we invoke the function, the all inner functions will always hold access to the variable of their parent. As soon as we have got the returned result from (b)=>{} as a function the next argument is ready to be passed to ©=>{}, this process will continue till the second last function. Finally, the innermost return keyword returns the expected result.
why do we need currying in Javascript ?
Let’s understand this with a real time use case.
Suppose you fetch a data something like this:
const all_users = [
{
uid:'1'
username:'its_hishu',
email:'hitesh@test.com',
},
{
uid:'2'
username:'tiptop',
email:'dev@test.com',
},
]
In the above data, you need to fetch id from the array of objects. you can simply use map function to simply iterate over it and get the id’s from it.
const users = all_users.map(item => item.uid)
it will return an array of ids. what happens if we need to do the same functionality again for another array of objects
let usersFromAnotherApi = [
{
uid:'5'
username:'coding_verse',
email:'hitesh@test.com',
},
{
uid:'6'
username:'tiptok',
email:'dev@test.com',
},]
you need to do the same loop and get only the ids from it. you can simplify this way by using curry
const getUserDetails = function(property) { return function(object) { return object[property]
}}
Now, you need to call the function get() which will return a function.
const getId = getUserDetails("uid")
After that, you need to pass the function inside the map loop which will get you all the id’s
let all_user_ids = users.map(getId)
console.log("uids", all_user_ids)
it will print output something like this
[1,2,5,6]
Now, you can run the following function to the second array of object. no need to rewrite again.
const user_name_list = names.map(getUserDetails("username"))console.log("name_list", user_name_list)
Currying Use Cases
- Function Composition — function composition is an important concept in javascript which needs a complete post. In simple terms, it is a way to combine two or more functions to return the result. For Ex, result = a( b(x) ) where b(x) is a function which returns a result.Further, the result will be passed as a argument to a function.
- First-class function — you can be able to create a first class function. first class function is nothing but a function can be passed as an argument.
Do claps comments if you understand currying, You can also follow me on `its_hishu` on instagram. In future i’ll posting more stuff like this Happy Coding :-)