Currying in JavaScript is a feature that is only enabled by the functional nature of programming language. Functional programming is a style of programming that attempts to pass functions as arguments(callbacks) and return functions without side-effects(changes to the program’s state).
Currying is transforming a function that takes multiple arguments into a sequence of nested functions. It recursively keeps returning new functions until all the parameters are exhausted. Closure takes in the responsibility of keeping the variables of the previous functions in the function chain alive and ready for use in the final execution.
This article will use arrow style functions, you can know more about them here. Let’s look at an example of this.
const add = (a,b,c) => { reuturn a+b+c; }; //Normal Function
const addCurr = a=> b=> c=> a+b+c; //Curried Function
console.log(add(1,2,3)); //6
console.log(addCurry(1)(2)(3)); //6
So here we see how a function add which takes 3 arguments has been converted to its curried form. The curried version is called addCurry(1)(2)(3). This may seem a bit confusing, let me breakdown step by step to make it easier to understand.
const addCurr = a=> b=> c=> a+b+c;
const curr1 = addCurr(1);
const curr2 = curr1(2);
const curr3 = curr2(3);
console.log(curr3); //6
This is the expanded version of the call addCurry(1)(2)(3). addCurr(1) returns a function, which we store in curr1, that takes one argument and in turn, returns a function. Thus curr1 is also a function that takes in an argument and returns a function. The same is repeated for the curr2. Now, curr2 returns a function, that is stored in curr3, which takes an argument and adds the arguments received by curr1 and curr2 to it.
Uses of Currying:
At first sight, currying in JavaScript does not look like to be a feature with a lot of utilities. But as you grow with experience, you create utility for currying. Here are some of the uses of currying :
-
-
- Make little code snippets that can not only be reused but also reconfigured.
Suppose we have to deal with string quite a lot. We some times need to get first n chars from start and other times from different locations. Let us see how we can deal with it using currying.const substringCurry = start=>length=>str=> str.substr(start, length); const subStringStart = (length, str) =>substringCurry(0)(length)(start); const firstThreeChars = subStringStart(3,"ABCDE"); //ABC const firstTwoChars = subStringStart(2, "ABCDE"); //AB
There is one more example of house discounting which I studied on some blog while studying currying. Suppose you are a property dealer and want to give a discount of 10% to one set of customers and 25% to another set of customers. Here is how currying modifies this approach.
const discount = (discount, price) => { return (price) => { return price*(1-discount); } } const discount10 = discount(0.1); const discount25 = discount(0.25); console.log(discount10(100)); //90 console.log(discount25(100)); //75
So. this is how we used on base function and used currying to compose new functions as per our requirements.
- Avoiding repeated lengthy function calls with same arguments.
Suppose there is a function that has 5 arguments out of which 3 are the same for 80% of the calls. By applying the concept of currying, you can avoid repeated function calls with the same arguments by making a temporary function 3 of whose arguments are already fixed in the closure.
- Make little code snippets that can not only be reused but also reconfigured.
-
Closures are the actuators that make currying possible in JavaScript. As already mentioned, you might not find the concept of currying very useful initially, but with experience and time, you will understand the benefits of this pattern. So I would advise all to get their hands dirty and apply Currying in Javascript in your projects, explore the areas you can use them and eventually you will master it.
0 Comments