You are currently viewing A Good Example to Understand Curry Function in JavaScript

A Good Example to Understand Curry Function in JavaScript

 Curry Function in JavaScript

‌In this article, we will see about Function curry in JavaScript with an example.

What is function currying?

Currying is a process in functional programming in which we transform a function with multiple arguments into a sequence of nesting functions that take one argument at a time. So, if we had a function f(a, b,c) transformed to f(a)(b)(c). It is important to note that curry doesn’t call a function, it simply transforms it.

Ex:

function sum(a,b,c){
return a+b+c;
}
console.log(sum(2,3,5))

output: 10

  1. Here, we have defined a function called sum(), which takes three parameters(a,b,c) and returns sum of three values.
  2. To call or invoke this function, we write the function name followed by parentheses. Within the parentheses, we specify the arguments. So, we pass (2,3,5) as arguments and we got the output 10.

Now currying this sum() function means that we need to transform sum from calling it with all three arguments, to calling it with one argument at a time. So, we transform :

sum(2,3,5) to sum(2)(3)(5)

The way we do that is by the nesting function where each function takes one argument at a time.

Example of Curry function

function sum(a,b,c){
   return a+b+c;
}

function curry(fn){
   return function(a){
      return function(b){
         return function(c){
            return fn(a,b,c)
         }
      }
   }
}

const currySum = curry(sum)
console.log(currySum(2)(3)(5))

Output: 10

Steps:

  1. In the above code, we have created a function called curry and this function accepts a function as its arguments and returns the carried version of the function.
  2. Now, each of the three arguments returns individual functions that accept one argument at a time and the function will be nested one inside the other. So, the curry function will return the function which accepts the argument ‘a’. Then function(a) will return the function which accepts argument (b) and then function(b) returns function(c).
  3. We are transforming from accepting three arguments at a time to one argument at a time.
  4. Now when we have broken down the function into nested functions, we check if we have all the necessary arguments to run the given function. In our case, we have a,b, and c arguments.
  5. In the innermost function, we return the actual passed-in function with all the necessary arguments fn(a,b,c).
  6. Then we have created a constant ‘currySum’ is equal to call the curry function in javascript passing in the sum function. i.e. const currySum = curry(sum)
  7. Finally, we invoked the currySum passing in one argument at a time, i.e currySum (2)(3)(5)
  8. We got the output, still remains at 10.
READ ALSO  What is HTML noscript Tag | Explained

Instead of passing three parenthesis we can break down into three separate function calls, like that:

const add1 = currySum(2);
const add2 = add1(3);
const add3 = add2(5);

console.log(add3)

So, the currySum() function takes the first argument, the add1() function takes the second argument, and the add2() function takes the third argument. Then computes the sum of all three arguments and returns the results.
Finally, we console the add3 variable because it has the three arguments of the currySum() function.
We got the output 10.

Conclusion

We have transformed the function with multiple arguments into a sequence of nesting functions that takes 1 argument at a time. So, currying is used to compose the reusable function. For ex: you can create functions like log info, log error, log now,etc. Where one or more arguments are set and you get to choose the remaining arguments. Curry means composing new functions very easily. If you understand the general concept of carrying, I would recommend practicing this concept of curry.

Leave a Reply