eeue56/elm-all-dict/2.0.0

NOTE: this package is not compatible with Elm 0.19.1

AllDict

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.

AllDict allows you to provide a custom operation for hashing keys. The ord function has a type signature of k -> comparable.

It's possible to define the ord function in such a way that AllDict can actually function as a DefaultDict.

An example of this might be

ord : Action -> Int
ord action =
  case action of
    Run -> 0
    Hide -> 1
    StandStill -> 2
    _ -> 3

This will default any Action in the dictionary to 3. You can then set a default like so:


myDict : AllDict Action String Int
myDict =
  AllDict.fromList
    ord
    [(Noop, "Do nothing")]

-- equal to `"Do nothing"`
-- as `ord` maps anything outside of Run, Hide, StandStill
-- to the same hash
eat =
  myDict |>
    AllDict.get Eat

Insert, remove, and query operations all take O(log n) time. AllDictionary equality with (==) is unreliable and should not be used. Instead, use AllDict.eq for element-wise comparisons, and AllDict.fullEq for a full comparison

Types

type AllDict k v comparable

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

Build

empty : (k -> comparable) -> AllDict k v comparable

Create an empty dictionary using a given ord function to calculate hashes

singleton : (k -> comparable) -> k -> v -> AllDict k v comparable

Create a dictionary with one key-value pair.

insert : k -> v -> AllDict k v comparable -> AllDict k v comparable

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

k
-> (Maybe v -> Maybe v)
-> AllDict k v comparable
-> AllDict k v comparable

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

remove : k -> AllDict k v comparable -> AllDict k v comparable

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

Query

isEmpty : AllDict k v comparable -> Bool

Determine if a dictionary is empty.

isEmpty empty == True
member : k -> AllDict k v comparable -> Bool

Determine if a key is in a dictionary.

get : k -> AllDict k v comparable -> 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 : AllDict k v comparable -> Int

Get the number of key-value pairs in a dict

getOrd : AllDict k v comparable -> k -> comparable

Helper function for grabbing the default value used in the dict

eq : AllDict k v comparable -> AllDict k v comparable -> Bool

Element equality. Does not check equality of base

fullEq : AllDict k v comparable -> AllDict k v comparable -> Bool

Base + element equality

Combine

AllDict k v comparable
-> AllDict k v comparable
-> AllDict k v comparable

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

AllDict k v comparable
-> AllDict k v comparable
-> AllDict k v comparable

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

AllDict k v comparable
-> AllDict k v comparable
-> AllDict k v comparable

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

Lists

keys : AllDict k v comparable -> List k

Get all of the keys in a dictionary.

values : AllDict k v comparable -> List v

Get all of the values in a dictionary.

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

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

fromList : (k -> comparable) -> List ( k, v ) -> AllDict k v comparable

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

Transform

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

Apply a function to all values in a dictionary.

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

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

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

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

filter : (k -> v -> Bool) -> AllDict k v comparable -> AllDict k v comparable

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

(k -> v -> Bool)
-> AllDict k v comparable
-> ( AllDict k v comparable, AllDict k v comparable )

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.