Home / Docs / Standard library / Prelude

map

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 map[T, U](xs: List[T], each: fn(T) -> U) -> List[U]
  ensures result.len == xs.len

The list of each(x) for each element x of xs, in the order of xs; each is called from the first element to the last, at most once per element. In edition 0.2 and later it takes an effectful and failing each, and stops at the first failure (spec/14 §1).

Edition 0.2 and later

una
pub map[T, U](xs: List[T], each: fn(T) -> U) -> List[U]
  ensures result.len == xs.len
  uses of each
  fails of each

The list of each(x) for each element x of xs, in the order of xs; each is called from the first element to the last, at most once per element. In edition 0.2 and later it takes an effectful and failing each, and stops at the first failure (spec/14 §1).

In editions 0.2, 0.3, 0.4, 0.5. In edition 0.1: each has no effects and cannot fail: the declaration has no uses of each or fails of each.

Parameters

Returns

List[U]: each(x) for each element x of xs, in order; as many as xs.

Effects

Those of each: it calls each from the first element on, at most once per element, has the effects of each, and stops at the first failure of each, which becomes its own.

See also

filter, fold

Example

src/shop.unauna
module shop

--- The names of a cart in upper case.
pub main() -> Unit
  uses @console: Console[print]
=
  let names = ["lamp", "desk"].map(each: fn(n) -> n.to_upper())
  @console.print(join(names, separator: ", "))

una run . prints:

Output
LAMP, 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.