---
title: "first_where"
url: https://una-lang.org/en/docs/std/prelude/first_where/
release: v0.4.0
edition: "0.1 and later"
source: https://github.com/una-lang/una/blob/v0.4.0/docs/std/listing.json
---

# first_where

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

- `xs`: the list
- `keep`: 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

[`filter`](https://una-lang.org/en/docs/std/prelude/filter/), [`contains`](https://una-lang.org/en/docs/std/prelude/contains/)

## Example

`src/shop.una`

```una
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:

```text
desk
```

## Specification

- [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1)

Generated from the standard modules of una v0.4.0 ([`docs/std/listing.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/docs/std/listing.json), which is not part of the specification). The specification governs: [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1).
