liveBook · Manning

Next

As explained in Clojure: The Essential Reference

next is a Clojure function that returns a sequence containing all but the first element of a collection, returning nil when no elements remain.

next returns the sequence after removing the first element. It accepts any seqable collection or nil, and returns nil when nothing remains. section 10.1

Its practical distinctions are:

  • Unlike rest, next returns nil rather than an empty list at the end. section 10.1
  • Together with first, next supports recursive traversal and nil punning: the remaining sequence itself can serve as the termination test. section 10.1

Recursive traversal

(defn next-loop [coll] (loop [xs (seq coll) results []] (if xs (recur (next xs) (conj results (first xs))) results))) (next-loop (range 10)) ;; [0 1 2 3 4 5 6 7 8 9]

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, edge cases, recursion, laziness, and chunking effects without detailing implementation internals.
Examples
Demonstrates nil and empty inputs, recursive range traversal, lazy recursion, and chunked-sequence consumption.
What it assumes
Assumes basic Clojure collections and recursion while establishing seqable inputs, nil results, and sequence idioms.
What you can do after
Apply it via Clojure sequence functions to advance through inputs and detect when traversal has ended.
How it connects
Connects next with first, rest, lazy sequences, recursion, and nil punning.

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.