liveBook · Manning

First element

As explained in Clojure: The Essential Reference

first is a Clojure function that returns the first element of a sequenceable collection or nil when none is available.

`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

PersistentList

'(1 2 3)

first, O(1)

last, O(n)

PersistentVector

[1 2 3]

nth/get, O(1)

peek, O(1)

PersistentQueue

(import 'clojure.lang.PersistentQueue) (PersistentQueue/EMPTY)

peek, O(1)

last, O(n)

PersistentTreeSet

(sorted-set 1 2 3)

first, O(1)

(first (rseq s)), O(log n)

PersistentTreeMap

(sorted-map :a 1 :b 2)

first, O(1)

(first (rseq s)), O(1)

PersistentArrayMap

(array-map :a 1 :b 2)

first, O(1)

last, O(n)

LongRange

(range 10)

first, O(1)

last, O(n)

LazySeq

(for [i [1 2]] i)

first, O(1)

last, O(n)

String

"abcd"

(.charAt "abcd" 0), O(1)

(.length "abcd"), O(1)

Compare head and tail access across ordered collection types: the available alternatives and their listed costs differ by type. section 3.5.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines the one-argument contract and nil edge cases, then illustrates collection-dependent ordering and access alternatives without implementing the function.
Examples
Uses a list lookup, area-code extraction from phone numbers, and map and set lookups to show differing sequential views.
What it assumes
Assumes basic Clojure function calls and collections while defining sequenceable inputs and nil results.
What you can do after
Apply it via Clojure’s standard library to retrieve a sequence head or extract leading fields with map.
How it connects
Connects sequenceable collections with map, second, last, distinct, and ordered versus unordered collections.

Also explained in

Compare how 2 books explain first element →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.