liveBook · Manning

Chunk

As explained in Clojure: The Essential Reference

chunk is a Clojure function that converts a ChunkBuffer into an immutable chunk implementing IChunk.

chunk converts a ChunkBuffer into an immutable IChunk, completing one batch for a chunked sequence. Chunking lets sequential processing fetch several items together instead of advancing one item at a time, mainly improving performance for suitable data layouts. section 10.12

Use chunk with its buffer-building companions:

  • Create a mutable buffer with chunk-buffer, add values with chunk-append, then call chunk once to obtain the immutable chunk. section 10.12
  • Pass the resulting chunk to chunk-cons with the remaining sequence. section 10.12

Build and consume 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)

How Clojure: The Essential Reference covers it

How deep it goes
Defines conversion and buffer lifecycle, then stops short of implementation internals.
Examples
Demonstrates integer accumulation, chunk consumption, buffer invalidation, and chunked mapping over ranges.
What it assumes
Assumes familiarity with lazy sequences and sequential collections while exposing the buffer workflow explicitly.
What you can do after
Build chunked sequence producers by filling buffers, freezing them with chunk, and combining them with chunk-cons.
How it connects
Connects chunk-buffer, chunk-append, chunk-cons, lazy sequences, and the IChunk abstraction.

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.