Curry with placeholders lets you fix arguments at any position, not just left-to-right. curry(fn)(_, b)(a) fills the second argument now and the first later. This tests whether you truly understand argument accumulation — not just the basic recursive pattern.
What is curry with placeholders?
Standard curry(add)(1)(2)(3) only works left-to-right — you must supply arguments in order. Placeholder curry lets you skip positions using a sentinel value (_), filling them in on later calls:
This is how Ramda and similar FP libraries implement curry. The placeholder _ is a sentinel — a unique symbol that marks "fill this slot later."
The mechanism: when called, merge new arguments into existing slots. For each position in the accumulated args, if it's _ and a new arg is available, substitute it. Leftover new args go after the existing ones.
The Problem
"Implement curry(fn) where curry.placeholder (exposed as _) can be used to skip argument positions, filling them on later calls."
Thought Process
The key insight: instead of just counting how many args you have, you need to track which positions are still unfilled (have a _). A call is "complete" when:
- You have at least
fn.lengthaccumulated arguments, AND - None of the first
fn.lengthpositions is a_
On each new call, merge incoming args with accumulated args: scan the accumulated array left-to-right, replacing _ entries with incoming args in order. Any leftover incoming args are appended.
Step 1 — The Placeholder Sentinel
Loading editor...
Step 2 — Merging Arguments
The core operation: given current accumulated args and new incoming args, produce a merged list.
Loading editor...
Step 3 — Full Implementation
Loading editor...
Step 4 — Edge Cases
Loading editor...
Full Solution
Loading editor...
What Interviewers Are Testing
- Sentinel value design — using a
Symbolso_can't accidentally match user data - Merge logic — understanding that args are filled left-to-right: each
_in the accumulated array gets the next incoming arg - Completion check — both conditions must hold:
args.length >= arityAND no_in the firstarityslots - Leftover args — new args that didn't fill any placeholder are appended, not discarded
Complexity
| Time | Space | |
|---|---|---|
| Per partial call | O(N) — N = accumulated arg count | O(N) |
| Final call | O(1) | O(N) |
Interview Tips
- Define
_as a Symbol first — state why: "I need a sentinel that can never accidentally match a real argument. Symbol is perfect for this." - Draw the merge step — write
[_, 2, _] + [1, 3]→[1, 2, 3]on the whiteboard/editor before writing code. The merge logic is the core of the problem. - Separate the completion check from the merge — two distinct concerns. Mixing them makes the code hard to reason about.
- Compare to standard curry — "Standard curry only works left-to-right. Placeholder curry adds positional flexibility at the cost of more complex merge logic."