Saved Bookmarks
| 1. |
What is currying in JavaScript? |
|
Answer» Currying is an advanced technique to transform a function of arguments n, to n functions of one or fewer arguments. Example of a curried function: function add (a) {return function(b){ return a + b; } } add(3)(4) For Example, if we have a function f(a,b), then the function after currying, will be transformed to f(a)(b). return a*b; } function currying(fn){ return function(a){ return function(b){ return fn(a,b); } } } var curriedMultiply = currying(multiply); multiply(4, 3); // Returns 12 curriedMultiply(4)(3); // Also returns 12 As one can see in the code above, we have transformed the function multiply(a,b) to a function curriedMultiply , which takes in one parameter at a time. |
|