Implement curry() with Placeholders

Placeholder curry lets you fix arguments at any position — curry(fn)(_, b)(a) fills b now and a later. Build the Symbol sentinel, the mergeArgs operation, and the completion check.

12 min read
JavaScript
Interview
Implementation
Functional

TABLE OF CONTENTS

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:

  1. You have at least fn.length accumulated arguments, AND
  2. None of the first fn.length positions 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 Symbol so _ 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 >= arity AND no _ in the first arity slots
  • Leftover args — new args that didn't fill any placeholder are appended, not discarded

Complexity

TimeSpace
Per partial callO(N) — N = accumulated arg countO(N)
Final callO(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."

Related Questions


Let's Connect

© 2026 Naveen Karthik // Built with React & MUI