Skip to content

std/sets

Import with import std/sets.

Types

struct Set<T>

opaque — construction surface is private to its defining module

Immutable collection of unique T values.

fn new<T>(): Set<T>

type function on Set

The empty set.

Interactive Tests

s: Set<Int> = Set.new()
assert Set.size(s) == 0
fn from_list<T>(elems: List<T>): Set<T>

type function on Set

Builds a set from a list, discarding duplicates (first occurrence wins for ordering). Prefer the #{...} literal when the elements are known inline.

Interactive Tests

assert Set.from_list([1, 2, 2, 3]) == #{1, 2, 3}
fn insert<T>(s: Set<T>, elem: T): Set<T>

type function on Set

Returns a new set with elem added (no-op if already present).

Interactive Tests

assert Set.insert(#{1, 2}, 3) == #{1, 2, 3}
assert Set.insert(#{1, 2}, 2) == #{1, 2}
fn remove<T>(s: Set<T>, elem: T): Set<T>

type function on Set

Returns a new set with elem removed (no-op if absent).

Interactive Tests

assert Set.remove(#{1, 2, 3}, 2) == #{1, 3}
assert Set.remove(#{1, 2}, 9) == #{1, 2}
fn contains?<T>(s: Set<T>, elem: T): Bool

type function on Set

True if elem is a member of the set.

Interactive Tests

assert Set.contains?(#{1, 2}, 1)
refute Set.contains?(#{1, 2}, 9)
fn size<T>(s: Set<T>): Int

type function on Set

The number of elements. O(1) — the set’s own count; the generic count is Iter.count.

Interactive Tests

assert Set.size(#{1, 2, 3}) == 3
fn union<T>(a: Set<T>, b: Set<T>): Set<T>

type function on Set

All elements in either set.

Interactive Tests

assert Set.union(#{1, 2}, #{2, 3}) == #{1, 2, 3}
fn intersection<T>(a: Set<T>, b: Set<T>): Set<T>

type function on Set

Elements in both sets.

Interactive Tests

assert Set.intersection(#{1, 2}, #{2, 3}) == #{2}
fn difference<T>(a: Set<T>, b: Set<T>): Set<T>

type function on Set

Elements in a but not b.

Interactive Tests

assert Set.difference(#{1, 2, 3}, #{2}) == #{1, 3}
fn subset?<T>(a: Set<T>, b: Set<T>): Bool

type function on Set

True if every element of a is also in b.

Interactive Tests

assert Set.subset?(#{1, 2}, #{1, 2, 3})
refute Set.subset?(#{1, 4}, #{1, 2, 3})
fn each_while(s: Set<T>, yield: (T) -> Bool): Bool

impl Iter.each_while

Pushes each element to yield in insertion order (matching to_list), stopping as soon as yield returns False. Drives the backing map’s own push loop and drops the present half of each entry.

fn known_count(s: Set<T>): Maybe<Int>

impl Iter.known_count

fn to_string(s: Set<T>): String

impl Display.to_string

fn inspect(s: Set<T>): String

impl Debug.inspect

fn equal?(a: Set<T>, b: Set<T>): Bool

impl Equatable.equal?

fn hash(s: Set<T>): Int

impl Hashable.hash