Skip to main content
06

Implement curry

Mediumclosuresrecursionfunctional programming

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

Implement curry(func). It returns a curried version of func: a function you can call with the arguments one at a time, or a few at a time. Once it has received as many arguments as func expects, it calls func with all of them and returns the result.

The number of arguments func expects is func.length.

js
const add = (a, b, c) => a + b + c
const curriedAdd = curry(add)

curriedAdd(1)(2)(3) // 6
curriedAdd(1, 2)(3) // 6
curriedAdd(1)(2, 3) // 6
curriedAdd(1, 2, 3) // 6

const addTen = curriedAdd(10)
addTen(1)(2) // 13
addTen(5, 5) // 20

Requirements

  • Keep collecting arguments until there are at least func.length of them, then call func with all of them and return its result.
  • Every call before that returns a new function that waits for the rest.
  • Partial results are reusable: calling addTen twice with different arguments gives two independent results.
  • A function with no parameters is called on the first call.

Approach

The curried function takes whatever arguments it is given and compares the count with func.length:

  • Enough arguments: call func with all of them and return the result.
  • Not enough: return a new function that, when called, joins the arguments collected so far with the new ones and runs the same check again.

That "run the same check again" is recursion: the inner function calls curried with the combined list.

js
const curriedAdd = curry((a, b, c) => a + b + c)

const step1 = curriedAdd(1) // args [1]: 1 < 3, returns a function
const step2 = step1(2) // args [1, 2]: 2 < 3, returns a function
step2(3) // args [1, 2, 3]: 3 >= 3, calls add and returns 6

The reuse trap

A tempting solution pushes into a single shared args array. It passes curriedAdd(1)(2)(3), but breaks as soon as a partial result is reused: addTen(1) and addTen(5) both push into the same array. Build a new array on every call ([...args, ...more]), so each partial function owns its own arguments.

Complexity

Each call copies the arguments collected so far, so collecting n arguments one at a time is O(n²) copying in total. With the small arities that curried functions have, that is negligible.

Follow-ups interviewers ask

  • Placeholders: support curry.placeholder (Lodash uses _), so curriedAdd(_, 2)(1)(3) fills arguments out of order.
  • Infinite currying: sum(1)(2)(3)() that adds until it is called with no arguments, without a fixed arity.
  • Why func.length can lie: it doesn't count rest parameters or parameters with default values, so curry((a, b = 1) => a + b) calls func after one argument.

Reference solution

js
/**
 * @param {Function} func
 * @returns {Function}
 */
export default function curry(func) {
  return function curried(...args) {
    if (args.length >= func.length) return func.apply(this, args)
    return (...more) => curried.apply(this, [...args, ...more])
  }
}
ts
type AnyFunction = (...args: any[]) => any

export default function curry(func: AnyFunction): AnyFunction {
  return function curried(this: unknown, ...args: unknown[]): unknown {
    if (args.length >= func.length) return func.apply(this, args)
    return (...more: unknown[]) => curried.apply(this, [...args, ...more])
  }
}