Function Currying Explained

The most simple explanation

Function Currying Explained

What is Currying?

Ever got this question in an interview? This is one of the important topics that a developer should know.

Currying is a technique to transform a function with multiple arguments, into a sequence of nesting functions, breaking down the arguments to each nested function.

That is, instead of taking all the arguments at once, it takes the first argument and returns a new function which takes the second argument and returns another function which in turn takes the third argument, and so and so until all the arguments are exhausted.

Hmm, ok now how does a currying function looks like?

function funcName(a) {
    return function (b) {
        return function (c) {
            return a * b * c
        }
    }
}

And why should I use currying?

  • One advantage is when you need to call the same function with some of the parameters a lot of time in your code.
  • It helps to remove repetition. DRY remember?

We will get more clarity when we jump to the examples. So let's go.

Let's look at an example of how we normally find the volume of a box

function volume ( length, breadth, height ) {
  return length * breadth * height
}
volume(3,3,2)

That was an easy-peasy code :)

Now say we want to find the volume of another box that has a different height.
To get this we may have to run the whole function again by passing the new height.
What if we have a lot of boxes with just different heights, but the same length and width?

Calling the same whole function, again and again, bad idea.

Also what if calculating the length and breadth was a tedious task, or think of any other big task that involves complex calculations? It would affect the performance!

This is where currying comes in.
Now lets look at the same code using the currying technique

function volume ( length, breadth ) {
  const areaValue = length * breadth 

  return function ( height ) {
        return areaValue * height
  } 
}
const area =  volume(3,3); 
const putHeight = area(2)

Yup, the code looks bigger

See how instead of passing all the arguments at once, we used a function inside another function to get our value?

Now let's take the same problem we discussed earlier. To find the volume of all boxes that have different heights but the same length and breadth.
We can get the volume of another box of height say 5, by just defining a variable:
const nextHeight = area(5)

What does this variable do now? We don't have to run the whole function again and again to get the volume of the new box every time.
The variable area in our code already has the value of length x breadth pre-populated with it.

So this means we only multiply the new height to the variable areaValue which already has the length x breadth value.
Consider complex calculations and how this technique improves performance!

That's it for now :)
Let's connect on twitter

Did you find this article valuable?

Support Mansoor Ameen by becoming a sponsor. Any amount is appreciated!