liveBook · Manning

Unreduced

As explained in Clojure: The Essential Reference

unreduced is a Clojure function that returns the underlying value of a Reduced object or the input value when it is not reduced.

unreduced retrieves the value inside a Reduced object. When given an ordinary value, it returns that value unchanged, making it safe for code that may receive either form. section 7.3

Use unreduced when processing reduction results or values that may carry an early-termination marker:

  • A Reduced object can be inspected with @r, (deref r), or (unreduced r); all three retrieve its payload. section 7.3
  • Unlike reduced, unreduced does not add a wrapper; unlike ensure-reduced, it removes one when present. section 7.3

Extracting a reduced value

(def r (reduced "string")) (unreduced r) ;; "string" (unreduced 42) ;; 42 section 7.3

How Clojure: The Essential Reference covers it

How deep it goes
Defines input and output behavior, contrasts related functions, and demonstrates dereferencing without covering implementation internals.
Examples
Uses a reduced string, ordinary numeric input, and a moving-average reducer receiving a reduced value.
What it assumes
Assumes familiarity with Clojure reduction and dereferencing while defining Reduced markers and their inspection from the basics.
What you can do after
Apply unreduced to normalize possibly reduced inputs before using their underlying values.
How it connects
Connects Reduced, reduce, reducers, transducers, dereferencing, reduced?, reduced, and ensure-reduced.

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.