Home / Docs / Standard library / Prelude
first_where
A function of the prelude, in scope in every module. Edition 0.1 and later.
Edition 0.1
pub first_where[T](xs: List[T], keep: fn(T) -> Bool) -> Option[T]Some of the first element x of xs for which keep(x) is true, and None when there is none; keep is called from the first element up to that one, at most once per element, and not after it. In edition 0.2 and later it takes an effectful and failing keep, and stops at the first failure (spec/14 §1).
Edition 0.2 and later
pub first_where[T](xs: List[T], keep: fn(T) -> Bool) -> Option[T]
uses of keep
fails of keepSome of the first element x of xs for which keep(x) is true, and None when there is none; keep is called from the first element up to that one, at most once per element, and not after it. In edition 0.2 and later it takes an effectful and failing keep, and stops at the first failure (spec/14 §1).
In editions 0.2, 0.3, 0.4, 0.5. In edition 0.1: keep has no effects and cannot fail: the declaration has no uses of keep or fails of keep.
Parameters
xs: the listkeep: the condition the element must meet
Returns
Option[T]: Some of the first element for which keep is true, and None when there is none; keep is not called after that element.
Effects
Those of keep: it calls keep from the first element on, at most once per element, has the effects of keep, and stops at the first failure of keep, which becomes its own.
See also
Example
module shop
--- The first item of a cart whose name starts with the letter d.
pub main() -> Unit
uses @console: Console[print]
=
let names = ["lamp", "desk", "door"]
when names.first_where(keep: fn(n) -> n.starts_with(prefix: "d"))
is Some(name) -> @console.print(name)
is None -> @console.print("none")una run . prints:
deskSpecification
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.