Updating
The function List.update_with
enables the replacement of elements of
a given list according to a boolean function: if the call of that
function on a element is true, then the element is replaced, otherwise
it remains.
const nats : list<int> = [0, 1, 2, 3, 4];
// evens_zeroed == [0, 1, 0, 3, 0]
const evens_zeroed = List.update_with(x => x % 2 == 0n, 0, nats);
The function List.update
enables the selective replacement of
elements of a given list according to a function that returns an
optional value, instead of a boolean as List.update_with
above.
That function takes an element and returns an optional value: if that
value is None()
, then the element is left unchanged, otherwise, if
the value is Some(v)
, then the element is replaced in the resulting
list by v
.
const f = x => x % 2 == 0n ? None() : Some(x*x);
// odds == [0, 1, 2, 9, 4]
const odds_squared = List.update(f, nats);
See predefined namespace List.