docs / Getting Started / Collection Traits

Collection Traits

Collections share a four-trait taxonomy that breaks the classic "kitchen sink" iterator interface into focused, composable pieces. List<T> and Array<T, N> inherit the default method bodies on each trait, so the same fluent pipeline works across collection types.

use system.io
use system.collections.list

fn main()
    let xs = List([1, 2, 3, 4, 5])

    // Transformable: map, filter, flat_map.
    let doubled = xs.map(fn(x int) int: x * 2)
    let evens = xs.filter(fn(x int) bool: x % 2 == 0)

    // Foldable: reduce, any, all, sum, min, max.
    // sum/min/max return T? — None on empty, Some(value) otherwise.
    let total = xs.sum() ?? 0
    let any_big = xs.any(fn(x int) bool: x > 4)
    let folded = xs.reduce(0, fn(acc int, x int) int: acc + x)

    // Sequenced: take, skip, sorted_by, reversed, zip, enumerate.
    let head = xs.take(3)
    let tail = xs.skip(2)
    let paired = List([1, 2, 3]).zip(List([10, 20, 30]))
    let indexed = List(["a", "b", "c"]).enumerate()

    // Queryable: is_empty, first, last, contains, index_of.
    println(f"{xs.first() ?? 0} {xs.last() ?? 0}")
    println(f"{xs.contains(3)} {xs.index_of(4)}")

    println(f"{total} {folded} {any_big}")
TraitMethodsReturns
Queryable<T>is_empty, first, last, contains, index_ofbool / T? / int?
Transformable<T>map, filter, flat_mapSelf
Foldable<T>reduce, any, all, count_where, sum, min, maxscalar / T?
Sequenced<T>take, skip, sorted_by, unique, reversed, zip, enumerateSelf

sum, min, and max return T?None on an empty collection, Some(value) otherwise. The standard library never traps on an empty input. Combine with ?? default for a one-shot fallback.

Map<K, V> and Set<T> keep ad-hoc map / filter / reduce methods — they don't fit the generic Self-returning shape of the traits until associated types and generic methods land in a future release.