liveBook · Manning

Chunk-buffer

As explained in Clojure: The Essential Reference

ChunkBuffer is a mutable Clojure runtime buffer backed by a Java object array for assembling a chunk.

chunk-buffer creates a mutable intermediate buffer for assembling a chunk during chunked sequential processing. It wraps a Java object array and is intended as an internal artifact, not ordinary application data. section 10.12

Use the buffer through this small contract:

  • Call (chunk-buffer capacity) with a positive integer capacity. section 10.12
  • Add values with (chunk-append b x), then convert the completed buffer with (chunk b). section 10.12
  • Pass the resulting chunk to chunk-cons when constructing a chunked lazy sequence. section 10.12
  • Treat capacity as fixed: appending to a full buffer raises ArrayIndexOutOfBoundsException. section 10.12

Assemble a chunk

(def b (chunk-buffer 10)) (chunk-append b 0) (chunk-append b 1) (chunk-append b 2) (def first-chunk (chunk b)) (chunk-cons first-chunk ()) ;; (0 1 2)

Listing 10.13. → Chunked sequences
(chunk-cons [chunk rest])
(chunk-first [s])
(chunk-rest [s])
(chunk-next [s])

(chunk-buffer [capacity])
(chunk-append [b x])
(chunk [b])
(chunked-seq? [s])

Browse the related chunked-sequence API alongside chunk-buffer, chunk creation, chunk consumption, and chunk detection. section 10.12

How Clojure: The Essential Reference covers it

How deep it goes
Defines construction, appending, conversion, capacity, and overflow behavior, while deferring undocumented implementation details.
Examples
Demonstrates assembling three values, converting them to a chunk, and consuming them through chunk-cons.
What it assumes
Assumes familiarity with sequential collections, lazy sequences, and chunk-cons, while defining the buffer operations directly.
What you can do after
Build chunked sequence transformations by filling buffers, converting them to chunks, and passing them to chunk-cons.
How it connects
Connects chunk-append and chunk with chunk-cons, chunked sequences, lazy sequences, and sequential collections.

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.