Set
Builtin.Set(item) :: # (opaque)
Returns Bool.True if the two sets contain the same values, and Bool.False otherwise.
Returns a human-readable representation of a Set, with items rendered
using Str.inspect.
expect Str.inspect(Set.from_list([1.I64, 2, 3])) == "Set.from_list([1, 2, 3])"Creates a new empty Set.
single : item -> Set(item) where [item.is_eq : item, item -> Bool, item.to_hash : item, Hasher -> Hasher]Creates a new Set with a single value.
Set.single(42.I64)Counts the number of values in a given Set.
expect Set.single(42).len() == 1Check if the set is empty.
Test if a value is in the Set.
Insert a value into a Set. An equal value already present is retained.
Removes the value from the given Set. May reorder the remaining values
by moving the last value into the removed value's position.
Retrieve the values in a Set as a List.
Build a value by folding through each value in the set. Starting with
a given state value, this runs the given step function on each
value, using its return value as the new state. It returns the final
state at the end.
expect Set.from_list([1, 2, 3.U64]).fold(0, |sum, item| sum + item) == 6
expect Set.empty().fold(0, |sum, item| sum + item) == 0.U64Iterate over the set's values without allocating a list of keys.
New values are appended, and inserting an existing value preserves its
position. Set.remove may reorder the remaining values.
Iterate in reverse of Set.iter's order without allocating a reversed list.
Create a Set from a List of values.
Run the given function on each item in the Set, and return
a Set with just the items for which the function returned Bool.True.
expect Set.from_list([1, 2, 3, 4]).keep_if(|num| num > 2) == Set.from_list([3, 4])Run the given function on each item in the Set, and return
a Set with just the items for which the function returned Bool.False.
expect Set.from_list([1, 2, 3, 4]).drop_if(|num| num > 2) == Set.from_list([1, 2])Combine two Sets by keeping the
union
of all the values.
expect {
a = Set.from_list([1, 2, 3])
b = Set.from_list([3, 4, 5])
Set.union(a, b) == Set.from_list([1, 2, 3, 4, 5])
}intersection : Set(a), Set(a) -> Set(a) where [a.is_eq : a, a -> Bool, a.to_hash : a, Hasher -> Hasher]Combine two Sets by keeping the
intersection
of all the values.
expect {
a = Set.from_list([1, 2, 3])
b = Set.from_list([2, 3, 4])
Set.intersection(a, b) == Set.from_list([2, 3])
}difference : Set(a), Set(a) -> Set(a) where [a.is_eq : a, a -> Bool, a.to_hash : a, Hasher -> Hasher]Remove the values in the first Set that are also in the second Set
using the set difference.
expect {
a = Set.from_list([1, 2, 3])
b = Set.from_list([2, 3, 4])
Set.difference(a, b) == Set.from_list([1])
}Convert each value in the set to something new, by calling a conversion function on each of them. Then return a new set containing the unique converted values.
expect Set.from_list([1, 2, 3]).map(|n| n * 2) == Set.from_list([2, 4, 6])
# Duplicates in the mapped output are collapsed—the result is a Set.
expect Set.from_list([1, -1, 2, -2]).map(|n| n * n) == Set.from_list([1, 4])Creates an empty set with room for at least the requested number of values.
Returns the number of values the set can hold before growing its hash table.
Ensures room for at least this many additional values.
Reduces unused capacity while retaining every value.
Removes every value while preserving the current capacity.
subscript : Set(item), item -> Bool where [item.is_eq : item, item -> Bool, item.to_hash : item, Hasher -> Hasher]Alias for Set.contains.
from_iter : Iter(item) -> Set(item) where [item.is_eq : item, item -> Bool, item.to_hash : item, Hasher -> Hasher]Creates a set from an iterator, retaining the first position of each value. Uses the iterator's known length to reserve storage when available.
Folds values in iteration order, stopping immediately on Break(state).
Returns the initial state when the set is empty.
join_map : Set(a), (a -> Set(b)) -> Set(b) where [b.is_eq : b, b -> Bool, b.to_hash : b, Hasher -> Hasher]Transforms each value into a set and combines the results, removing duplicates.