Skip to content

set

The set module is auto-imported — no import statement needed.

FunctionSignatureDescription
new() -> Set[A]Create an empty set.
from_list(List[A]) -> Set[A]Create a set from a list of values.
insert(Set[A], A) -> Set[A]Add a value to the set. Returns a new set.
remove(Set[A], A) -> Set[A]Remove a value from the set. Returns a new set.
contains(Set[A], A) -> BoolCheck if a value is in the set.
len(Set[A]) -> IntReturn the number of elements.
is_empty(Set[A]) -> BoolCheck if the set has no elements.
to_list(Set[A]) -> List[A]Convert a set to a list.
union(Set[A], Set[A]) -> Set[A]Return the union of two sets.
intersection(Set[A], Set[A]) -> Set[A]Return the intersection of two sets.
difference(Set[A], Set[A]) -> Set[A]Return elements in a that are not in b.
symmetric_difference(Set[A], Set[A]) -> Set[A]Return elements in either set but not both.
is_subset(Set[A], Set[A]) -> BoolCheck if all elements of a are in b.
is_disjoint(Set[A], Set[A]) -> BoolCheck if two sets have no elements in common.
filter(Set[A], Fn[A] -> Bool) -> Set[A]Keep elements that satisfy a predicate.
map(Set[A], Fn[A] -> B) -> Set[B]Apply a function to each element, returning a new set.
fold(Set[A], B, Fn[B, A] -> B) -> BReduce a set with an initial accumulator.
any(Set[A], Fn[A] -> Bool) -> BoolCheck if any element satisfies a predicate.
all(Set[A], Fn[A] -> Bool) -> BoolCheck if all elements satisfy a predicate.

Create an empty set.

set.new()

Create a set from a list of values.

set.from_list([1, 2, 3])

Add a value to the set. Returns a new set.

set.insert(s, 42)

Remove a value from the set. Returns a new set.

set.remove(s, 42)

Check if a value is in the set.

set.contains(s, 42)

Return the number of elements.

set.len(s)

Check if the set has no elements.

set.is_empty(s)

Convert a set to a list.

set.to_list(s)

Return the union of two sets.

set.union(a, b)

set.intersection(a: Set[A], b: Set[A]) -> Set[A]

Section titled “set.intersection(a: Set[A], b: Set[A]) -> Set[A]”

Return the intersection of two sets.

set.intersection(a, b)

set.difference(a: Set[A], b: Set[A]) -> Set[A]

Section titled “set.difference(a: Set[A], b: Set[A]) -> Set[A]”

Return elements in a that are not in b.

set.difference(a, b)

set.symmetric_difference(a: Set[A], b: Set[A]) -> Set[A]

Section titled “set.symmetric_difference(a: Set[A], b: Set[A]) -> Set[A]”

Return elements in either set but not both.

set.symmetric_difference(a, b)

set.is_subset(a: Set[A], b: Set[A]) -> Bool

Section titled “set.is_subset(a: Set[A], b: Set[A]) -> Bool”

Check if all elements of a are in b.

set.is_subset(a, b)

set.is_disjoint(a: Set[A], b: Set[A]) -> Bool

Section titled “set.is_disjoint(a: Set[A], b: Set[A]) -> Bool”

Check if two sets have no elements in common.

set.is_disjoint(a, b)

set.filter(s: Set[A], f: Fn[A] -> Bool) -> Set[A]

Section titled “set.filter(s: Set[A], f: Fn[A] -> Bool) -> Set[A]”

Keep elements that satisfy a predicate.

set.filter(s, fn(x) => x > 2)

set.map(s: Set[A], f: Fn[A] -> B) -> Set[B]

Section titled “set.map(s: Set[A], f: Fn[A] -> B) -> Set[B]”

Apply a function to each element, returning a new set.

set.map(s, fn(x) => x * 2)

set.fold(s: Set[A], init: B, f: Fn[B, A] -> B) -> B

Section titled “set.fold(s: Set[A], init: B, f: Fn[B, A] -> B) -> B”

Reduce a set with an initial accumulator.

set.fold(s, 0, fn(acc, x) => acc + x)

set.any(s: Set[A], f: Fn[A] -> Bool) -> Bool

Section titled “set.any(s: Set[A], f: Fn[A] -> Bool) -> Bool”

Check if any element satisfies a predicate.

set.any(s, fn(x) => x > 2)

set.all(s: Set[A], f: Fn[A] -> Bool) -> Bool

Section titled “set.all(s: Set[A], f: Fn[A] -> Bool) -> Bool”

Check if all elements satisfy a predicate.

set.all(s, fn(x) => x > 0)