Functional Programming: Currying vs. Partial Application
What are they and how do we use them?
When learning functional programming, a lot of beginners get confused about some concepts of FP. When I was new to FP, I was so confused about currying and partial application.
In this post, I will talk about them with examples.
What Is Currying?
Well, mostly, if people get confused about this, then probably it would be because of the term curry which is the same as the food name from India.
However, in computer science, currying is a terminology of a technique. It has originated from a mathematical function.
Imagine there is a JavaScript function, add.
const add = (x, y) => x + y;So, add takes two arguments — x and y, and returns a summed value. This function, add, can be represented in a mathematical way as follows:
f(x,y) = x + yThen, the function f(x,y) can be represented like this:
f(x,y) = h(x)(y) = x + yh is a translated function that takes a single argument and outputs a function that takes a single argument. With x = 2, then the function f will be like this, below:
f(2,y) = 2 + yf(2,y) still needs to get y to return a summed value. Then, it can be represented as another function that takes y and this is equal to this.
f(2,y) = h(2)(y) = 2 + yh(2)(y) also refers to a function that waits for y since it already knows what x refers to.
Let’s get back to our add function in JavaScript.
If add is f(x,y) which takes two parameters, then how could h(x)(y) be represented, using add? Like h(2)(y) ended up returning another function that takes y, add also should return a function, like so:
// Arrow function
const add = x => y => x + y;// Normal function
function add(x) {
return function(y) {
return x + y;
}
}Then, we can implement this function like this:
const addFive = add(5);addFive(7); // 12So, the currying function is a function that returns another function which takes only one parameter at a time, like add.
What Is Partial Application?
Then, what’s a partial application function?
Just for a second, let’s take a look at currying again. We handled the function add which takes two parameters. What do you think it should be like with three parameters?
const add = (x, y, z) => x + y + z;I explained that currying is a function that returns another function that takes one parameter at a time.
Then, this time, add takes three parameters and it should be disassembled into three different functions that take one parameter.
const add = x => (y, z) => x + y + z;This returns a function but it still takes two parameters at once. Then, the returned function has to be changed again.
// Arrow function
const add = x => y => z => x + y + z;// Normal function
function add(x) {
return function(y) {
return function(z) {
return x + y + z;
}
}
}const addFive = add(5);
const addFourAgain = addFive(4);
const nineTeen = addFourAgain(10);
nineTeen === 19 // trueOK, I thought we were going to talk about partial application but why are we talking about currying again? Because they are very, very similar to each other.
const add = x => (y, z) => x + y + z;So, this wasn’t a currying function since add returned a function that takes two parameters, not one. But this is a partial application function.
…What?
Things in Common and Differences
So, here’s the difference between them.
Currying: A function returning another function that might return another function, but every returned function must take only one parameter at a time.
Partial application: A function returning another function that might return another function, but each returned function can take several parameters.
// Currying function
const add = x => y => z => x + y + z;// Partial Application function
const add = x => (y, z) => x + y + z;What is in common: Both currying and partial application functions are not the same functions as the original. They are newly returned functions and take fewer parameters.
What is not in common: Currying takes only one parameter, unlike partial application which can take more than one.
Why Do I Need Them?
Both of them allow you to take advantage of lazy evaluation. Here’s a simple example.






