Home / Docs / Standard library / Prelude

sum

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 sum(xs: List[Int]) -> Int

The exact sum of the elements as an Int, 0 when xs is empty; Int has no bound, so it never overflows. In edition 0.2 and later it takes any integer type and adds the values as Int (spec/14 §1, spec/20 §6).

Edition 0.2 and later

una
pub sum[T: Integer](xs: List[T]) -> Int

The exact sum of the elements as an Int, 0 when xs is empty; Int has no bound, so it never overflows. In edition 0.2 and later it takes any integer type and adds the values as Int (spec/14 §1, spec/20 §6).

In editions 0.2, 0.3, 0.4, 0.5. In edition 0.1: sum(xs: List[Int]) -> Int: Int elements only.

Parameters

Returns

Int: the exact sum of the elements, as an Int; 0 for an empty list; it never overflows.

See also

fold

Example

src/shop.unauna
module shop

--- The total of a cart, in cents.
pub main() -> Unit
  uses @console: Console[print]
=
  let total = [1999, 499].sum()
  @console.print("total {total}")

una run . prints:

Output
total 2498

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.