liveBook · Manning

Transducer

As explained in Clojure: The Essential Reference

A transducer composition is a combined transducer formed by composing multiple transducers into one transformation.

A transducer composition combines transformations into one reusable transformation. In Clojure, collection-free calls such as (filter odd?) and (map inc) produce transducers; comp joins them. Applied through transduce, the chain processes a collection in a single pass without producing an intermediate result for each step. section 7.0 section 7.2

Compose, then reduce

(transduce (comp (filter odd?) (map inc)) + (range 10)) ;; 30 The chain keeps odd numbers, increments them, then sums them. Compare (reduce + (map inc (filter odd? (range 10)))), which also returns 30. In a transducer chain, write the transformations in the order each input should encounter them. section 7.2.1 section 2.2.2

Choose how to run the composed transformation:

  • Use transduce with a reducing function, an optional initial accumulator, and a collection to produce a final reduction result. section 7.2.1
  • Use sequence or eduction to obtain transformed elements, or into to copy them into another collection. section 7.2
(require '[clojure.string :refer [starts-with? split-lines]])

(defn alaska? [postcode]
  (starts-with? postcode "99"))

(defn unique-postcodes [mailing]
  (sequence (comp
              (map :label)
              (map split-lines)
              (map last)
              (remove alaska?) ; #1
              (distinct))
    mailing))

(unique-postcodes mailing)
;; ("43793" "75395")

A mailing-list pipeline extracts postcodes, removes those starting with 99, and eliminates duplicates. Its sequence call shows how a composed chain can remain separate from the input collection. section 2.2.2

A custom transducer accepts a reducing function and returns a transformed reducing function with step and completion arities. For ordinary composition, collection-free core functions avoid that implementation work. Transducers emphasize reuse; unlike reducers, parallel use is reliable only for stateless transducers. section 7.2.1 section 7.0

How Clojure: The Essential Reference covers it

How deep it goes
Defines collection-free transducer arities and transduce’s reducing-function contract, then shows composition and a custom transducer.
Examples
Uses an odd-number sum, a postcode-cleaning pipeline, and a transducer-based multiplication example.
What it assumes
Assumes familiarity with reduce and function composition while specifying the transducer and reducing-function contracts.
What you can do after
Apply it via Clojure’s comp and transduce to reuse ordered transformations with a reducing function.
How it connects
Connects function composition and reduce with sequence, eduction, into, and reducers.

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.