Currying ()

Currying ()

JavaScript Concept

Hello developers!! 🤝
In this post, I would like to discuss techniques to work with functions in JavaScript called Currying().
If you have given frontend interviews you must have encountered this topic and if you have not then you will surely get this topic in your upcoming interviews 👀.

As we all know JavaScript is so interesting So,
Let's learn something interesting... 💥

INTRODUCTION

In javascript, there exists an advanced technique for working with functions, It is called CURRYING(). However, it is not only used in JavaScript but also in other programming languages.

Generally, it is the transformation of functions. So it translates a function from a callable function(a,b,c) to function (a)(b)(c).

Note: An American mathematician named Haskell Curry developed this technique, that’s why it is called currying.

Transformation of normal function & Currying function

Meaning in short

Curried functions are also constructed by chaining closures & by defining & immediately returning their inner function simultaneously. The number of arguments a function takes is also called ARITY.

Why is Currying helpful?

  1. It helps you to avoid passing the same variable again and again.

  2. It helps to create a higher-order function.

  3. It helps in removing the repetition of code.

In-Depth

Currying() is a functional programming technique. where a function with multiple arguments is transformed into a series of functions each function takes a single argument. This allows us to create a new function by partially applying some of these arguments, Creating a more specialized function that can be reused for specific use cases.

Basically, We wrap a function inside another function. This means we are going to return a function from another function to obtain this kind of translation. The parent function takes the first provided argument and returns the function that takes the next argument and keeps this on repeating till the number of arguments ends.

Example of Currying

function Volume (length ) {
    return  function (breath) {
        return function (height) {
            return  length breath height ;
            }
        }
    }        

console.log(volume(2)(3)(4));

Explanation of example

This example explains the currying technique with the help of closures. During the Thread of execution, the Volume function will be invoked. Inside there is an anonymous function that receives a parameter and returns some code. We are exposing our function to another function, so closure will be created. Closure always contains the function definition along with the lexical environment of the parent, both things remain connected as a bundle. Hence, it does not matter where we invoke them, all inner functions will always hold access to the variable of their parent.

As soon as we have got the returned result as a function the next argument is ready to be passed, this process will continue till the second last function. The innermost return keyword returns the expected result.

Benefits of using Currying

There are several reasons why currying is used in JavaScript –

1. Partial application: Currying allows you to create a new function with some of the arguments already defined. This can be useful when you want to create a specialized version of a function that you can reuse throughout your code.

2. Composition: Currying is often used in functional programming to compose functions. By breaking a function into smaller functions that each take a single argument, you can easily combine them to create more complex functions.

  1. Flexibility: Currying allows you to create functions that can accept different numbers of arguments. This can be useful when you want to create a function that can be used in a variety of contexts.

  2. Code reusability: Currying can help you avoid repeating code by creating reusable functions that can be used in different parts of your codebase.

Set the default value of the function

function Sum (a){
    return function (b){
        return a + b ; 
    } 
}
let double  = Sum(2);
console.log(double(3));
Output : 5

//Giving value of a as default value
function Sum (a){
    return function (b){
        return a + b ; 
    } 
}
let double  = Sum(2);
console.log(double(7));
Output : 9

//Giving value of a as default value
function Sum (a){
    return function (b){
        return a + b ; 
    } 
}
let double  = Sum(5);
console.log(double(3));
Output : 8

//Giving value of b as default value
function Sum (a){
    return function (b){
        return a + b ; 
    } 
}
let double  = Sum(2);
console.log(double(3));
Output : 5

//Giving value of b as default value

Tip: Currying functions take the value of arity (parameter) as per the execution of the function – when we give the default value of the first arity, the function used it as a’s value but when we give the default value of the second arity, the function used it as b’s value.

Currying and Partial application

Some might start to think that the number of nested functions a curried function has depends on the number of arguments it receives. Yes, that makes it a curry.

Currying and Partial Application are related (because of closure), but they are different concepts.

Partial Application transforms a function into another function with smaller arity.

//Currying function
function Sum(x){
    return (y) => {
        return(z) =>  {
        return x + y + z 
        };
    };
};
console.log(Sum(2)(3)(4));
Output : 9
//Partial application 

function Sum(x){
    return (y,z) =>{
        return x + y + z
      };
};
let x = Sum(10)(3,2);
console.log(x);
console.log((x)(3,2));
//we can call it in both way 
Output : 15

Overall

curried functions provide a powerful way to create modular and reusable code by breaking down a function that takes multiple arguments into a sequence of functions that each take a single argument.

Closure makes currying possible in JavaScript. I hope you have learned something new about currying..!!!

Thanks for reading :)