Home / Docs / Standard library / Prelude

append

Release v0.4.0Edition 0.1 and later

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

una
pub append[T](xs: List[T], item: T) -> List[T]
  ensures result.len == xs.len + 1

xs with item added after its last element (spec/14 §1).

Parameters

Returns

List[T]: xs with item after its last element; one element longer than xs.

See also

set (edition 0.2 on)

Cost

Time amortized O(1) append, O(n) copy when the list is shared (ADR 0107), where:

Example

src/shop.unauna
module shop

--- A cart with one more item.
pub main() -> Unit
  uses @console: Console[print]
=
  let cart = ["lamp"].append(item: "chair")
  @console.print(join(cart, separator: ", "))
  @console.print("{cart.len} items")

una run . prints:

Output
lamp, chair
2 items

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.