joneshf/elm-tagged/1.1.0

NOTE: this package is not compatible with Elm 0.19.1

Tagged.Dict

A module that allows tagging dictionaries, while maintaining an API parallel to Dict.

A common idea is wanting to use a value that is not comparable as the key of a Dict a b. 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 in the keys and threading a comparison function through, we can just tell elm that we'd like to tag the Dict a b at compile time. Doing so allows us to reuse the underlying behavior of the Dict a b with very little runtime overhead. Most functions here are simple wrappers to refine the types without modifying the values.

type alias TaggedDict a b c =
Tagged a (Dict b c)

A dictionary that tags the keys with an additional constraint.

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

Build

empty : TaggedDict k comparable v

Create an empty dictionary.

singleton : Tagged k comparable -> v -> TaggedDict k comparable v

Create a dictionary with one key-value pair.

Tagged k comparable
-> v
-> TaggedDict k comparable v
-> TaggedDict k comparable v

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

Query

Tagged k comparable
-> (Maybe v -> Maybe v)
-> TaggedDict k comparable v
-> TaggedDict k comparable v

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

Tagged k comparable
-> TaggedDict k comparable v
-> TaggedDict k comparable v

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

isEmpty : TaggedDict k c v -> Bool

Determine if a dictionary is empty.

member : Tagged k comparable -> TaggedDict k comparable v -> Bool

Determine if a key is in a dictionary.

get : Tagged k comparable -> TaggedDict k comparable v -> Maybe v

Get the value associated with a key.

size : TaggedDict k c v -> Int

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

Lists

untaggedKeys : TaggedDict k comparable v -> List comparable

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

keys : TaggedDict k comparable v -> List (Tagged k comparable)

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

values : TaggedDict k comparable v -> List v

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

toUntaggedList : TaggedDict k comparable v -> List ( comparable, v )

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

fromUntaggedList : List ( comparable, v ) -> TaggedDict k comparable v

Convert an untagged association list into a dictionary.

toList : TaggedDict k comparable v -> List ( Tagged k comparable, v )

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

fromList : List ( Tagged k comparable, v ) -> TaggedDict k comparable v

Convert an association list into a dictionary.

Transform

map :
(Tagged k comparable -> a -> b)
-> TaggedDict k comparable a
-> TaggedDict k comparable b

Apply a function to all values in a dictionary.

foldl : (Tagged k comparable -> v -> b -> b) -> b -> TaggedDict k comparable v -> b

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

foldr : (Tagged k comparable -> v -> b -> b) -> b -> TaggedDict k comparable v -> b

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

(Tagged k comparable -> v -> Bool)
-> TaggedDict k comparable v
-> TaggedDict k comparable v

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

(Tagged k comparable -> v -> Bool)
-> TaggedDict k comparable v
-> ( TaggedDict k comparable v, TaggedDict 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

TaggedDict k comparable v
-> TaggedDict k comparable v
-> TaggedDict k comparable v

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

TaggedDict k comparable v
-> TaggedDict k comparable v
-> TaggedDict 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.

TaggedDict k comparable v
-> TaggedDict k comparable v
-> TaggedDict k comparable v

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

(Tagged k comparable -> a -> result -> result)
-> (Tagged k comparable -> a -> b -> result -> result)
-> (Tagged k comparable -> b -> result -> result)
-> TaggedDict k comparable a
-> TaggedDict k comparable b
-> result
-> result

The most general way of combining two dictionaries.