liveBook · Manning

Last

As explained in Clojure: The Essential Reference

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

Returns the final element of a sequenceable collection, or nil when no element is available. It accepts one collection argument, including nil or an empty sequence. section 3.5.1

Use last with collections that implement the sequence interface:

  • Ordered sequences return their final element: (last '(1 2 3 4)) produces 4. section 3.5.1
  • nil and empty sequences return nil rather than raising an exception. section 3.5.1
  • Maps yield key-value pairs and sets yield elements, but unordered collections do not provide a predictable insertion-based final element. section 3.5.1

Basic use

(def numbers '(1 2 3 4)) (last numbers) ;; 4 (last nil) ;; nil section 3.5.1

Listing 3.25. → Sequence processing, Iteration
(first [xs])
(second [xs])
(last [xs])

Browse the related sequence-access signatures for first, second, and last. section 3.5.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, nil behavior, unordered collections, and access costs without detailing implementation internals.
Examples
Uses lists, hash-maps, sets, extracted regular-expression matches, and a sequence of numbers.
What it assumes
Assumes familiarity with Clojure collections and teaches the sequenceable input contract from the function’s behavior.
What you can do after
Apply last to retrieve a collection’s final available element and handle missing elements through nil.
How it connects
Connects sequence access with first, second, sorted-set, maps, sets, and lazy sequences.

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.