Everything Is a Sequence

Every aggregate data structure in Clojure can be viewed as a sequence. A sequence has three core capabilities:

  • You can get the first item in a sequence:

     (first aseq)

    first returns nil if its argument is empty or nil.

  • You can get everything after the first item—the rest of a sequence:

     (rest aseq)

    rest returns an empty seq (not nil) if there are no more items.

  • You can construct a new sequence by adding an item to the front of an existing sequence. This is called consing:

     (cons elem aseq)

Under the hood, these three capabilities are declared in the Java interface clojure.lang.ISeq. (Keep this in mind when reading about Clojure, because the name ISeq is often used interchangeably with seq.)

The seq function ...

Get Programming Clojure, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.