avatarMoon

Summary

This context discusses the differences and similarities between currying and partial application in functional programming, providing examples in JavaScript.

Abstract

In functional programming, currying and partial application are two techniques that often confuse beginners. Currying is a technique where a function returns another function that takes only one parameter at a time. On the other hand, partial application is a function that returns another function that might return another function, but each returned function can take several parameters. The context provides examples in JavaScript to illustrate the differences and similarities between these two techniques.

Opinions

  • Currying and partial application are not the same functions as the original; they are newly returned functions and take fewer parameters.
  • Currying takes only one parameter, unlike partial application which can take more than one.
  • Both currying and partial application allow developers to take advantage of lazy evaluation, making it more efficient in places where it is needed.
  • The names of currying and partial application may confuse developers because they cannot guess what they are for by their names.
  • Understanding the flow of these techniques can help developers understand functional programming better.
  • Currying and partial application can help developers efficiently use lazy evaluation.
  • The context provides resources for further reading on currying and partial application.

Functional Programming: Currying vs. Partial Application

What are they and how do we use them?

Photo by Florian Klauer on Unsplash

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 + y

Then, the function f(x,y) can be represented like this:

f(x,y) = h(x)(y) = x + y

h 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 + y

f(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 + y

h(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); // 12

So, 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 // true

OK, 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.

So, whenever we click #myDiv, its background color will be set as blue with a different hex. We didn’t even have to put the whole string into the code every time it’s needed.

As in the example above, currying or partial application makes lazy evaluation more efficient in places they are needed.

Conclusion

Currying and partial application may make developers confused with their names because they can’t really guess what they are for by their names.

But actually, they are all about how many parameters they will take at a time and a more important thing to remember is that you can efficiently take advantage of lazy evaluation.

You might want to read my another article, talking about pipe in functional programming, and you’d understand this flow as well. Thank you for reading!

const add = x => y => x + y;
const minus = x => y => x - y;
pipe(
 add(5),
 minus(10)
)(10); // -5

Resources

Functional Programming
JavaScript
Coding
Programming
Web Development
Recommended from ReadMedium