liveBook · Manning

Partial application

As explained in Clojure: The Essential Reference

Partial application is the technique of fixing some arguments of a function to produce another function that accepts the remaining arguments.

Partial application fixes some arguments of an existing function and returns a new function that waits for the rest. In Clojure, it is useful for turning configuration into reusable computational pieces, especially with sequence functions such as map and filter. section 2.2.8

Use partial application to:

  • Create a configured function: (def incrementer (partial + 1)); (incrementer 1 1) returns 3. section 2.2.8
  • Preserve the remaining arguments and the original function's arity contract; supplying too many arguments raises an ArityException. section 2.2.8
  • Prefer partial over a custom wrapper when emphasizing that an existing function is intentionally receiving arguments later. section 2.2.8

Validate configured data

(map (partial validate #{:id :cache :product}) [valid-req invalid-req]) ;; (true false) Here, partial supplies the whitelist while map supplies each request. section 2.2.8

Listing 2.13. → Higher order functions, currying, deferred invocation
(partial
  ([f])
  ([f arg1])
  ([f arg1 arg2])
  ([f arg1 arg2 arg3])
  ([f arg1 arg2 arg3 & more]))

Inspect partial's supported arities and its role alongside higher-order functions, currying, and deferred invocation. section 2.2.8

How Clojure: The Essential Reference covers it

How deep it goes
Defines deferred arguments, demonstrates arity behavior, and contrasts partial application with explicit currying.
Examples
Uses arithmetic, string searching, string construction, collection predicates, request validation, and sequence mapping.
What it assumes
Assumes familiarity with Clojure functions and higher-order functions while illustrating deferred arguments directly.
What you can do after
Apply partial to build configured functions and compose them with sequence-processing operations.
How it connects
Connects partial application with higher-order functions, currying, deferred invocation, map, and filter.

Also explained in

Compare how 10 books explain partial application →

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.