liveBook · Manning

Butlast

As explained in Clojure: The Essential Reference

butlast is a Clojure function that returns a sequence containing all but the last element of a collection.

butlast returns a sequence containing every element of a collection except its last element. It accepts sequential, seqable input or nil, and returns nil when no element remains. section 10.1

Removing the final element

(butlast '(0 1 2 3)) ;; (0 1 2) (butlast nil) ;; nil (butlast []) ;; nil

Like the neighboring sequence-processing functions, butlast generates a lazy sequence and composes naturally with sequence transformations. section 10.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, laziness, and empty cases, then demonstrates ordinary, nil, and empty-collection behavior.
Examples
Uses quoted lists, nil, empty vectors, and a composable into* helper to demonstrate results and transformations.
What it assumes
Assumes familiarity with Clojure collections and sequences while defining seqable input and nil behavior.
What you can do after
Apply it via Clojure sequence pipelines when the final input element must be excluded.
How it connects
Connects butlast with sequences, lazy sequences, rest, next, and composable sequence transformations.

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.