bburdette / typed-collections / TSet

A set of unique values. The elements can be any type that can be converted to an elm 'comparable'.

This is helpful if you have values that are really just a comparable underneath, but you want to keep them separate using the type system. For instance:

type Kilos
    = Kilos Float

type Pounds
    = Pounds Float

Create a TSet with the empty function, which takes two converter functions as arguments. It can be convenient to create a canonical empty TSet for a certain type:

emptyKiloSet =
    TSet.empty
        (\(Kilos n) -> n)
        Kilos

Then to do the equivalent of Set.fromList:

TSet.insertList emptyKiloSet mykilolist

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

Sets


type TSet k comparable

Represents a set of unique values. Create with the empty function.

Build

empty : (k -> comparable) -> (comparable -> k) -> TSet k comparable

Create an empty TSet. Requires two conversion functions: one from the key to comparable, and the other from comparable to key.

clear : TSet k comparable -> TSet k comparable

remove all values from the set.

insert : k -> TSet k comparable -> TSet k comparable

Insert a value into a set.

remove : k -> TSet k comparable -> TSet k comparable

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

Query

isEmpty : TSet k comparable -> Basics.Bool

Determine if a set is empty.

member : k -> TSet k comparable -> Basics.Bool

Determine if a value is in a set.

size : TSet k comparable -> Basics.Int

Determine the number of elements in a set.

Combine

union : TSet k comparable -> TSet k comparable -> TSet k comparable

Get the union of two sets. Keep all values.

intersect : TSet k comparable -> TSet k comparable -> TSet k comparable

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

diff : TSet k comparable -> TSet k comparable -> TSet k comparable

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

Lists

toList : TSet k comparable -> List k

Convert a set into a list, sorted from lowest to highest.

insertList : TSet k comparable -> List k -> TSet k comparable

Insert a list of values into a set.

Transform

mapInto : (k -> k2) -> TSet k comparable -> TSet k2 comparable2 -> TSet k2 comparable2

Map a function onto a set, inserting into a second set.

foldl : (k -> b -> b) -> b -> TSet k comparable -> b

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

foldr : (k -> b -> b) -> b -> TSet k comparable -> b

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

filter : (k -> Basics.Bool) -> TSet k comparable -> TSet k comparable

Create a new set consisting only of elements which satisfy a predicate.

partition : (k -> Basics.Bool) -> TSet k comparable -> ( TSet k comparable, TSet k comparable )

Create two new sets; the first consisting of elements which satisfy a predicate, the second consisting of elements which do not.