Enjoy 20% off all plans by following us on social media. Check out other promotions!
Quiz Questions

How do currying and partial application differ from each other?

Topics
JAVASCRIPT
Edit on GitHub

TL;DR

Currying transforms a function with multiple arguments into a sequence of functions, each taking a single argument. For example, a function f(a, b, c) becomes f(a)(b)(c). Partial application, on the other hand, fixes a few arguments of a function and produces another function with a smaller number of arguments. For example, if you partially apply f(a, b, c) with a, you get a new function f'(b, c).


Currying vs partial application

Currying

Currying is a technique where a function with multiple arguments is transformed into a sequence of functions, each taking a single argument. This allows for more flexible function composition and reuse.

Example

function add(a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
}
const result = add(1)(2)(3); // 6

In this example, add is a curried function that takes three arguments one at a time.

Partial application

Partial application is a technique where you fix a few arguments of a function, producing another function with a smaller number of arguments. This is useful for creating specialized functions from more general ones.

Example

function add(a, b, c) {
return a + b + c;
}
const addOne = add.bind(null, 1);
const result = addOne(2, 3); // 6

In this example, addOne is a partially applied function that fixes the first argument of add to 1.

Key differences

  • Currying: Transforms a function with multiple arguments into a sequence of functions, each taking a single argument.
  • Partial application: Fixes a few arguments of a function and produces another function with a smaller number of arguments.

Further reading

Edit on GitHub