liveBook · Manning

Partition

As explained in Clojure: The Essential Reference

partition is a Clojure function that divides a collection into subsequences according to a requested size, step, and optional padding.

`partition` lazily divides a collection into subsequences of up to `n` items. Its optional `step` controls each subsequence’s starting offset, while optional padding fills a final incomplete subsequence. section 10.6

Fixed-size and overlapping partitions

`(partition 3 (range 10))` returns `((0 1 2) (3 4 5) (6 7 8))`, discarding the incomplete remainder. With a smaller step, `(partition 3 2 (range 10))` returns `((0 1 2) (2 3 4) (4 5 6) (6 7 8))`. Padding preserves the remainder: `(partition 3 3 [:a :b :c] (range 10))` ends with `(9 :a :b)`.

Important edge behavior:

  • Use `partition-all` when the final subsequence may be smaller; use `partition-by` when a function determines split points. section 10.6
  • A negative `n` returns an empty sequence; `n` equal to zero can produce an infinite sequence of empty lists, and `step` zero can repeat partitions indefinitely. section 10.6

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, arities, laziness, padding, offsets, and edge cases, while contrasting related partitioning functions.
Examples
Demonstrates strict, overlapping, padded, lazy, and transducer-based partitions over ranges and records.
What it assumes
Assumes familiarity with Clojure collections and sequences; accepts any collection compatible with `seq`.
What you can do after
Apply it via Clojure to batch collections, overlap windows, or fill incomplete final groups.
How it connects
Connects fixed-size partitioning with lazy sequences, transducers, partition-all, and partition-by.

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.