chapter nine
A sequence is a Clojure abstract data type. An abstract data type (or ADT) describes the behavior of a data structure without mandating a specific implementation. A sequence is better described in terms of its properties:
- It’s iterated sequentially: you can’t access the nth element without first accessing the nth - 1.
- Works like a stateless cursor: the iteration can only move forward and consumed by the same caller who started the iteration.
- It’s commonly (but not necessarily) lazy: the next element is produced only if that element is requested.
- It’s persistent and immutable: like all other core data structures, sequences cannot be altered once created, but changes are possible in terms of a new sequence based on the previous (with structural sharing).
Clojure makes large use of the sequence abstraction. This is reflected in the amount of functions dedicated to the topic. This chapter is mainly about functions for sequential generation while the next covers sequential processing.
The following diagram shows what can generate or feed sequences in the standard library.
Figure 9.1. The different built-in facilities to create sequences.
There are four big families of sequence generators: