liveBook · Manning

Currying

As explained in Clojure: The Essential Reference

Currying is the transformation of a function of multiple arguments into a chain of functions that each accept a single argument.

Currying transforms a function with multiple arguments into nested functions that each accept one argument. Each call supplies one value and returns the next function, allowing deferred invocation until every value is available. section 2.2.8

Nested single-argument functions

A four-argument addition can be expressed directly or through progressively nested functions: `(f1 1 2 3 4)`, `((f2 1 2 3) 4)`, `(((f3 1 2) 3) 4)`, and `((((f4 1) 2) 3) 4)` all produce the same result. Clojure usually favors partial application instead, avoiding this nesting while still deferring trailing arguments. section 2.2.8

Distinguish currying from Clojure’s usual alternative:

  • Currying returns another one-argument function after each invocation. section 2.2.8
  • Partial application fixes supplied arguments, permits remaining arguments together, and works especially well when composing sequence operations such as map and filter. section 2.2.8

How Clojure: The Essential Reference covers it

How deep it goes
Defines nested invocation, contrasts partial application, and stops short of compiler-level currying implementation.
Examples
Uses equivalent four-argument addition functions with progressively nested single-argument returns.
What it assumes
Assumes familiarity with functions and higher-order functions while deriving the transformation from ordinary multi-argument functions.
What you can do after
Apply currying by expressing multi-argument calls as nested single-argument functions, or choose partial for idiomatic Clojure composition.
How it connects
Connects currying with partial application, higher-order functions, function composition, and deferred invocation.

Also explained in

Compare how 7 books explain currying →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.