9 Sequences

 

A Clojure sequence is an abstract data type. An abstract data type (or ADT) describes the behavior of a data structure without mandating a specific implementation. The following are the main properties of the abstraction:

  • As the name implies it’s iterated sequentially: you cannot access the nth element without first accessing the nth -1 (and there are no gaps).
  • It works like a stateless cursor: the iteration can only move forward.
  • 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).

Optionally, sequences also support the following features (although they are not part of the contract):

  • They are commonly (but not necessarily) lazy: the next element is produced only if that element is requested.
  • They are also cached: the first access to the sequence elements produces a cached version of each item. Subsequent access to the sequence does not require further computation.
  • They often apply "chunking" to improve performance. Chunking consists of processing a few more elements than requested, assuming the caller will soon move forward and access the rest of the sequence.

9.1  Sequential Collections

9.1.1  seq and sequence

9.1.2  rseq

9.1.3  subseq and rsubseq

9.1.4  seque

9.1.5  pmap, pcalls and pvalues

9.2  Abstract Generators

9.2.1  repeatedly

9.2.2  iterate

9.2.3  repeat and cycle

9.3  Other Generators

9.3.1  lazy-seq

9.3.2  tree-seq

9.3.3  file-seq

9.3.4  xml-seq

9.3.5  re-seq

9.3.6  line-seq

9.3.7  resultset-seq

9.3.8  iterator-seq and enumeration-seq

9.3.9  concat and lazy-cat

9.4  Lists

sitemap