liveBook · Manning

Currying

As explained in Functional Programming in JavaScript

A function-transformation technique that converts a function taking multiple arguments into a sequence of functions each taking fewer arguments, often unary functions.

Currying turns a function that needs several arguments into a sequence of functions that each accept one argument. Instead of running immediately with missing values, a curried function keeps returning a function until it has everything needed to produce a result. This makes it possible to supply configuration now and the changing value later. section 4.3

Figure 4.6. Evaluating a curried function f. The function produces a concrete result only when all arguments have been provided; otherwise, it returns another function that waits for these parameters to be passed in.

A curried call yields another waiting function until every required argument has been supplied; only then does it yield a concrete value. section 4.3

This is useful when a multi-argument function prevents easy composition. By fixing some arguments first, currying reduces the remaining function to a unary function, which can fit into a pipeline. It also creates small reusable function templates instead of repeating the same setup at every call site. section 1.2 section 4.3

Build a two-argument currier

A regular function receives both values at once: `fullname(first, last)`. A curried version receives `first`, returns a function, and that returned function receives `last`: `function curry2(fn) { return function(firstArg) { return function(secondArg) { return fn(firstArg, secondArg); }; }; }` Calling `name('Curry')('Haskell')` supplies the values in steps. Calling only `name('Curry')` produces a function that has remembered the first value through a closure. section 4.3

The remembered first argument is what makes currying practical. A type checker can first capture the expected type—`checkType(String)`—and then be reused against many values. Likewise, a student lookup can capture a database or array first and leave an SSN as the final input, so the caller uses one consistent `findStudent(ssn)` function regardless of storage choice. section 4.3 section 4.3

Currying helps when you want to:

  • Preconfigure stable dependencies such as a database, logger appender, layout, or logger name. section 4.3
  • Turn a function with several parameters into a unary function that can participate in `run` or another pipeline. section 1.2
  • Separate data lookup and DOM updates into smaller pieces, isolating impure work from the main flow. section 1.2

Currying adds flexibility, but it is not free. Each staged call is a nested function execution and uses additional function-context stack frames. In a heavily repeated calculation, the extra overhead can be substantial enough to halt a browser, so use currying for modularity and reuse rather than indiscriminately in performance-sensitive inner loops. section 7.1 section 7.1

In JavaScript, currying must be supplied explicitly, either with a small helper such as `curry2` or with `R.curry`. The payoff is a function that can be configured in stages, reused in several places, and composed more easily with other single-input functions. section 4.3 section 4.2

How Functional Programming in JavaScript covers it

How deep it goes
Defines deferred curried evaluation, builds a two-argument helper, and examines its execution-context stack overhead.
Examples
Demonstrates type checking, student lookup, configurable logging, DOM output, and a nested-loop addition benchmark.
What it assumes
Assumes familiarity with JavaScript functions and arguments, then introduces currying through simple nested functions.
What you can do after
Apply it via R.curry or a manual helper to create reusable, partially configured JavaScript functions.
How it connects
Connects currying with closures, partial application, function composition, pipelines, side effects, and execution contexts.

Also explained in

Compare how 7 books explain currying →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.