rtfeldman / elm-sorter-experiment / Sort.Dict

A dictionary mapping unique keys to values.

Insert, remove, and query operations all take O(log n) time.

This implementation is based on Skinney/elm-dict-exploration, except that it uses Sorter instead of comparable, so it permits keys that are not comparable.

Dictionaries


type alias Dict key value =
Internal.Dict.Dict key value

A dictionary of keys and values. So a (Dict String User) is a dictionary that lets you look up a String (such as user names) and find the associated User.

Build

empty : Sorter k -> Dict k v

Create an empty dictionary.

singleton : Sorter k -> k -> v -> Dict k v

Create a dictionary with one key-value pair.

insert : k -> v -> Dict k v -> Dict k v

Insert a key-value pair in a dictionary. Replaces value when that key is already present.

insertAll : Dict k v -> Dict k v -> Dict k v

Take all the key-value pairs in the first dictionary and insert them into the second dictionary.

remove : 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.

update : 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. The given function gets the current value as a parameter and its return value determines if the value is updated or removed. New key-value pairs can be stored too.

merge : Sorter k -> (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 first dictionary
  2. In both dictionaries
  3. Only in the second dictionary

You then traverse all the keys, building up whatever you want.

The given Sorter will be used to compare the keys.

Query

isEmpty : Dict k v -> Basics.Bool

Determine if a dictionary is empty. isEmpty empty == True

size : Dict k v -> Basics.Int

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

get : k -> Dict 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 "Jerry" animals == Just Mouse
get "Spike" animals == Nothing

memberOf : Dict k v -> k -> Basics.Bool

Return True if the given key is in the given dictionary.

eq : Dict k v -> Dict k v -> Basics.Bool

Check if two dictionaries are equal.

Transform

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

Apply a function to all values in a dictionary.

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

Keep a key-value pair only if it satisfies a predicate.

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

Remove a key-value pair if it satisfies a predicate.

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

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

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

Lists

keys : Dict k v -> List k

Get all of the keys in a dictionary, sorted from lowest to highest. keys (fromList [(0,"Alice"),(1,"Bob")]) == [0,1]

values : Dict k v -> List v

Get all of the values in a dictionary, in the order of their keys. values (fromList [(0,"Alice"),(1,"Bob")]) == ["Alice", "Bob"]

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

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

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

Convert an association list into a dictionary.