liveBook · Manning

Chunk-rest

As explained in Clojure: The Essential Reference

chunk-rest is a Clojure function that returns the remainder of a chunked sequence after removing its first chunk, or an empty list when none remains.

chunk-rest returns the remainder of a chunked sequence after removing its first chunk. It parallels rest for lazy-sequence processing and returns an empty list when no chunk remains. section 10.12

Use chunk-rest when processing chunked sequences:

  • Pass it a chunked sequence, where chunk-seq? returns true. section 10.12
  • Pair chunk-first with chunk-rest to transform the current chunk and recursively continue with later chunks. section 10.12
  • Treat chunk-* functions as low-level tools for chunked processing rather than general-purpose sequence operations. section 10.12

Continuing a chunked map

A chunked mapping function processes the first chunk, then calls (chunk-rest s) in its recursive step: (chunk-cons (chunk b) (map-chunked f (chunk-rest s))). Taking ten results from (map-chunked inc (range 10000)) produces (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 operations, including chunk-rest alongside chunk-cons, chunk-first, and chunk-next. section 10.12

How Clojure: The Essential Reference covers it

How deep it goes
Defines the sequence role and input contract, then demonstrates recursive use without detailing internal implementation.
Examples
Uses a chunked map implementation over range to show recursive continuation with chunk-rest.
What it assumes
Assumes familiarity with sequential collections, lazy sequences, and the distinction between chunks and chunked sequences.
What you can do after
Apply it to implement chunk-aware recursive sequence processing.
How it connects
Connects chunk-first, chunk-next, chunk-cons, lazy sequences, and chunked sequences.

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.