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)
.
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
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
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.