[2011-09-01] dev, javascript, advancedjs, jslang

(Ad, please don’t block)

Currying and partial application are two ways of transforming a function into another function with a generally smaller arity. While they are often confused with each other, they work differently. This post explains the details.
## Currying

Currying takes a function
**JavaScript example.** The following is the uncurried binary function `add()`.
`add` looks as follows.
**The algorithm for currying.** The function `curry` curries a given binary function. It has the signature
`curry` takes a binary function and returns a unary function that returns a unary function. Its JavaScript code is
## Partial application

Partial application takes a function
*bound* to x.
`partApply` takes a binary function and a value and produces a unary function. Its JavaScript code is
**General partial application in JavaScript.** JavaScript has the built-in method `Function.prototype.bind` that works on functions with any arity and can bind an arbitrary amount of parameters. Its invocation has the following syntax.
`func` into a new function whose implicit `this` parameter is `thisValue` and whose initial arguments are always as given. When one invokes the new function, the arguments of such an invocation are appended to what has already been provided via `bind`. MDN has more details.
`thisValue` does not matter for the (non-method) function `add` which is why it is `null` above.
## Currying versus partial application

The difference between the two is:
## Related reading

f: X × Y → Rand turns it into a function

f': X → (Y → R)Instead of calling f with two arguments, we invoke f' with the first argument. The result is a function that we then call with the second argument to produce the result. Thus, if the uncurried f is invoked as

f(3, 5)then the curried f' is invoked as

f'(3)(5)

function add(x, y) { return x + y; }Calling it:

> add(3, 5) 8The curried version of

function addC(x) { return function (y) { return x + y; } }Calling it:

> addC(3)(5) 8

curry: (X × Y → R) → (X → (Y → R))Explanation:

function curry(f) { return function(x) { return function(y) { return f(x, y); } } }

f: X × Y → Rand a fixed value x for the first argument to produce a new function

f': Y → Rf' does the same as f, but only has to fill in the second parameter which is why its arity is one less than the arity of f. One says that the first argument is

**JavaScript example.** Binding the first argument of function `add` to `5` produces the function `plus5`. Compare their definitions to see that we have simply filled in the first argument.

function plus5(y) { return 5 + y; }

**The algorithm for partial application.** The function `partApply` partially applies binary functions. It has the signature

partApply : ((X × Y → R) × X) → (Y → R)Explanation:

function partApply(f, x) { return function(y) { return f(x, y); } }

It turnsfunc.bind(thisValue, [arg1], [arg2], ...)

> var plus5 = add.bind(null, 5) > plus5(10) 15Note that

- Currying
*always*produces nested unary (1-ary) functions. The transformed function is still largely the same as the original. - Partial application produces functions of arbitrary arity. The transformed function is different from the original – it needs less arguments.

- Partial application on Wikipedia [partial source of this post]