Set

is_eq : Set(a), Set(a) -> Bool where { a.is_eq : a, a -> Bool }

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

empty : -> Set(_item)

Creates a new empty Set.

single : item -> Set(item)

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 }

Test if a value is in the Set.

insert : Set(a), a -> Set(a) where { a.is_eq : a, a -> Bool }

Insert a value into a Set.

remove : Set(a), a -> Set(a) where { a.is_eq : a, a -> Bool }

Removes the value from the given Set.

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

Retrieve the values in a Set as a List.

from_list : List(a) -> Set(a) where { a.is_eq : a, a -> Bool }

Create a Set from a List of values.

keep_if : Set(a), (a -> Bool) -> Set(a)

Run the given function on each element in the Set, and return a Set with just the elements 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)

Run the given function on each element in the Set, and return a Set with just the elements 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 }

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 }

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 }

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 }

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])