eeue56/elm-all-dict/2.0.1

NOTE: this package is not compatible with Elm 0.19.1

EveryDict

A dictionary mapping unique keys to values. This dictionary can use any type as a key. In the core-provided Dict, keys can only be comparable.

It uses a custom toString as a hashing function - so don't use it on any types which have non-unique representations in strings!

It's hard to imagine many cases where you would want a function as a key, but it's now possible.

Insert, remove, and query operations all take O(log n) time. EveryDict equality with (==) is unreliable and should not be used.

Types

type EveryDict k v

A dict which works with any type which lifts the type from Core's Dict

Build

empty : EveryDict k v

Create an empty dictionary

singleton : k -> v -> EveryDict k v

Create a dictionary with one key-value pair.

insert : k -> v -> EveryDict k v -> EveryDict k v

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

update : k -> (Maybe v -> Maybe v) -> EveryDict k v -> EveryDict k v

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

remove : k -> EveryDict k v -> EveryDict k v

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

Query

isEmpty : EveryDict k v -> Bool

Determine if a dictionary is empty.

isEmpty empty == True
member : k -> EveryDict k v -> Bool

Determine if a key is in a dictionary.

get : k -> EveryDict k 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.

animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]


get "Tom"   animals == Just Cat
get "Mouse" animals == Just Mouse
get "Spike" animals == Nothing
size : EveryDict k v -> Int

Get the number of key-value pairs in a dict

eq : EveryDict k v -> EveryDict k v -> Bool

Element equality

Combine

union : EveryDict k v -> EveryDict k v -> EveryDict k v

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

intersect : EveryDict k v -> EveryDict k v -> EveryDict k v

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

diff : EveryDict k v -> EveryDict k v -> EveryDict k v

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

Lists

keys : EveryDict k v -> List k

Get all of the keys in a dictionary.

values : EveryDict k v -> List v

Get all of the values in a dictionary.

toList : EveryDict k v -> List ( k, v )

Convert a dictionary into an association list of key-value pairs.

fromList : List ( k, v ) -> EveryDict k v

Convert an association list into a dictionary. Takes a default value, and a list of key-pair tuples

Transform

map : (k -> a -> b) -> EveryDict k a -> EveryDict k b

Apply a function to all values in a dictionary.

foldl : (k -> v -> b -> b) -> b -> EveryDict k 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 -> EveryDict k v -> b

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

filter : (k -> v -> Bool) -> EveryDict k v -> EveryDict k v

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

partition : (k -> v -> Bool) -> EveryDict k v -> ( EveryDict k v, EveryDict k 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.