Home / Docs / Standard library / Prelude
at
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 at[T](xs: List[T], index: Int) -> Option[T]
ensures (result is None) == (index < 0 or index >= xs.len)Some of the element at position index, counted from 0, and None exactly when index is below 0 or at least xs.len; it never fails (spec/14 §1).
Parameters
xs: the listindex: the position, from 0
Returns
Option[T]: Some of the element at index, and None exactly when index is below 0 or at least xs.len; it never fails.
See also
Cost
Time O(1) index (ADR 0107).
Example
src/shop.una
module shop
--- The item at a position of a cart, if there is one.
describe(names: List[Text], index: Int) -> Text
=
when names.at(index: index)
is Some(name) -> "{index}: {name}"
is None -> "{index}: no item"
pub main() -> Unit
uses @console: Console[print]
=
let names = ["lamp", "desk", "chair"]
@console.print(describe(names, index: 1))
@console.print(describe(names, index: 3))una run . prints:
Output
1: desk
3: no itemSpecification
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.