liveBook · Manning

R/cat

As explained in Clojure: The Essential Reference

r/cat is the concatenation function in clojure.core.reducers that combines counted values into a reducible and foldable tree or creates an empty mutable collection.

r/cat combines counted values into a reducible, foldable Cat tree, avoiding repeated construction of temporary persistent collections during parallel folds. It differs from clojure.core/cat, the transducer. With no arguments it returns an empty java.util.ArrayList; with a zero-argument ctor it creates the chosen empty collection; with left and right values it creates a combined node. Inputs may be any counted values, including nil. section 7.1.4

Use the arities according to the fold role:

  • Pass r/cat as fold’s combinef alongside r/append! to accumulate mutable leaves into a Cat tree. section 7.1.4
  • Use r/foldcat for the standard orchestration; its result remains countable, reducible, and foldable. section 7.1.4
  • Supply a ctor such as #(HashSet.) or #(StringBuilder.) when the accumulated collection needs a different mutable type. section 7.1.4

Fold transformed values

(take 5 (r/foldcat (r/map inc (into [] (range 1000))))) ;; (1 2 3 4 5) (def words (r/foldcat (r-word text))) (type words) ;; clojure.core.reducers.Cat

How Clojure: The Essential Reference covers it

How deep it goes
Explains arities, counted inputs, Cat structure, and fold composition, with examples extending to custom mutable collection types.
Examples
Demonstrates transformed sequences, word processing, HashSet accumulation, StringBuilder accumulation, and fold performance comparison.
What it assumes
Assumes familiarity with reducers, fold, reducing functions, counted values, and Java collection construction.
What you can do after
Apply it via clojure.core.reducers/fold or r/foldcat to build efficiently reducible results from parallel reductions.
How it connects
Connects r/cat to clojure.core.reducers/fold, r/foldcat, r/append!, mutable collections, and persistent data structures.

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.