Home / Docs / Standard library / Prelude

set

Release v0.4.0Edition 0.2 and later

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

una
pub set[T](edit xs: List[T], index: Int, item: T) -> Unit
  requires 0 <= index and index < xs.len
  ensures xs.len == old(xs).len

Replaces the element at index of xs with item (spec/17 §8).

Parameters

Returns

Unit: nothing; afterwards xs holds item at index, and its length is unchanged.

Requires

Each condition under requires is an obligation at every call, which una check proves (spec/12 §3): it reports U5001 when the condition is refuted and U5002 when it is unknown (spec/12 §6), and una run, una test and una build refuse a package with an obligation that is not proved (spec/22 §2).

See also

append, at

Cost

Time O(1) index and set, O(n) copy when the list is shared (ADR 0107), where:

Example

src/shop.unauna
module shop

--- A shelf count corrected after a recount.
pub main() -> Unit
  uses @console: Console[print]
=
  let edit counts = [3, 0, 7]
  if counts.len > 1
    edit counts.set(index: 1, item: 12)
  let total = counts.sum()
  @console.print("total {total}")

una run . prints:

Output
total 22

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.