edkv / elm-generic-dict / GenericDict


type Dict k v

A dictionary of keys and values. Keys can be any type.

Build

empty : Dict k v

Create an empty dictionary.

singleton : (k -> String) -> k -> v -> Dict k v

Create a dictionary with one key-value pair.

insert : (k -> String) -> k -> v -> Dict k v -> Dict k v

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

update : (k -> String) -> k -> (Maybe v -> Maybe v) -> Dict k v -> Dict k v

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

remove : (k -> String) -> k -> Dict k v -> Dict k v

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

Query

isEmpty : Dict k v -> Basics.Bool

Determine if a dictionary is empty.

member : (k -> String) -> k -> Dict k v -> Basics.Bool

Determine if a key is in a dictionary.

get : (k -> String) -> k -> Dict k v -> Maybe v

Get the value associated with a key. Returns Nothing if the key is not found.

size : Dict k v -> Basics.Int

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

Lists

keys : Dict k v -> List k

Get all of the keys in a dictionary.

values : Dict k v -> List v

Get all of the values in a dictionary.

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

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

fromList : (k -> String) -> List ( k, v ) -> Dict k v

Convert an association list into a dictionary.

Transform

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

Apply a function to all values in a dictionary.

fold : (k -> v -> b -> b) -> b -> Dict k v -> b

Fold over the key-value pairs in a dictionary.

filter : (k -> v -> Basics.Bool) -> Dict k v -> Dict k v

Keep only the key-value pairs that pass the given test.

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

Partition a dictionary according to some test. The first dictionary contains all key-value pairs which passed the test, and the second contains the pairs that did not.

Combine

union : Dict k v -> Dict k v -> Dict k v

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

intersect : Dict k v -> Dict k v -> Dict 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 : Dict k v -> Dict k v -> Dict k 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) -> Dict k a -> Dict k 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.