Difficulty: Intermediate
What is currying in JavaScript, how is it different from partial application, and how do you combine small functions using compose and pipe?
Currying transforms a function that takes several arguments, f(a, b, c), into a chain of functions that each take one argument, f(a)(b)(c). Partial application is related but different: it fixes some arguments of a function and returns a function that takes the rest, in whatever grouping. Function composition builds a new function by feeding the output of one function into the next, usually with pipe (left to right) or compose (right to left).
| Aspect | Currying | Partial application |
|---|---|---|
| Shape | Always unary steps: f(a)(b)(c) | Fix any number now, rest later: g(c) |
| Result | Sequence of single-argument functions | One function with fewer parameters |
| Built-in support | None, write a helper | fn.bind(null, a, b) |
| Typical helper | curry(fn) using fn.length | partial(fn, ...fixed) |
In practice, most "curry" helpers in libraries like Lodash and Ramda are auto-curried: they accept arguments one at a time or several at once (add(1)(2)(3), add(1, 2)(3)), so they blur the two ideas. Strictly speaking, that is a mix of currying and partial application.
A curry helper uses fn.length, the number of declared parameters, as the target arity:
Note that fn.length does not count rest parameters or parameters after the first one with a default value, so curry does not work on (...args) => {} or (a, b = 1) => {} unless you pass the arity explicitly.
const withGst = addTax(0.18) creates a GST calculator you can pass to map.pipe(trim, toLowerCase, slugify) reads like a recipe and each step is easy to unit test.filterBy('city')('Pune') fit naturally into array.filter.store => next => action => {}, and Redux's compose combines store enhancers. Event handler factories in React (handleChange = field => e => ...) are curried functions too.Both are one-liners with reduce:
compose(f, g, h)(x) equals f(g(h(x))), mathematical order, right to left.pipe(f, g, h)(x) equals h(g(f(x))), reading order, left to right.Each function in the chain should take a single argument (which is where currying helps) and return the value the next one expects. There is a TC39 pipeline operator proposal (|>), but it is still Stage 2 and not usable without a transpiler.
tap(console.log) helper in the pipe makes it easier.function curry(fn, arity = fn.length) {
return function curried(...args) {
if (args.length >= arity) return fn.apply(this, args);
return (...more) => curried.apply(this, [...args, ...more]);
};
}
const priceWithTax = (rate, discount, price) =>
Math.round((price - discount) * (1 + rate));
const curried = curry(priceWithTax);
const withGst = curried(0.18); // fix the tax rate
const withGstNoDiscount = withGst(0); // fix the discount
console.log(curried(0.18)(0)(1000));
console.log(curried(0.18, 100)(1000)); // auto-curried: several at once
console.log([200, 500].map(withGstNoDiscount));
// Partial application with bind: fix the first two args at once
const festiveSale = priceWithTax.bind(null, 0.18, 200);
console.log(festiveSale(1000));
The curry helper keeps collecting arguments until it has as many as fn.length, then calls the original. bind performs partial application directly, fixing two arguments in one step.
const pipe = (...fns) => input => fns.reduce((acc, fn) => fn(acc), input);
const compose = (...fns) => input => fns.reduceRight((acc, fn) => fn(acc), input);
const trim = s => s.trim();
const lower = s => s.toLowerCase();
const slugify = s => s.replace(/\s+/g, '-');
const tap = label => v => (console.log(label, v), v);
const toSlug = pipe(trim, lower, tap('after lower:'), slugify);
console.log(toSlug(' Masala Dosa Combo '));
const toSlug2 = compose(slugify, lower, trim); // same result, reversed order
console.log(toSlug2(' Paneer Tikka '));
pipe applies functions left to right and compose right to left; both are a single reduce. A tap helper lets you log intermediate values without breaking the chain.
Currying, Partial Application, Function Composition, pipe, compose, Arity