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, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.