First element
A first operation retrieves the initial element of a sequence or collection, optionally subject to a predicate, and indicates when no matching element exists.
Clojure: The Essential Reference
first is a Clojure function that returns the first element of a sequenceable collection or nil when none is available.
- How it is used
- Selects a collection’s head during sequential processing, including transformations that extract one field from each item.
- 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 you can do after
- Apply it via Clojure’s standard library to retrieve a sequence head or extract leading fields with map.
Read how Clojure: The Essential Reference explains first element →
The Well-Grounded Rubyist, Fourth Edition
first is an Enumerable method that returns the first element or a specified number of initial elements from an enumerable object.
- How it is used
- Retrieves the beginning of arrays, ranges, hashes, and other Enumerable objects during collection querying.
- How deep it goes
- Explains return shapes, hash iteration, infinite enumerables, and lazy evaluation, but does not exhaustively catalog Enumerable methods.
- Examples
- Uses arrays, ranges, hashes, an infinite die iterator, and multiples of three selected from an infinite range.
- What you can do after
- Apply it via Ruby's Enumerable API to obtain one item or a finite initial slice from an iterable, including lazy infinite sequences.
Read how The Well-Grounded Rubyist, Fourth Edition explains first element →
Also covered in
Related concepts
- enumerable protocolpart of · An enumerable protocol is an interface or abstraction that exposes elements or generated values for sequential traversal and generic processing.
- lastcontrasts with · Last is an operation that returns or emits the final element encountered in a finite ordered collection, iterable, stream, or sequence.
- query methodkind of · A query method is a method that defines, constructs, refines, or evaluates a database query.
- terminal operationkind of · A terminal operation ends a stream or sequence pipeline, triggers evaluation of preceding transformations, and produces a final result or side effect.
- each_entryuses · each_entry is a Ruby enumeration method that yields each element of a collection to a block.
- collectionpart of · A collection is a container or abstraction that groups multiple elements for storage, access, traversal, or processing.
Questions about first element
What is first element?
A first operation retrieves the initial element of a sequence or collection, optionally subject to a predicate, and indicates when no matching element exists.
Which Manning books explain first element?
Clojure: The Essential Reference and The Well-Grounded Rubyist, Fourth Edition.
What is the difference between first element and last?
A first operation retrieves the initial element of a sequence or collection, optionally subject to a predicate, and indicates when no matching element exists. Last is an operation that returns or emits the final element encountered in a finite ordered collection, iterable, stream, or sequence.
What is first element a kind of?
First element is a kind of query method. A query method is a method that defines, constructs, refines, or evaluates a database query.
What is first element part of?
First element is part of enumerable protocol. An enumerable protocol is an interface or abstraction that exposes elements or generated values for sequential traversal and generic processing.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

