Set

Set k

Provides a set type which stores a collection of unique values, without any ordering

empty

Creates a new empty Set.

emptySet = Set.empty {}
countValues = Set.len emptySet

expect countValues == 0

single

Creates a new Set with a single value.

singleItemSet = Set.single "Apple"
countValues = Set.len singleItemSet

expect countValues == 1

insert

Insert a value into a Set.

fewItemSet =
    Set.empty {}
    |> Set.insert "Apple"
    |> Set.insert "Pear"
    |> Set.insert "Banana"

countValues = Set.len fewItemSet

expect countValues == 3

len

Counts the number of values in a given Set.

fewItemSet =
    Set.empty {}
    |> Set.insert "Apple"
    |> Set.insert "Pear"
    |> Set.insert "Banana"

countValues = Set.len fewItemSet

expect countValues == 3

remove

Removes the value from the given Set.

numbers =
    Set.empty {}
    |> Set.insert 10
    |> Set.insert 20
    |> Set.remove 10

has10 = Set.contains numbers 10
has20 = Set.contains numbers 20

expect has10 == Bool.false
expect has20 == Bool.true

contains

Test if a value is in the Set.

Fruit : [Apple, Pear, Banana]

fruit : Set Fruit
fruit =
    Set.single Apple
    |> Set.insert Pear

hasApple = Set.contains fruit Apple
hasBanana = Set.contains fruit Banana

expect hasApple == Bool.true
expect hasBanana == Bool.false

toList

Retrieve the values in a Set as a List.

numbers : Set U64
numbers = Set.fromList [1,2,3,4,5]

values = [1,2,3,4,5]

expect Set.toList numbers == values

fromList

Create a Set from a List of values.

values =
    Set.empty {}
    |> Set.insert Banana
    |> Set.insert Apple
    |> Set.insert Pear

expect Set.fromList [Pear, Apple, Banana] == values

union

Combine two Set collection by keeping the union of all the values pairs. This means that all of the values in both Sets will be combined.

set1 = Set.single Left
set2 = Set.single Right

expect Set.union set1 set2 == Set.fromList [Left, Right]

intersection

Combine two Sets by keeping the intersection of all the values pairs. This means that we keep only those values that are in both Sets.

set1 = Set.fromList [Left, Other]
set2 = Set.fromList [Left, Right]

expect Set.intersection set1 set2 == Set.single Left

difference

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.fromList [Left, Right, Up, Down]
second = Set.fromList [Left, Right]

expect Set.difference first second == Set.fromList [Up, Down]

walk

Iterate through the values of a given Set and build a value.

values = Set.fromList ["March", "April", "May"]

startsWithLetterM = \month ->
    when Str.toUtf8 month is
        ['M', ..] -> Bool.true
        _ -> Bool.false

reduce = \state, k ->
    if startsWithLetterM k then
        state + 1
    else
        state

result = Set.walk values 0 reduce

expect result == 2

walkUntil

Iterate through the values of a given Set and build a value, can stop iterating part way through the collection.

numbers = Set.fromList [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.walkUntil numbers NotFound find42

expect result == FoundTheAnswer