Implement currying function

Beyond the basic curry(fn) (collect fn.length arguments), interviewers often ask for two harder variants: an infinitely curried sum that ends when called with no arguments, and Lodash-style currying with placeholders that let you skip arguments and fill them later.

Variant 1: infinite currying

sum(1)(2)(3)() → 6. Each call returns a new function capturing the running total; a call with no arguments returns the result. A twist: make the function usable directly as a number via valueOf/Symbol.toPrimitive, so +sum(1)(2) works without the final ().

Variant 2: placeholders

With a placeholder symbol _, curried(_, 2)(1) fills position 0 later. Algorithm:

  1. Keep an array of collected arguments.
  2. Each new call fills existing placeholders first (left to right), then appends.
  3. Call fn once the first fn.length positions contain real values (no placeholders).

Points to mention

  • Preserve this with apply.
  • fn.length ignores defaults and rest parameters, so accept an explicit arity.
  • Currying adds call overhead and can hurt readability; it shines for configuration-first helpers and point-free pipelines.
  • Related: partial application with bind, and composition helpers (pipe), which pair naturally with curried functions.

Code example

js
1// Variant 1: infinite currying, finished by an empty call (or used as a number)
2function sum(...initial) {
3 let total = initial.reduce((a, b) => a + b, 0);
4 const next = (...more) => {
5 if (more.length === 0) return total;
6 total += more.reduce((a, b) => a + b, 0);
7 return next;
8 };
9 next[Symbol.toPrimitive] = () => total; // allows +sum(1)(2)
10 return next;
11}
12console.log(sum(1)(2)(3)()); // 6
13console.log(sum(1, 2)(3, 4)(5)()); // 15
14console.log(+sum(10)(20)); // 30
15
16// Variant 2: currying with placeholders (Lodash-style)
17const _ = Symbol("placeholder");
18function curryWithPlaceholders(fn, arity = fn.length) {
19 return function curried(...args) {
20 const filled = (collected, incoming) => {
21 const out = [...collected];
22 const rest = [...incoming];
23 for (let i = 0; i < out.length && rest.length; i++) if (out[i] === _) out[i] = rest.shift();
24 return [...out, ...rest];
25 };
26 const ready = (collected) => collected.length >= arity && collected.slice(0, arity).every((a) => a !== _);
27 const run = (collected) => (ready(collected) ? fn.apply(this, collected) : (...more) => run(filled(collected, more)));
28 return run(filled([], args));
29 };
30}
31
32const formatDate = curryWithPlaceholders((year, month, day) => `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`);
33const firstOfMonth = formatDate(_, _, 1); // fix the day, fill year and month later
34console.log(firstOfMonth(2026)(3)); // 2026-03-01
35console.log(formatDate(2026, _, 15)(12)); // 2026-12-15
36console.log(formatDate(2026)(7)(4)); // 2026-07-04

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app