Skip to the solution
MediumDSAAsked in 20246 min read

Implement curry(fn) That Supports Any Call Pattern — curry(1)(2)(3), curry(1,2,3), or curry(1,2)(3)

4.1k views

The problem

Problem, as an interviewer would state it: "Implement curry(fn) — returning a version of fn that can genuinely be called with arguments split across ANY number of separate calls, only actually invoking the original function once ENOUGH total arguments have been collected."

Examples:

Code
const curried = curry((a,b,c) => a+b+c);
curried(1)(2)(3); // 6
curried(1,2,3);   // 6
curried(1,2)(3);  // 6

Clarifying questions expected:

  • How does this determine "enough" arguments have been collected — via the function's own declared arity (fn.length)?
  • Does a partially-applied intermediate function need to be genuinely REUSABLE for multiple different later completions?
  • Should this work for a function of ANY arity, not just a fixed number of parameters?

Code / implementation expected: Yes — real, direct proof that all real call-pattern variations (fully split, fully combined, and every mix in between) produce the identical result, plus confirmation a partial application is genuinely reusable.

  • js-coding
  • utility
  • algorithm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: curry() correctly handles all four real call-pattern variations, producing identical results, with genuinely reusable partial applications

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 45 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Implement objectMap(obj, fn) — Transform Every Value via Object.entries/fromEntries

Continue