A sequenceable collection can provide a view of its elements for sequential access. In Clojure, collections implementing `clojure.lang.Seqable` support this behavior: call `seq` directly, or pass the collection to a function that uses `seq` internally. A collection need not be a list to be processed as a sequence. section 9.1 section 9.0
Access elements through a sequence
Given `(def numbers '(1 2 3 4))`, `(first numbers)` returns `1`, `(second numbers)` returns `2`, and `(last numbers)` returns `4`. Each function also accepts `nil` or an empty collection; it returns `nil` when the requested element is unavailable. section 3.5.1
The collection determines what its sequence view means:
- A map yields key-value pairs. A hash map does not promise insertion order, so its “first” pair should not be treated as the first pair inserted. section 3.5.1
- A set also has no inherent ordering. Use a sorted collection when its traversal order matters. section 3.5.1
- `rseq` offers reverse access, while `subseq` and `rsubseq` select portions of sorted collections; these are alternatives to a forward `seq` view. section 9.1
Process more than one collection
`(map str (range 10) ["a" "b" "c"] "hijklm")` produces `("0ah" "1bi" "2cj")`. `map` accepts sequenceable inputs, returns a lazy sequence, and stops when its shortest input ends. section 3.5.2
For step-by-step traversal, `first` reads the head and `rest` or `next` advances. At the end, `rest` returns an empty list whereas `next` returns `nil`, making `next` useful as a loop condition. On a lazy input, choosing between them can also change how much work is forced. section 10.1 section 3.5.1
Table 3.7. Alternative ways to access the head or the tail for ordered collection types.
| Type |
Example |
head |
tail |
|
|
|
first, |
last, |
|
|
|
nth/get, |
peek, |
|
|
|
peek, |
last, |
|
|
|
first, |
|
|
|
|
first, |
|
|
|
|
first, |
last, |
|
|
|
first, |
last, |
|
|
|
first, |
last, |
|
|
"abcd" |
|
|
Compare head and tail operations by collection type. A shared sequence interface does not make every access equally efficient: for example, `last` is O(n) on a persistent list, while a vector has an O(1) `peek` operation. section 3.5.1
