An association list is a list of tuples that map unique keys to values. The keys can be of any type (so long as it has a reasonable definition for equality). This includes pretty much everything except for functions and things that contain functions.
All functions in this module are "stack safe," which means that your program won't crash from recursing over large association lists. You can read Evan Czaplicki's document on tail-call elimination for more information about this topic.
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.
If you are using this module as an ordered dictionary, please note that if you
are replacing the value of an existing entry, the entry will remain where it
is in the insertion order. (If you do want to change the insertion order,
consider using get in conjunction with insert instead.)
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.
type Animal
= Cat
| Mouse
animals : Dict String Animal
animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
get "Tom" animals
--> Just Cat
get "Jerry" animals
--> Just Mouse
get "Spike" animals
--> NothingDetermine the number of key-value pairs in the dictionary.
size (fromList [ ( "a", 1 ), ( "b", 2 ), ( "c", 3 ) ])
--> 3
size (insert 1 "b" (singleton 1 "a"))
--> 1Compare two dictionaries for equality, ignoring insertion order. Dictionaries are defined to be equal when they have identical key-value pairs where keys and values are compared using the built-in equality operator.
You should almost never use the built-in equality operator to compare dictionaries from this module since association lists have no canonical form.
eq
(fromList [ ( "a", 1 ), ( "b", 2 ) ])
(fromList [ ( "b", 2 ), ( "a", 1 ) ])
--> TrueGet all of the keys in a dictionary, in the order that they were inserted with the most recently inserted key at the head of the list.
keys (fromList [ ( 0, "Alice" ), ( 1, "Bob" ) ])
--> [ 1, 0 ]Get all of the values in a dictionary, in the order that they were inserted with the most recently inserted value at the head of the list.
values (fromList [ ( 0, "Alice" ), ( 1, "Bob" ) ])
--> [ "Bob", "Alice" ]Convert a dictionary into an association list of key-value pairs, in the order that they were inserted with the most recently inserted entry at the head of the list.
Convert an association list into a dictionary. The elements are inserted
from left to right. (If you want to insert the elements from right to left, you
can simply call List.reverse on the input before passing it to fromList.)
Fold over the key-value pairs in a dictionary from most recently inserted to least recently inserted.
users : Dict String Int
users =
empty
|> insert "Alice" 28
|> insert "Bob" 19
|> insert "Chuck" 33
foldl (\name age result -> age :: result) [] users
--> [28,19,33]Fold over the key-value pairs in a dictionary from least recently inserted to most recently insered.
users : Dict String Int
users =
empty
|> insert "Alice" 28
|> insert "Bob" 19
|> insert "Chuck" 33
foldr (\name age result -> age :: result) [] users
--> [33,19,28]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.
If you are using this module as an ordered dictionary, the ordering of the output dictionary will be all the entries of the first dictionary (from most recently inserted to least recently inserted) followed by all the entries of the second dictionary (from most recently inserted to least recently inserted).
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 in the following order, building up whatever you want:
All the keys that appear only in the right dictionary from least recently inserted to most recently inserted.
All the keys in the left dictionary from least recently inserted to most recently inserted (without regard to whether they appear only in the left dictionary or in both dictionaries).
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.