liveBook · Manning

Chunk-first

As explained in Clojure: The Essential Reference

chunk-first is a Clojure function that returns the first chunk of a chunked sequence.

chunk-first returns the first chunk of a chunked sequence. Use it when processing chunked data in groups rather than one element at a time; its input must satisfy the chunked-sequence abstraction. section 10.12

Use chunk-first alongside the chunked-sequence operations:

  • Call (chunk-first s) to obtain an IChunk from s. section 10.12
  • Reduce or transform that chunk, then rebuild the result with chunk-cons and continue with chunk-rest. section 10.12
  • Treat chunk-* functions as low-level implementation-oriented tools; chunking primarily improves sequential-processing performance. section 10.12

Transforming the first chunk

A chunked mapper obtains the first chunk, creates a buffer sized to that chunk, applies f to each element, and emits the rebuilt chunk: (let [cf (chunk-first s) b (chunk-buffer (count cf))] ... (chunk-cons (chunk b) (map-chunked f (chunk-rest s)))). Applying (map-chunked inc (range 10000)) begins with (1 2 3 4 5 6 7 8 9 10). 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 chunked-sequence API, including chunk-cons, 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 input contract and composition with chunk-cons and chunk-rest, but leaves chunk internals at implementation-detail level.
Examples
Uses a chunked mapper over range and a buffer-based example that transforms each chunk before rebuilding the sequence.
What it assumes
Assumes familiarity with Clojure sequences and lazy processing while identifying chunked-sequence inputs explicitly.
What you can do after
Apply chunk-first to build chunk-aware lazy sequence transformations with related chunk-* operations.
How it connects
Connects chunked sequences with chunk-cons, chunk-rest, chunk-next, chunk-buffer, chunk-append, and chunk.

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.