liveBook · Manning

Higher-order function

As explained in Clojure: The Essential Reference

A higher-order function is a function that can be treated as a value by being stored, passed as an argument, or returned.

A higher-order function accepts a function as an argument or returns one. This works because functions are values: they can be stored, passed around, and returned. In Clojure, `fn` creates such values, making it possible to assemble a computation where it is needed. section 2.1.2 section 2.4.2

Pass a transformation to map

A mapping can pair each original key with a new key and a function. `map` applies an `fn` that destructures each rule and calls its function on the original value; `apply merge` combines the results. For example, a person map can become `{:id "1234567", :name "john doe", :avatar "http://focus.on/me.jpg"}`. Explicit `fn` is useful here because the shorter `#()` syntax does not support destructuring. section 2.1.2

`partial` takes a function and some arguments, then returns a function waiting for the remaining arguments. `(def incrementer (partial + 1))` makes `(incrementer 1 1)` return `3`. This avoids hand-writing nested functions for deferred invocation. Unlike automatic currying, Clojure uses an explicit `partial` call; use `fn` when the required argument arrangement needs a custom wrapper. section 2.2.8

Adapt validation for map

Given `validate`, which checks a request against a whitelist, `(map (partial validate #{:id :cache :product}) [valid-req invalid-req])` returns `(true false)`. The generated function holds the whitelist while `map` supplies each request. The eventual call still has to satisfy the underlying function’s arity. section 2.2.8

`memoize` shows another use: it accepts a function and returns a caching wrapper without changing the original. The wrapper stores results by argument values, so a repeated call can retrieve its result rather than recompute it. This is appropriate when the wrapped function’s result depends only on its arguments; other influences could make a cached result wrong. section 2.4.2

Observe a cached call

For a wrapped two-argument addition function that prints on a cache miss, the first `(f 1 2)` prints `Cache miss for [1 2]` and returns `3`. A second `(f 1 2)` returns `3` without printing; `(f 1 3)` prints a new miss and returns `4`. section 2.4.2

How Clojure: The Essential Reference covers it

How deep it goes
Defines function-as-value behavior and works through partial application and transparent caching; contrasts explicit partial with automatic currying.
Examples
Demonstrates partial request validation and memoized addition with visible cache misses.
What it assumes
Assumes familiarity with Clojure function calls and collections while defining higher-order functions and fn lambdas explicitly.
What you can do after
Apply it via Clojure’s partial and memoize to adapt functions and cache repeat calls without changing the originals.
How it connects
Links fn lambdas and referential transparency to memoization and function wrappers.

Also explained in

Compare how 14 books explain higher-order function →

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.