bburdette / typed-collections / TDict

A dictionary mapping unique keys to values. The keys can be any type that can be converted to an elm 'comparable'.

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

type TagId
    = TagId Int

type CustomerId
    = CustomerId Int

emptyTagDict =
    TDict.empty (\(TagId id) -> id) TagId String

emptyCustomerDict =
    TDict.empty (\(CustomerId id) -> id) CustomerId String

So inserting a (tag,string) into an emptyTagDict works:

TDict.insert ( TagId 1, "some string of interest" ) emptyTagDict

But inserting a (tag,string) into an emptyCustomerDict doesn't:

TDict.insert ( TagId 1, "some string of interest" ) emptyCustomerDict

Dictionaries


type TDict k comparable val

A dictionary of keys and values. Create a TDict with the empty function.

Build

empty : (k -> comparable) -> (comparable -> k) -> TDict k comparable val

Create an empty dictionary of keys and values. Requires two conversion functions: one from the key to comparable, and the other from comparable to key.

clear : TDict k comparable val -> TDict k comparable val

Remove all elements from the TDict

insert : k -> v -> TDict k comparable v -> TDict k comparable v

Insert a key-value pair into a dictionary. Replaces value when there is a collision.

update : k -> (Maybe v -> Maybe v) -> TDict k comparable v -> TDict k comparable v

Update the value of a dictionary for a specific key with a given function.

remove : k -> TDict k comparable val -> TDict k comparable val

Remove a key-value pair from a dictionary. If the key is not found, no changes are made.

Query

isEmpty : TDict k comparable val -> Basics.Bool

Determine if a dictionary is empty.

member : k -> TDict k comparable v -> Basics.Bool

Determine if a key is in a dictionary.

get : k -> TDict k comparable v -> Maybe v

Get the value associated with a key. If the key is not found, return Nothing. This is useful when you are not sure if a key will be in the dictionary.

size : TDict k comparable v -> Basics.Int

Determine the number of key-value pairs in the dictionary.

Lists

keys : TDict k comparable v -> List k

Get all of the keys in a dictionary, sorted from lowest to highest.

values : TDict k comparable v -> List v

Get all of the values in a dictionary, in the order of their keys.

toList : TDict k comparable v -> List ( k, v )

Convert a dictionary into an association list of key-value pairs, sorted by keys.

insertList : TDict k comparable v -> List ( k, v ) -> TDict k comparable v

Merge an association list into a dictionary.

Transform

map : (k -> a -> b) -> TDict k comparable a -> TDict k comparable b

Apply a function to all values in a dictionary.

foldl : (k -> v -> b -> b) -> b -> TDict k comparable v -> b

Fold over the key-value pairs in a dictionary, in order from lowest key to highest key.

foldr : (k -> v -> b -> b) -> b -> TDict k comparable v -> b

Fold over the key-value pairs in a dictionary, in order from highest key to lowest key.

filter : (k -> v -> Basics.Bool) -> TDict k comparable v -> TDict k comparable v

Keep a key-value pair when it satisfies a predicate.

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

Partition a dictionary according to a predicate. The first dictionary contains all key-value pairs which satisfy the predicate, and the second contains the rest.

Combine

union : TDict k comparable v -> TDict k comparable v -> TDict k comparable v

Combine two dictionaries. If there is a collision, preference is given to the first dictionary.

intersect : TDict k comparable v -> TDict k comparable v -> TDict k comparable v

Keep a key-value pair when its key appears in the second dictionary. Preference is given to values in the first dictionary.

diff : TDict k comparable v -> TDict k comparable v -> TDict k comparable v

Keep a key-value pair when its key does not appear in the second dictionary.

merge : (k -> a -> result -> result) -> (k -> a -> b -> result -> result) -> (k -> b -> result -> result) -> TDict k comparable a -> TDict k comparable b -> result -> result

The most general way of combining two dictionaries. You provide three accumulators for when a given key appears:

  1. Only in the left dictionary.
  2. In both dictionaries.
  3. Only in the right dictionary.

You then traverse all the keys from lowest to highest, building up whatever you want.