An AVL Tree based dictionary.
A dictionary mapping unique keys to values. The keys can be any type. This includes both custom and comparable types such as Int, Float, Time, Char, String, and tuples or lists of comparable types.
Insert, remove, get and member operations all take O(log n) time.
Size takes constant O(1) time.
A comparator is a function which compares two keys.
So a Dict ID User is a dictionary
that lets you look up a ID (such as user ids)
and find the associated User.
import AVL.Dict as Dict exposing (Comparator, Dict)
type ID
= ID Int
idComparator : Comparator ID
idComparator (ID x) (ID y) =
compare x y
users : Dict ID User
users =
Dict.fromListWith idComparator
[ ( "Alice", User (ID 0) "Alice" 28 1.65 )
, ( "Bob", User (ID 1) "Bob" 19 1.82 )
, ( "Chuck", User (ID 2) "Chuck" 33 1.75 )
]
alice : Maybe User
alice =
Dict.get (ID 0) users
type alias User =
{ id : ID
, name : String
, age : Int
, height : Float
}Create an empty dictionary.
Create an empty dictionary with custom key comparator.
Create a dictionary with one key-value pair.
Create a dictionary with one key-value pair with custom key comparator.
Convert an association list into a dictionary.
Convert an association list into a dictionary with custom key comparator.
Get all of the keys in a dictionary, sorted from lowest to highest.
keys (fromList [ ( 1, "Bob" ), ( 0, "Alice" ) ]) == [ 0, 1 ]Get all of the values in a dictionary, in the order of their keys.
values (fromList [ ( 1, "Bob" ), ( 0, "Alice" ) ]) == [ "Alice", "Bob" ]Convert a dictionary into an association list of key-value pairs, sorted by keys.
toList (fromList [ ( 1, "Bob" ), ( 0, "Alice" ) ]) == [ ( 0, "Alice" ), ( 1, "Bob" ) ]Insert a key-value pair into a dictionary. Replaces value when there is a collision.
Remove a key-value pair from a dictionary. If the key is not found, no changes are made.
Update the value of a dictionary for a specific key with a given function.
Remove all entries from a dictionary. Useful when you need to create new empty dictionary using same comparator.
Determine if a dictionary is empty.
Determine the number of key-value pairs in the dictionary. It takes constant time to request the size.
Determine if a key is in a dictionary.
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 == NothingApply a function to all values in a dictionary.
Keep only the key-value pairs that pass the given test.
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.
Fold over the key-value pairs in a dictionary from lowest key to highest key.
Fold over the key-value pairs in a dictionary from highest key to lowest key.
Combine two dictionaries. If there is a collision, preference is given to the left dictionary.
Keep a key-value pair when its key does not appear in the right dictionary.
Keep a key-value pair when its key appears in the right dictionary. Preference is given to values in the left dictionary.
The most general way of combining two dictionaries. You provide three accumulators for when a given key appears:
You then traverse all the keys from lowest to highest, building up whatever you want.
A dictionary of keys and values. So a
Dict String Useris a dictionary that lets you look up aString(such as user names) and find the associatedUser.