#12) What is Currying in JS❓
Mayank Yadav
Posted on August 2, 2021
🔰Currying
✅It is a advanced technique of working with functions.
✅It is used in other languages also.
✅It is basically to transform a function with n arguments, to n functions of one or less arguments.
f(a, b, c) into f(a)(b)(c)
💠For better understanding, here's an example👇
✅First create a helper function curryFun(n) that performs curring for two arguments.
✅curryFun(n) does the curring transformation.
💠As you can see, the implementation is straightforward
✅It has just two wrappers.
✅The result of curryFun(n) is a wrapper function(x).
✅When it is called like multiply(10), the argument is saved in the Lexical Environment, and a new wrapper is returned function(y).
✅Then this wrapper is called with (2) as an argument, and it passes the call to the original sum.
⚠So if anyone want to know about more advance currying implementation such as _.curry, just comment it, I'll help you out or anyone can also do!!!
Posted on August 2, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
July 31, 2022