Currying transforms a multi-argument function into one you can call with its arguments spread across several calls: curry(add)(1)(2)(3). This version adds placeholders — a special _ value that holds an argument slot open so you can fill positions out of order, like curry(add)(_, 2, 3)(1). It's how lodash's curry lets you skip an early argument and supply it later.
Implement curry(fn) plus the placeholder _. Collect arguments across calls until there are fn.length of them (with no holes), then invoke fn. A _ marks a position to be filled by a later call's next argument.
function curry(fn) {} // collect args until fn.length, then invoke
const _ = /* placeholder */;
const add = (a, b, c) => a + b + c;
curry(add)(1)(2)(3); // 6
curry(add)(1, 2)(3); // 6 — several args per call
curry(add)(_, 2, 3)(1); // 6 — placeholder fills last: add(1, 2, 3)
curry(add)(1)(_, 3)(2); // 6 — hole in the middle, filled by 2
fn.length — keep collecting until you have that many non-placeholder arguments, then call fn._ — a held slot; the next call's arguments fill placeholders left to right before appending the rest.fn is finally invoked.fn once the arity is satisfied.Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.