Refactor a Function to Use Currying in JavaScript

Share this video with your friends

Send Tweet

This lesson teaches you what currying is: the act of refactoring a function so that it receives its arguments one at a time. We will use the canonical example of an add function to demonstrate this. This lesson also teaches the concept of “arity”, the number of arguments a function receives, as it is useful knowledge for functional programming.

Tony Catalfo
Tony Catalfo
~ 6 years ago

Would we use add2 like this add2(3)(4)?

Tony Catalfo
Tony Catalfo
~ 6 years ago

Would we use add2 like this add2(3)(4)?

Kyle Shevlin
Kyle Shevlin(instructor)
~ 6 years ago

Hi Tony,

That is correct. If you had both arguments to pass in at the same time, you would need to pass them that way. In your particular example, add2(3) returns a function. This is a partially applied function that has the argument 3 stored in closure. Because this is a function, we need to call it with the second argument, 4, in order to invoke it and have the function return the summation. To break it down into steps, it looks like this:

const add = x => y => x + y
const addWithThree = add(3) // Returns a function that looks like `y => 3 + y`
console.log(addWithThree(4)) // 7, because 4 => 3 + 4
console.log(addWithThree(10)) // 13, because 10 => 3 + 10
Carlos
Carlos
~ 5 years ago

"4 = a qaternary, so fourth and so on." I see what you did there.

Maciej Kołodziejczak
Maciej Kołodziejczak
~ 5 years ago

Can you provide any use cases this may be actually useful? Add function isn't a good one or I just don't see it.

Kyle Shevlin
Kyle Shevlin(instructor)
~ 5 years ago

Maciej, please watch the next lesson on partial application. You're correct that add doesn't seem like a powerful example, but it's the canonical way of teaching the concept. The goal of this lesson is to simply understand that you don't need to receive all your arguments at once, but can receive them in series. This will open up new possibilities such as partial application and functional composition that you cannot get without currying.

Let's take for example writing a map function that takes a function to call for each iteration and an array of items.

const map = callback => array => array.map(callback)

Now, because we've curried that function, we can accomplish something we otherwise would not be able to do. We can supply the first argument, callback and get back a function that can be used on any array.

const getSquares = map(number => number * number)

const arr1 = [1, 2, 3]
const arr2 = [4, 5, 6]

console.log(getSquares(arr1)) // [1, 4, 9]
console.log(getSquares(arr2)) // [16, 25, 36]

Already we gain the ability to pipe data into a function and get output out, and we never have to declare the callback again. There are many applications of currying, but finishing the rest of the course should help demonstrate some of their benefits.