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
compareID : Comparator ID
compareID (ID x) (ID y) =
compare x y
users : Dict ID User
users =
Dict.fromListWith compareID
[ ( ID 0, User (ID 0) "Alice" 28 1.65 )
, ( ID 1, User (ID 1) "Bob" 19 1.82 )
, ( ID 2, 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 with comparable keys.
Create an empty dictionary with custom keys.
Create a dictionary with one comparable key-value pair.
Create a dictionary with one custom key-value pair.
Convert an association list into a dictionary with comparable keys.
Convert an association list into a dictionary with custom keys.
Get all of the keys in a dictionary, sorted from lowest to highest.
Dict.keys (Dict.fromList [ ( 1, "Bob" ), ( 0, "Alice" ) ]) == [ 0, 1 ]Get all of the values in a dictionary, in the order of their keys.
Dict.values (Dict.fromList [ ( 1, "Bob" ), ( 0, "Alice" ) ]) == [ "Alice", "Bob" ]Convert a dictionary into an association list of key-value pairs, sorted by keys.
Dict.toList (Dict.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 determine 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 =
Dict.fromList [ ( "Tom", Cat ), ( "Jerry", Mouse ) ]
Dict.get "Tom" animals == Just Cat
Dict.get "Jerry" animals == Just Mouse
Dict.get "Spike" animals == NothingGet the key-value pair associated with minimum key. If Dict is empty return Nothing.
import AVL.Dict as Dict exposing (Dict)
type alias User =
{ name : String
, age : Int
, height : Float
}
users : Dict String User
users =
Dict.fromList
[ ( "Bob", User "Bob" 19 1.82 )
, ( "Alice", User "Alice" 28 1.65 )
, ( "Chuck", User "Chuck" 33 1.75 )
]
Dict.minimum users == Just ( "Alice", User "Alice" 28 1.65 )Get the key-value pair associated with maximum key. If Dict is empty return Nothing.
import AVL.Dict as Dict exposing (Dict)
type alias User =
{ name : String
, age : Int
, height : Float
}
users : Dict String User
users =
Dict.fromList
[ ( "Bob", User "Bob" 19 1.82 )
, ( "Alice", User "Alice" 28 1.65 )
, ( "Chuck", User "Chuck" 33 1.75 )
]
Dict.maximum users == Just ( "Chuck", User "Chuck" 33 1.75 )Apply 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.