Home / Docs / Standard library / Prelude

first_where

Release v0.4.0Edition 0.1 and later

A function of the prelude, in scope in every module. Edition 0.1 and later.

Edition 0.1

una
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

una
pub first_where[T](xs: List[T], keep: fn(T) -> Bool) -> Option[T]
  uses of keep
  fails of keep

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).

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

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

filter, contains

Example

src/shop.unauna
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:

Output
desk

Specification

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.