contains : Set k, k -> Bool
Test if a value is in the Set.
Fruit : [Apple, Pear, Banana]
fruit : Set Fruit
fruit =
Set.single(Apple)
|> Set.insert(Pear)
has_apple = Set.contains(fruit, Apple)
has_banana = Set.contains(fruit, Banana)
expect has_apple == Bool.true
expect has_banana == Bool.falseto_list : Set k -> List k
Retrieve the values in a Set as a List.
numbers : Set U64
numbers = Set.from_list([1,2,3,4,5])
values = [1,2,3,4,5]
expect Set.to_list(numbers) == values
difference : Set k, Set k -> Set k
Remove the values in the first Set that are also in the second Set
using the set difference
of the values. This means that we will be left with only those values that
are in the first and not in the second.
first = Set.from_list([Left, Right, Up, Down])
second = Set.from_list([Left, Right])
expect Set.difference(first, second) == Set.from_list([Up, Down])
walk :
Set k,
state,
(state, k -> state)
-> state
Iterate through the values of a given Set and build a value.
values = Set.from_list(["March", "April", "May"])
starts_with_letter_m = \month ->
when Str.to_utf8(month) is
['M', ..] -> Bool.true
_ -> Bool.false
reduce = \state, k ->
if starts_with_letter_m(k) then
state + 1
else
state
result = Set.walk(values, 0, reduce)
expect result == 2walk_until :
Set k,
state,
(state,
k
->
[
Continue state,
Break state
])
-> state
Iterate through the values of a given Set and build a value, can stop
iterating part way through the collection.
numbers = Set.from_list([1,2,3,4,5,6,42,7,8,9,10])
find42 = \state, k ->
if k == 42 then
Break(FoundTheAnswer)
else
Continue(state)
result = Set.walk_until(numbers, NotFound, find42)
expect result == FoundTheAnswerkeep_if : Set k, (k -> Bool) -> Set k
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,5])
|> Set.keep_if(\k -> k >= 3)
|> Bool.is_eq(Set.from_list([3,4,5]))drop_if : Set k, (k -> Bool) -> Set k
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,5]
|> Set.drop_if(\k -> k >= 3)
|> Bool.is_eq(Set.from_list([1,2]))