liveBook · Manning

Chunk-cons

As explained in Clojure: The Essential Reference

chunk-cons is a Clojure function that constructs a chunked sequence by adding a chunk to a sequential remainder.

chunk-cons constructs a chunked sequence by combining an IChunk with a sequential remainder. It serves as a near drop-in replacement for cons when building lazy, chunked sequences, helping other data sources benefit from chunked processing. section 10.12

Constructing a chunked sequence

(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) section 10.12

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 chunk-* operations, including chunk-first, chunk-rest, chunk-next, chunk-buffer, chunk-append, chunk, and chunked-seq?. section 10.12

How Clojure: The Essential Reference covers it

How deep it goes
Defines the chunk and sequential-remainder inputs, then demonstrates construction without detailing the underlying implementation.
Examples
Uses a three-element chunk and a mapped lazy sequence to demonstrate chunk-cons in practice.
What it assumes
Assumes familiarity with Clojure sequences, lazy sequences, chunks, and chunk buffers.
What you can do after
Build chunked lazy sequences from generated chunks and sequential remainders.
How it connects
Connects chunked sequences with lazy sequences, cons construction, chunks, chunk buffers, and sequence traversal functions.

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.