joneshf / elm-tagged / Tagged.Set

A module that allows tagging sets, while maintaining an API parallel to Set.

A common idea is wanting to store a value that is not comparable in Set a. Since we can't currently do that there are many different ways to address the problem. One way to solve that problem is to use a type level assertion.

Rather than holding on to an entirely different type for the values and threading a comparison function through, we can just tell elm that we'd like to tag the Set a at compile time. Doing so allows us to reuse the underlying behavior of the Set a with very little runtime overhead. Most functions here are simple wrappers to refine the types without modifying the values.


type alias TaggedSet tag comparable =
Tagged tag (Set comparable)

A set that tags the values with an additional constraint.

The constraint is phantom in that it doesn't show up at runtime.

Build

empty : TaggedSet tag comparable

Create an empty set.

singleton : Tagged tag comparable -> TaggedSet tag comparable

Create a set with one value.

insert : Tagged tag comparable -> TaggedSet tag comparable -> TaggedSet tag comparable

Insert a value pair into a set.

remove : Tagged tag comparable -> TaggedSet tag comparable -> TaggedSet tag comparable

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

Query

isEmpty : TaggedSet tag c -> Basics.Bool

Determine if a set is empty.

member : Tagged tag comparable -> TaggedSet tag comparable -> Basics.Bool

Determine if a value is in a set.

size : TaggedSet tag c -> Basics.Int

Determine the number of values in a set.

Lists

toUntaggedList : TaggedSet tag comparable -> List comparable

Convert a set into a sorted list of untagged values.

fromUntaggedList : List comparable -> TaggedSet tag comparable

Convert an untagged list into a set.

toList : TaggedSet tag comparable -> List (Tagged tag comparable)

Convert a set into a sorted list of tagged values.

fromList : List (Tagged tag comparable) -> TaggedSet tag comparable

Convert a list into a set.

Transform

map : (Tagged tag comparable -> comparable2) -> TaggedSet tag comparable -> TaggedSet tag comparable2

Apply a function to all values in a set.

foldl : (Tagged tag comparable -> b -> b) -> b -> TaggedSet tag comparable -> b

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

foldr : (Tagged tag comparable -> b -> b) -> b -> TaggedSet tag comparable -> b

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

filter : (Tagged tag comparable -> Basics.Bool) -> TaggedSet tag comparable -> TaggedSet tag comparable

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

partition : (Tagged tag comparable -> Basics.Bool) -> TaggedSet tag comparable -> ( TaggedSet tag comparable, TaggedSet tag comparable )

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

Combine

union : TaggedSet tag comparable -> TaggedSet tag comparable -> TaggedSet tag comparable

Get the union of two sets. Keep all values.

intersect : TaggedSet tag comparable -> TaggedSet tag comparable -> TaggedSet tag comparable

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

diff : TaggedSet tag comparable -> TaggedSet tag comparable -> TaggedSet tag comparable

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