rtfeldman/elm-sorter-experiment/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

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 =
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.

store : k -> v -> Dict k v -> Dict k v

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

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.

Query

isEmpty : Dict k v -> Bool

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

size : Dict k v -> 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
member : k -> Dict k v -> Bool

Determine if a key is in a dictionary.

Transform

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

Apply a function to all values in a dictionary.

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

Keep a key-value pair when 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 -> 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.

Combine

{ from : Dict k v
, into : Dict k v
}
-> Dict k v

Take all the key-value pairs in the from dictionary and store them into the into dictionary.

{ preferred : Dict k v
, other : Dict k v
}
-> Dict k v

Keep a key-value pair when its key appears in the both the preferred and in the other dictionary.

Preference for values and Sorter is given to values in the preferred dictionary.

{ original : Dict k v
, other : Dict k v
}
-> Dict k v

Keep a key-value pair when its key appears in the original dictionary but not in the other dictionary.

The original dictionary's Sorter will be used.

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

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.