Set

A set of unique values. The values can be any comparable type. This includes Int, Float, Time, Char, String, and tuples or arrays of comparable types.

Set, remove, and query operations all take O(log n) time.

type Set t

Represents a set of unique values. So (Set Int) is a set of integers and (Set String) is a set of strings.

empty : Set a

Create an empty set.

singleton : comparable -> Set comparable

Create a set with one value.

set : comparable -> Set comparable -> Set comparable

Set a value into a set.

remove : comparable -> Set comparable -> Set comparable

Remove a value from a set. If the value is not found, no changes are made.

toggle : comparable -> Set comparable -> Set comparable

Toggle a value in a set. If the value isn't in the set, it is added. If the value is in the set, it is removed.

Query

isEmpty : Set a -> Bool

Determine if a set is empty.

member : comparable -> Set comparable -> Bool

Determine if a value is in a set.

count : Set a -> Int

Determine the number of elements in a set.

first : Set a -> Maybe a

Get the first element of the set.

last : Set a -> Maybe a

Get the last element of the set.

findFirst : (a -> Bool) -> Set a -> Maybe a

Find the first value that passes the test.

findLast : (a -> Bool) -> Set a -> Maybe a

Find the last value that passes the test.

any : (a -> Bool) -> Set a -> Bool

Checks if any value in the set passes the test.

all : (a -> Bool) -> Set a -> Bool

Checks if all values in the set passes the test.

Combine

union : Set comparable -> Set comparable -> Set comparable

Get the union of two sets. Keep all values.

intersect : Set comparable -> Set comparable -> Set comparable

Get the intersection of two sets. Keeps values that appear in both sets.

diff : Set comparable -> Set comparable -> Set comparable

Get the difference between the first set and the second. Keeps values that do not appear in the second set.

Arrays

toArray : Set a -> Array a

Convert a set into an array, sorted from lowest to highest.

fromArray : Array comparable -> Set comparable

Convert an array into a set, removing any duplicates.

Transform

map :
(comparable -> comparable2) -> Set comparable -> Set comparable2

Map a function onto a set, creating a new set with no duplicates.

foldl : (a -> b -> b) -> b -> Set a -> b

Fold over the values in a set, in order from lowest to highest.

foldr : (a -> b -> b) -> b -> Set a -> b

Fold over the values in a set, in order from highest to lowest.

filter : (comparable -> Bool) -> Set comparable -> Set comparable

Only keep elements that pass the given test.

import Set exposing (Set)

numbers : Set Int
numbers =
    Set.fromArray [ -2, -1, 0, 1, 2 ]

positives : Set Int
positives =
    Set.filter (\x -> x > 0) numbers

-- positives == Set.fromArray [1,2]
filterMap :
(comparable -> Maybe comparable2) -> Set comparable -> Set comparable2

Filter out unwanted results of a map operation.

import Set

strings : Set String
strings =
    Set.fromArray [ "3", "not a number", "-5" ]

numbers : Set Int
numbers =
    Set.filterMap String.toInt strings

-- numbers == Set.fromArray [ 3, -5 ]
partition :
(comparable -> Bool)
-> Set comparable
-> { trues : Set comparable, falses : Set comparable
}

Create two new sets. The first contains all the elements that passed the given test, and the second contains all the elements that did not.