Full answer
Currying changes the shape of a function. add(a, b, c) becomes add(a)(b)(c). Each call returns a function that waits for the next argument until all have arrived. The idea comes from functional programming, where small functions with one input are easy to combine.
Partial application is related but different. It fixes a few arguments and returns a function for the rest, whatever count remains. Function.prototype.bind does this, and so does const addTax = (rate) => (price) => price * (1 + rate).
A flexible curry helper reads the function's length, which is the number of declared parameters. The wrapper collects arguments across calls and runs the original function as soon as enough arguments have been gathered. Functions with default or rest parameters have a smaller length, so they need an explicit arity.
Compose and pipe join single-argument functions. compose(f, g)(x) is f(g(x)), running right to left, while pipe(f, g)(x) is g(f(x)), running left to right. Both can be written with reduce. Currying helps because a curried function with the data last, such as filterBy(minimum)(list), can be partly applied and dropped into a pipe.
Currying costs a small amount of call overhead and can make stack traces longer, so use it where it improves reuse.
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 compose = (...fns) => (input) => fns.reduceRight((value, fn) => fn(value), input);
const pipe = (...fns) => (input) => fns.reduce((value, fn) => fn(value), input);
const add3 = curry((a, b, c) => a + b + c);
console.log(add3(1)(2)(3), add3(1, 2)(3), add3(1)(2, 3));
const multiplyBy = curry((factor, value) => factor * value);
const double = multiplyBy(2);
console.log([1, 2, 3].map(double));
const greet = function (greeting, name) {
return `${greeting}, ${name}`;
};
const hello = greet.bind(null, 'Hello'); // partial application with bind
console.log(hello('Ria'));
const filterBy = curry((minimum, list) => list.filter((n) => n >= minimum));
const total = (list) => list.reduce((sum, n) => sum + n, 0);
const passMarkTotal = pipe(filterBy(40), total);
console.log(passMarkTotal([35, 52, 40, 91, 12]));
const exclaim = (text) => text + '!';
const wrap = (text) => `[${text}]`;
console.log(compose(wrap, exclaim)('hi'), pipe(wrap, exclaim)('hi'));
console.log(greet.length, ((a, b = 2) => a + b).length);Run with Node.js v24.22.1 as js-32.js; two runs gave identical output. Output: 6 6 6 [ 2, 4, 6 ] Hello, Ria 183 [hi!] [hi]! 2 1
Key points
- Currying: f(a, b, c) becomes f(a)(b)(c).
- Partial application: fix some arguments now, supply the rest later.
- A curry helper uses fn.length to know when to run.
- compose runs right to left; pipe runs left to right; both use reduce.
- Put the data argument last so functions can be partly applied.
Common mistakes
- Using fn.length with default or rest parameters and curry never running as expected.
- Mixing up compose and pipe order.
- Calling the final curried function with extra arguments and assuming they are ignored; the helper passes them to the original function.
- Putting the data argument first, which makes partial application less useful.
Follow-ups
- How would you write curry for a function with a variable number of arguments?
- How is a partially applied function different from a closure that captures a variable?
- How would you handle async functions in a pipe?
Tips
- Define the difference in one sentence each before writing code.
- Mention the data-last convention; it shows you know why currying is used.
- Explain fn.length and its limits unprompted.