All questions

Curry IV

Premium

Curry IV

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.

Signature

function curry(fn) {}  // collect args until fn.length, then invoke
const _ = /* placeholder */;

Examples

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

Notes

  • Arity is fn.length — keep collecting until you have that many non-placeholder arguments, then call fn.
  • Multiple per call — arguments can come one at a time or several at once.
  • Placeholder _ — a held slot; the next call's arguments fill placeholders left to right before appending the rest.
  • Extra arguments — anything beyond the arity is ignored when fn is finally invoked.
  • Return value — a function while incomplete; the result of 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.

Upgrade to Premium