Set

Standard library for set operations.

Functions in this module operate on type Set<A>, which represents a hash set (using a hash array mapped trie) with O(log n) put(), contains?(), and delete(). Literal sets can be created with the syntax #[a, b, ...], where a, b, etc. are elements.

To import all names from this module, use:

import Set (*)

With no import, you can still access anything with the prefix Set., like Set.intersect.

Index

NameTypeDescription

length(sized)

Set<A> -> Int

Returns the length of sized.

empty?(sized)

Set<A> -> Bool

Returns whether sized is empty.

concat(a, b)

(Set<A>, Set<A>) -> Set<A>

Concatenates a and b together, equivalent to a ++ b.

concat_all(l)

[Set<A>] -> Set<A>

Concatenates all elements in the list l together.

map(container, f)

(Set<A>, A -> B) -> Set<B>

Calls f for each element in container, collecting the results into a new container.

intersect(a, b)

(Set<A>, Set<A>) -> Set<A>

Returns a new set with elements that are both in set a and in set b.

union(a, b)

(Set<A>, Set<A>) -> Set<A>

Merges sets a and b, returning a new set.

subtract(a, b)

(Set<A>, Set<A>) -> Set<A>

Returns a new set with elements that are in set a, but not in set b.

subset?(a, b)

(Set<A>, Set<A>) -> Bool

Returns true if b is a subset of a; that is, if a contains every element in b.

disjoint?(a, b)

(Set<A>, Set<A>) -> Bool

Returns true if a and b share no elements in common.

fold(collection, init, f)

(Set<A>, F, (F, A) -> F) -> F

Computes a single value by applying the combining function f to each element of the given collection, starting with the initial value init.

filter(collection, f)

(Set<A>, A -> Bool) -> Set<A>

Calls f for each element in collection, returning a new collection that only contains the elements for which f returns true.

find(collection, f)

(Set<A>, A -> Bool) -> Option<A>

Returns the first element in collection for which f returns true.

filter_map(collection, f)

(Set<A>, A -> Option<B>) -> Set<B>

Calls f for each element in collection, returning a new collection that contains every e such that f returned Some(e).

fold_map(collection, init, f)

(Set<A>, F, (F, A) -> (F, B)) -> (F, Set<B>)

Computes a new collection and a single value by applying the combining function f to each element of the given collection, starting with the initial value init.

put(collection, elem)

(Set<A>, A) -> Set<A>

Puts elem into collection, returning a new collection.

delete(collection, elem)

(Set<A>, A) -> Set<A>

Removes elem from collection, returning a new collection.

contains?(collection, elem)

(Set<A>, A) -> Bool

Returns true if collection contains elem.

all?(collection, f)

(Set<A>, A -> Bool) -> Bool

Calls f for each element in collection, and returns true if f returns true for every element.

any?(collection, f)

(Set<A>, A -> Bool) -> Bool

Calls f for each element in collection, and returns true if f returns true for at least one element.

to_list(a)

Set<A> -> [A]

Converts a to a list.

to_map(a)

Set<(K, V)> -> Map<K, V>

Converts a to a map.

Functions

intersect : (Set<A>, Set<A>) -> Set<A>
intersect(a, b)

Returns a new set with elements that are both in set a and in set b.

assert intersect(#[1, 3, 5], #[3, 5, 6]) == #[3, 5]
assert intersect(#['a', 'b'], #['c', 'd', 'e', 'f']) == #[]
assert intersect(#[], #["hey", "hello"]) == #[]
union : (Set<A>, Set<A>) -> Set<A>
union(a, b)

Merges sets a and b, returning a new set. This is an alias for concat() and is equivalent to a ++ b.

assert union(#[1, 3, 5], #[3, 5, 6]) == #[1, 3, 5, 6]
assert union(#['a', 'b'], #['c', 'd', 'e', 'f']) ==
  #['a', 'b', 'c', 'd', 'e', 'f']
assert union(#[], #["hey", "hello"]) == #["hey", "hello"]
subtract : (Set<A>, Set<A>) -> Set<A>
subtract(a, b)

Returns a new set with elements that are in set a, but not in set b. This is equivalent to a -- b.

assert subtract(#[1, 3, 5], #[3, 5, 6]) == #[1]
assert subtract(#[3, 5, 6], #[1, 3, 5]) == #[6]
assert subtract(#[], #["hey", "hello"]) == #[]
assert subtract(#["hey", "hello"], #[]) == #["hey", "hello"]
subset? : (Set<A>, Set<A>) -> Bool
subset?(a, b)

Returns true if b is a subset of a; that is, if a contains every element in b. Note that if a == b, this function returns true.

assert subset?(#[3, 5, 6], #[3, 5])
assert !subset?(#[3, 5, 6], #[1, 3, 5])
assert !subset?(#[3, 5], #[3, 5, 6])
assert subset?(#["hey", "hello"], #[])
disjoint? : (Set<A>, Set<A>) -> Bool
disjoint?(a, b)

Returns true if a and b share no elements in common.

assert disjoint?(#[1, 3, 5], #[2, 4, 6])
assert !disjoint?(#[1, 3, 5], #[2, 5])
assert disjoint?(#[], #["hey", "hello"])

Implementations

impl Sized for Set<A>

The following functions are from the Sized interface.

length : Set<A> -> Int
length(sized)

Returns the length of sized. See the full description in the Sized interface.

empty? : Set<A> -> Bool
empty?(sized)

Returns whether sized is empty. See the full description in the Sized interface.

impl Concat for Set<A>

The following functions are from the Concat interface.

concat : (Set<A>, Set<A>) -> Set<A>
concat(a, b)

Concatenates a and b together, equivalent to a ++ b. See the full description in the Concat interface.

concat_all : [Set<A>] -> Set<A>
concat_all(l)

Concatenates all elements in the list l together. See the full description in the Concat interface.

impl Mappable for Set

The following functions are from the Mappable interface.

map : (Set<A>, A -> B) -> Set<B>
map(container, f)

Calls f for each element in container, collecting the results into a new container. See the full description in the Mappable interface.

impl Collection for Set

The following functions are from the Collection interface.

fold : (Set<A>, F, (F, A) -> F) -> F
fold(collection, init, f)

Computes a single value by applying the combining function f to each element of the given collection, starting with the initial value init. See the full description in the Collection interface.

filter : (Set<A>, A -> Bool) -> Set<A>
filter(collection, f)

Calls f for each element in collection, returning a new collection that only contains the elements for which f returns true. See the full description in the Collection interface.

find : (Set<A>, A -> Bool) -> Option<A>
find(collection, f)

Returns the first element in collection for which f returns true. See the full description in the Collection interface.

filter_map : (Set<A>, A -> Option<B>) -> Set<B>
filter_map(collection, f)

Calls f for each element in collection, returning a new collection that contains every e such that f returned Some(e). See the full description in the Collection interface.

fold_map : (Set<A>, F, (F, A) -> (F, B)) -> (F, Set<B>)
fold_map(collection, init, f)

Computes a new collection and a single value by applying the combining function f to each element of the given collection, starting with the initial value init. See the full description in the Collection interface.

put : (Set<A>, A) -> Set<A>
put(collection, elem)

Puts elem into collection, returning a new collection. See the full description in the Collection interface.

delete : (Set<A>, A) -> Set<A>
delete(collection, elem)

Removes elem from collection, returning a new collection. See the full description in the Collection interface.

contains? : (Set<A>, A) -> Bool
contains?(collection, elem)

Returns true if collection contains elem. See the full description in the Collection interface.

all? : (Set<A>, A -> Bool) -> Bool
all?(collection, f)

Calls f for each element in collection, and returns true if f returns true for every element. See the full description in the Collection interface.

any? : (Set<A>, A -> Bool) -> Bool
any?(collection, f)

Calls f for each element in collection, and returns true if f returns true for at least one element. See the full description in the Collection interface.

impl ToList for Set

The following functions are from the ToList interface.

to_list : Set<A> -> [A]
to_list(a)

Converts a to a list. See the full description in the ToList interface.

impl ToMap for Set

The following functions are from the ToMap interface.

to_map : Set<(K, V)> -> Map<K, V>
to_map(a)

Converts a to a map. See the full description in the ToMap interface.