Home / Docs / Standard library / Prelude
first
Release v0.4.0Edition 0.1 and later
A function of the prelude, in scope in every module. Edition 0.1 and later.
una
pub first[T](xs: List[T]) -> Option[T]
ensures (result is None) == (xs.len == 0)Some of the element at position 0, and None exactly when xs is empty (spec/14 §1).
Parameters
xs: the list
Returns
Option[T]: Some of the element at position 0, and None exactly when xs is empty.
Use instead
first_wherefor the first element that meets a condition
See also
Cost
Time O(1) index (ADR 0107).
Example
src/shop.una
module shop
--- The first item of a cart, if any.
describe(names: List[Text]) -> Text
=
when names.first()
is Some(name) -> "first: {name}"
is None -> "the cart is empty"
pub main() -> Unit
uses @console: Console[print]
=
@console.print(describe(["lamp", "desk"]))
@console.print(describe([]))una run . prints:
Output
first: lamp
the cart is emptySpecification
Generated from the standard modules of una v0.4.0 (docs/std/listing.json, which is not part of the specification). The specification governs: spec/14 §1.