A dictionary mapping unique keys to values. The keys can be any comparable
type. This includes Int
, Float
, Time
, Char
, String
, and tuples or
lists of comparable types.
Insert, remove, and query operations all take O(log n) time.
AssocList.Dict k v
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
.
import Dict exposing (Dict)
users : Dict String User
users =
Dict.fromList
[ ( "Alice", User "Alice" 28 1.65 )
, ( "Bob", User "Bob" 19 1.82 )
, ( "Chuck", User "Chuck" 33 1.75 )
]
type alias User =
{ name : String
, age : Int
, height : Float
}
empty : Dict k v
Create an empty dictionary.
singleton : comp -> v -> Dict comp v
Create a dictionary with one key-value pair.
insert : comp -> v -> Dict comp v -> Dict comp v
Insert a key-value pair into a dictionary. Replaces value when there is a collision.
update : comp -> (Maybe v -> Maybe v) -> Dict comp v -> Dict comp v
Update the value of a dictionary for a specific key with a given function.
remove : comp -> Dict comp v -> Dict comp v
Remove a key-value pair from a dictionary. If the key is not found, no changes are made.
isEmpty : Dict k v -> Basics.Bool
Determine if a dictionary is empty.
isEmpty empty == True
member : comp -> Dict comp v -> Basics.Bool
Determine if a key is in a dictionary.
get : comp -> Dict comp 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
size : Dict k v -> Basics.Int
Determine the number of key-value pairs in the dictionary.
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 : List ( comp, v ) -> Dict comp v
Convert an association list into a dictionary.
map : (k -> a -> b) -> Dict k a -> Dict k b
Apply a function to all values in a dictionary.
foldl : (k -> v -> b -> b) -> b -> Dict k v -> b
Fold over the key-value pairs in a dictionary from lowest key to highest key.
import Dict exposing (Dict)
getAges : Dict String User -> List String
getAges users =
Dict.foldl addAge [] users
addAge : String -> User -> List String -> List String
addAge _ user ages =
user.age :: ages
-- getAges users == [33,19,28]
foldr : (k -> v -> b -> b) -> b -> Dict k v -> b
Fold over the key-value pairs in a dictionary from highest key to lowest key.
import Dict exposing (Dict)
getAges : Dict String User -> List String
getAges users =
Dict.foldr addAge [] users
addAge : String -> User -> List String -> List String
addAge _ user ages =
user.age :: ages
-- getAges users == [28,19,33]
filter : (comp -> v -> Basics.Bool) -> Dict comp v -> Dict comp v
Keep only the key-value pairs that pass the given test.
partition : (comp -> v -> Basics.Bool) -> Dict comp v -> ( Dict comp v, Dict comp v )
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.
union : Dict comp v -> Dict comp v -> Dict comp v
Combine two dictionaries. If there is a collision, preference is given to the first dictionary.
intersect : Dict comp v -> Dict comp v -> Dict comp v
Keep a key-value pair when its key appears in the second dictionary. Preference is given to values in the first dictionary.
diff : Dict comp a -> Dict comp b -> Dict comp a
Keep a key-value pair when its key does not appear in the second dictionary.
merge : (comp -> a -> result -> result) -> (comp -> a -> b -> result -> result) -> (comp -> b -> result -> result) -> Dict comp a -> Dict comp b -> result -> result
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.