A dictionary mapping unique keys to values. Keys can be any type.
A comparison function k -> k -> Order is used to compare keys, this should uniquely identify keys.
Insert, remove, and query operations all take O(log n) time.
Insert a key-value pair into a dictionary. Replaces value when there is a collision.
Update the value of a dictionary for a specific key with a given function.
Remove a key-value pair from a dictionary. If the key is not found, no changes are made.
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.
import DictAny as Dict exposing (Dict)
type Animal = Cat | Dog | Mouse
animalToInt : Animal -> Int
animalToInt animal =
case animal of
Cat -> 0
Dog -> 1
Mouse -> 2
comparer : Animal -> Animal -> Order
comparer a b = compare (animalToInt a) (animalToInt b)
animals : Dict Animal String
animals = [ (Cat, "Tom"), (Mouse, "Jerry") ]
|> fromList comparer
animals |> Dict.get comparer Cat --> Just "Tom"
animals |> Dict.get comparer Dog --> Nothing
animals |> Dict.get comparer Mouse --> Just "Jerry"Get all of the keys in a dictionary, sorted from lowest to highest.
keys (fromList compare [ ( 0, "Alice" ), ( 1, "Bob" ) ])
--> [0,1]Get all of the values in a dictionary, in the order of their keys.
values
(fromList compare [ ( 0, "Alice" ), ( 1, "Bob" ) ])
--> ["Alice", "Bob"]Convert a dictionary into an association list of key-value pairs, sorted by keys.
Convert an association list into a dictionary.
Fold over the key-value pairs in a dictionary from lowest key to highest key.
import DictAny as Dict exposing (Dict)
type alias User = { age : Int }
getAges : Dict String User -> List Int
getAges dict =
dict |> Dict.foldl consAge []
consAge : String -> User -> List Int -> List Int
consAge _ user ages =
user.age :: ages
users : Dict String User
users =
[ ("Fred", {age=33})
, ("Bert", {age=19})
, ("Adam", {age=42})
] |> Dict.fromList compare
getAges users --> [33,19,42]Fold over the key-value pairs in a dictionary from highest key to lowest key.
import DictAny as Dict exposing (Dict)
type alias User = { age : Int }
getAges : Dict String User -> List Int
getAges dict =
dict |> Dict.foldr consAge []
consAge : String -> User -> List Int -> List Int
consAge _ user ages =
user.age :: ages
users : Dict String User
users =
[ ("Fred", {age=33})
, ("Bert", {age=19})
, ("Adam", {age=42})
] |> Dict.fromList compare
getAges users --> [42,19,33]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.
Combine two dictionaries. If there is a collision, preference is given to the first dictionary.
Keep a key-value pair when its key appears in the second dictionary. Preference is given to values in the first dictionary.
Keep a key-value pair when its key does not appear in the second 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 Key Valueis a dictionary that lets you look up a unique Key and find the associated Value.