Set

Builtin.Set(item) :: # (opaque)
parser_for : _
encoder_for : _
is_eq : Set(a), Set(a) -> Bool where [a.is_eq : a, a -> Bool, a.to_hash : a, Hasher -> Hasher]

Returns Bool.True if the two sets contain the same values, and Bool.False otherwise.

to_hash : Set(a), Hasher -> Hasher where [a.to_hash : a, Hasher -> Hasher]

Feed a Set into a Hasher. The hash is independent of insertion order.

to_inspect : Set(a) -> Str

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])"
empty : () -> Set(_item)

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)
len : Set(_item) -> U64

Counts the number of values in a given Set.

expect Set.single(42).len() == 1
is_empty : Set(_item) -> Bool

Check if the set is empty.

contains : Set(a), a -> Bool where [a.is_eq : a, a -> Bool, a.to_hash : a, Hasher -> Hasher]

Test if a value is in the Set.

insert : Set(a), a -> Set(a) where [a.is_eq : a, a -> Bool, a.to_hash : a, Hasher -> Hasher]

Insert a value into a Set. An equal value already present is retained.

remove : Set(a), a -> Set(a) where [a.is_eq : a, a -> Bool, a.to_hash : a, Hasher -> Hasher]

Removes the value from the given Set. May reorder the remaining values by moving the last value into the removed value's position.

to_list : Set(a) -> List(a)

Retrieve the values in a Set as a List.

fold : Set(a), state, (state, a -> state) -> state

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.U64
iter : Set(a) -> Iter(a)

Iterate 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.

iter_rev : Set(a) -> Iter(a)

Iterate in reverse of Set.iter's order without allocating a reversed list.

from_list : List(a) -> Set(a) where [a.is_eq : a, a -> Bool, a.to_hash : a, Hasher -> Hasher]

Create a Set from a List of values.

keep_if : Set(a), (a -> Bool) -> Set(a) where [a.to_hash : a, Hasher -> Hasher]

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])
drop_if : Set(a), (a -> Bool) -> Set(a) where [a.to_hash : a, Hasher -> Hasher]

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])
union : 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 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])
}
map : Set(a), (a -> b) -> Set(b) where [b.is_eq : b, b -> Bool, b.to_hash : b, Hasher -> Hasher]

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])
with_capacity : U64 -> Set(_item)

Creates an empty set with room for at least the requested number of values.

capacity : Set(_item) -> U64

Returns the number of values the set can hold before growing its hash table.

reserve : Set(item), U64 -> Set(item) where [item.to_hash : item, Hasher -> Hasher]

Ensures room for at least this many additional values.

release_excess_capacity : Set(item) -> Set(item) where [item.to_hash : item, Hasher -> Hasher]

Reduces unused capacity while retaining every value.

clear : Set(item) -> Set(item)

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.

fold_until : Set(item), state, (state, item -> [Continue(state), Break(state)]) -> state

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.