std/sets
Import with import std/sets.
Exports
Section titled “Exports”Types
struct Set
Section titled “struct Set”struct Set<T>
opaque — construction surface is private to its defining module
Immutable collection of unique T values.
Set.new
Section titled “Set.new”fn new<T>(): Set<T>
type function on Set
The empty set.
Interactive Tests
s: Set<Int> = Set.new()
assert Set.size(s) == 0
Set.from_list
Section titled “Set.from_list”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}
Set.insert
Section titled “Set.insert”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}
Set.remove
Section titled “Set.remove”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}
Set.contains?
Section titled “Set.contains?”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)
Set.size
Section titled “Set.size”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
Set.union
Section titled “Set.union”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}
Set.intersection
Section titled “Set.intersection”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}
Set.difference
Section titled “Set.difference”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}
Set.subset?
Section titled “Set.subset?”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})
Set.each_while
Section titled “Set.each_while”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.
Set.known_count
Section titled “Set.known_count”fn known_count(s: Set<T>): Maybe<Int>
impl Iter.known_count
Set.to_string
Section titled “Set.to_string”fn to_string(s: Set<T>): String
impl Display.to_string
Set.inspect
Section titled “Set.inspect”fn inspect(s: Set<T>): String
impl Debug.inspect
Set.equal?
Section titled “Set.equal?”fn equal?(a: Set<T>, b: Set<T>): Bool
impl Equatable.equal?
Set.hash
Section titled “Set.hash”fn hash(s: Set<T>): Int
impl Hashable.hash