Currying in JavaScript
Published by
sanya sanya
The Currying is an advanced technique used in JavaScript to work with functions. The main work of currying is to transform the functions.
The concept of currying can be explained using a simple example of the Hotel that makes Chicken Dish orders. Suppose the Hotel has a function called MakeChicken that takes three arguments: flavor, size, and name. With currying, we could transform this function into a sequence of three functions that each take a single argument.
For example, we could define a MakeChicken function that takes the flavor argument and returns a new function that takes the size argument. That function, in turn, returns another function that takes the name argument and finally returns the finished Chicken.
It translates a function altering its state from function(name, age, location) to function(name)(age)(location). Moreover, Currying doesn't call a function, but it transforms it.
The code showing the working of currying is mentioned below -
function curry(f) { return function(length) { return function(breadth) { return f(length, breadth); }; }; }
function area(length, breadth) { return length * breadth; }
let parameters = curry(area); console.log( parameters(4)(6) );
//Output: 24
We have created the curry(f) function which is a helper function. And, inside the curry(f) function we are returning two arguments which will translate area(length, breadth) to area(length)(breadth).
##Why use Currying?
Several advantages of using curring are supported by Javascript which makes our programs better. Some advantages of using currying are mentioned below -
- It makes our programs less Complex and more readable.
- It helps us to avoid passing the same variables multiple times.
- It creates Higher Order Function.
- Nothing in the code can be lost using Currying.
- Partial Functions can be easily generated.
##Advance Currying
`Advanced Currying` involves creating functions that take many arguments not just a fixed amount of numbers or values.
However, the process of Advanced Currying can be achieved using the closures and recursion to return the nested functions which takes a single argument until all the arguments have been received and the original function can be called with them.
The code showing the working of the Advanced Currying is mentioned below -
function curry(func) { return function curried(...args) { if (args.length >= func.length) { return func(...args); } else { return function(...nextArgs) { return curried(...args.concat(nextArgs)); } } }; } function add(x, y, z) { return x + y + z; } const addCurry = curry(add); console.log(addCurry(1)(2)(3));
// Output: 6
In this implementation, the curry function takes a function func as its argument and returns a new curried function. This curried function uses the rest parameter ...args to collect any number of arguments passed to it.
If the number of arguments collected so far is greater than or equal to the number of arguments required by the original function (func), the curried function calls func with the collected arguments and returns the result.
Otherwise, the curried function returns a new function that takes additional arguments (...nextArgs) and calls itself recursively with the combined set of arguments (args.concat(nextArgs)).
Library
WEB DEVELOPMENT
Basic
HTML - Hyper Text Markup Language
CSS - Cascading Style Sheets
JavaScript
An Introduction to Javascript!
How to Run JavaScript Code
Variables in Javascript
Numbers in JavaScript
JavaScript Operators
Data Types in JavaScript
Conditional Statements
Switch Statements
Loops in Javascript
Arrays in JavaScript
Strings in JavaScript
Objects in JavaScript
Object Methods in JavaScript
Functions in JavaScript
Object Referencing and Copying in JavaScript
' this' keyword
Asynchronous Programming in JavaScript
Callbacks in JavaScript
Promises in JavaScript
Constructor Functions in JavaScript
Async and Await in JavaScript
Type Conversion in Javascript
DOM
Currying in JavaScript
Network Request
Frontend
Backend
Interview Questions
FAANG QUESTIONS

