Currying transforms an n-ary function into a chain of unary functions: f(a, b, c) becomes f(a)(b)(c). It's a change of shape, and doesn't supply any values itself. Partial application supplies some arguments now and returns a function expecting all the remaining ones in one call: g = partial(f, a) then g(b, c). Calling a curried function with one argument is a partial application, which is why the terms get mixed up.
const volume = (l, w, h) => l * w * h;
// Currying: reshape into unary steps
const curriedVolume = (l) => (w) => (h) => l * w * h;
curriedVolume(2)(3)(4); // 24
// Partial application: fix some args, take the rest together
const partial = (fn, ...preset) => (...rest) => fn(...preset, ...rest);
const volumeOfLength2 = partial(volume, 2);
volumeOfLength2(3, 4); // 24
| Currying | Partial application | |
|---|---|---|
| What it does | Converts f(a,b,c) to f(a)(b)(c) | Pre-fills some args of f |
| Result arity | Always unary functions, one per argument | Function taking the remaining args at once |
| Number of steps | As many as the arguments | Usually one |
| Needs to know arity? | Yes (to know when to call f) | No |
| JS built-in | None | Function.prototype.bind |
Practical curry helpers in JS are "loose": they accept one or more arguments per call, and invoke the original once enough arguments have arrived.
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) return fn.apply(this, args);
return (...more) => curried.apply(this, [...args, ...more]);
};
}
const cv = curry(volume);
cv(2)(3)(4); // 24
cv(2, 3)(4); // 24
cv(2)(3, 4); // 24
This relies on fn.length, which ignores parameters with defaults and rest parameters:
((a, b = 1) => 0).length; // 1
((...xs) => 0).length; // 0, so auto-curry can't work on variadic functions
bind).const logError = log.bind(null, 'error')). Simple, no special helpers needed.map(fn), filter(pred), propEq(key)(value)), or an API where configuration arrives in stages (Redux middleware: store => next => action).In everyday JavaScript, hand-written curried arrow functions ((field) => (event) => …) are more common and easier to type in TypeScript than generic curry utilities.
this handling: arrow-based helpers drop dynamic this; use function and apply(this, …) if methods matter.f(a)(b)(c)(d) with unnamed intermediates is hard to follow.bind as built-in partial application; implement curry using fn.length.Spotted a mistake or a rights concern? Report an issue