liveBook · Manning

Partition-all

As explained in Clojure: The Essential Reference

partition-all is a Clojure function that divides a collection into subsequences of up to a requested size, retaining a final smaller subsequence when necessary.

Use `partition-all` when work needs to be done in batches and the final batch must not be lost. It splits a collection into subsequences of at most `n` items, retaining a smaller final subsequence when items remain. The result is lazy. section 10.6

Keep the remainder

`(partition-all 3 (range 10))` returns `((0 1 2) (3 4 5) (6 7 8) (9))`. By contrast, `partition` omits `(9)` because it produces only complete groups unless given padding. section 10.6

The forms are `(partition-all n coll)`, `(partition-all n step coll)`, and `(partition-all n)`. `step` sets the distance between the starts of successive groups: `(partition-all 3 2 (range 10))` returns `((0 1 2) (2 3 4) (4 5 6) (6 7 8) (8 9))`. The one-argument form returns a transducer for use in a processing pipeline. section 10.6

Use a positive `n` for ordinary batching. A negative `n` returns an empty list; zero can produce an infinite sequence of empty lists. The input collection must support `seq`. Unlike count-based `partition-all`, `partition-by` starts a new group when the value returned by a function changes. section 10.6

Batch parallel work

`(pmap insert-query (partition-all 10 records))` passes groups of records to a query-building function, including any short final group. Grouping smaller tasks can improve `pmap` performance, but benchmark it rather than assuming it will. section 10.6 section 9.1.5

How Clojure: The Essential Reference covers it

How deep it goes
Defines arities, lazy results, step behavior, size edge cases, and the transducer form, then works through sequence and batching examples.
Examples
Shows ranges split into complete and short groups, overlapping groups, and record batches passed to pmap.
What it assumes
Assumes familiarity with Clojure sequence expressions such as range and map while spelling out the partition-all input contract.
What you can do after
Apply it via Clojure to retain remainder batches, choose a step, or group work before pmap.
How it connects
Contrasts with partition and partition-by, and combines with transducers and pmap.

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.