`first` retrieves the head of a sequenceable collection. It takes one argument and returns the element at the first position, or `nil` if the collection is empty or the argument is `nil`. It is a small but useful building block for collection processing. section 3.5.1 section 3.5
Read the head
```clojure (first '(1 2 3 4)) ; 1 (first []) ; nil (first nil) ; nil ``` `second` and `last` retrieve elements at their respective positions and likewise return `nil` when that position is unavailable. section 3.5.1
“First” means first in the collection’s sequential view. A map yields key-value pairs, while a set yields elements without a guaranteed ordering; neither should be used with `first` to request a particular member. For an ordered collection, choose the access function that matches both the position needed and the collection type. section 3.5.1
Extract a field from each value
Pass `first` to `map` to take the leading part of each split phone number: ```clojure (->> ["221 610-5007" "661 471-3948" "555 515-0158"] (map #(clojure.string/split % #" ")) (map first) distinct) ;; ("221" "661" "555") ``` Here, `first` selects each area code; `distinct` removes repeats. 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 access across ordered collection types: the available alternatives and their listed costs differ by type. section 3.5.1

