How do currying and partial application differ from each other?
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).
Different ways to preconfigure arguments
Currying changes a multi-argument function into a chain of unary functions; partial application fixes some arguments without requiring unary steps.
Both techniques create reusable specialized functions, but partial application can leave any practical number of parameters for the later call.
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);console.log(result); // 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);console.log(result); // 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.