Full answer
A basic curry collects arguments until their count reaches fn.length, then calls fn. A placeholder, such as lodash's _, lets a caller skip an argument and supply it later: curried(_, 2)(1) is the same as curried(1, 2).
How to implement it. Keep an array of the arguments collected so far. On each call, create a new array: for every earlier slot that is still a placeholder, take the next new argument; after that, append what is left. Then count how many slots are real values. A slot counts as filled only when it is not the placeholder. Also the array length must be at least the arity, because a trailing placeholder is still missing. When all slots are filled, call fn with them. Otherwise return a new curried function that remembers the merged array. Each step returns a new closure, so the intermediate functions can be reused without sharing state: const add1 = curried(1) can be called many times.
Details:
fn.lengthis the number of parameters before the first default value or rest parameter. A function like(a, b = 2) => ...has length 1. Accept an optionalarityargument for these cases.- Use a unique placeholder, a
Symbol, so that no legitimate value equals it. If you export a placeholder, make sure callers import the same one. - Preserve
thisif the function is used as a method, by capturing it with a regular function andapply.
Currying helps partial application and building pipelines. The cost is readability for people who do not know it, and an extra closure per call.
const _ = Symbol('placeholder');
function curry(fn, arity = fn.length) {
function collect(received) {
return function (...args) {
const merged = received.slice();
let next = 0;
for (let i = 0; i < merged.length && next < args.length; i++) {
if (merged[i] === _) merged[i] = args[next++];
}
while (next < args.length) merged.push(args[next++]);
const filled = merged.slice(0, arity).filter((value) => value !== _).length;
if (merged.length >= arity && filled >= arity) return fn.apply(this, merged);
return collect(merged);
};
}
return collect([]);
}
const volume = curry((length, width, height) => `${length} x ${width} x ${height} = ${length * width * height}`);
console.log(volume(2, 3, 4));
console.log(volume(2)(3)(4));
console.log(volume(2, 3)(4));
console.log(volume(_, 3, 4)(2));
console.log(volume(_, _, 4)(2)(3));
console.log(volume(_, 3)(_, 4)(2));
const withTwo = volume(2);
console.log(withTwo(3, 4), withTwo(10, 10));
const greet = curry((greeting, name, punctuation = '!') => `${greeting}, ${name}${punctuation}`, 2);
console.log(greet('Hello')('Asha'));
console.log(((a, b = 1) => 0).length, ((...rest) => 0).length);Run with Node.js v24.22.1 as js-104.js; two runs gave identical output. Output: 2 x 3 x 4 = 24 2 x 3 x 4 = 24 2 x 3 x 4 = 24 2 x 3 x 4 = 24 2 x 3 x 4 = 24 2 x 3 x 4 = 24 2 x 3 x 4 = 24 2 x 10 x 10 = 200 Hello, Asha! 1 0
Key points
- Collect arguments in a closure and call fn when all required slots are filled.
- Fill earlier placeholder slots first, then append new arguments.
- A slot is filled only if it is not the placeholder; trailing placeholders still count as missing.
- fn.length ignores defaults and rest parameters, so allow an explicit arity.
- Each step returns a new closure so partially applied functions are reusable.
Common mistakes
- Relying on fn.length for functions with default or rest parameters.
- Using undefined as the placeholder, so real undefined arguments are skipped.
- Mutating the shared argument array, so partially applied functions affect each other.
- Counting a trailing placeholder as a supplied argument.
Follow-ups
- What is the difference between currying and partial application?
- How would you make a curried function that accepts all remaining arguments at once?
- How does compose or pipe work with curried functions?
Tips
- Explain the fill-from-left rule with one example before coding.
- Mention fn.length limits and a Symbol placeholder.
- Show that partially applied functions are reusable.