Home / Docs / Standard library / Prelude
set
A function of the prelude, in scope in every module. Edition 0.2 and later.
pub set[T](edit xs: List[T], index: Int, item: T) -> Unit
requires 0 <= index and index < xs.len
ensures xs.len == old(xs).lenReplaces the element at index of xs with item (spec/17 §8).
Parameters
xs: the list, changed in placeindex: the position to replace, from 0item: the new element
Returns
Unit: nothing; afterwards xs holds item at index, and its length is unchanged.
Requires
0 <= index and index < xs.len: the position is inside the list
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
Cost
Time O(1) index and set, O(n) copy when the list is shared (ADR 0107), where:
n: the length ofxs
Example
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:
total 22Specification
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.