A dictionary mapping unique keys to values while remembering their
insertion order. The keys can be any comparable type. This includes
Int, Float, Time, Char, String, and tuples or lists
of comparable types.
The insertion order is reflected in the functions under "Conversions" and "Transform". The order in the lists and the iteration order will be the order of insertion.
The API mirrors the core
Dict's
API, with exception for the functions listed there under
"Combine",
because these functions do not have an obvious way to handle
the order between the combined dictionaries.
Create an empty dictionary.
Create a dictionary with one key-value pair.
Insert a key-value pair into a dictionary. If the key already exists, the old value will be forgotten and the new value will be inserted at the end.
import OrderedDict
OrderedDict.empty
|> OrderedDict.insert 1 "one"
|> OrderedDict.insert 2 "two"
|> OrderedDict.insert 1 "three"
|> OrderedDict.toList
--> [ (2, "two"), (1, "three") ]Update the value of a dictionary for a specific key with a given function.
If a value exists for the key and the passed function returns a Just v,
the value will be replaced, keeping its order. If it did not exist, the
new value will be added to the end.
When the function returns a Nothing, the key is removed.
import OrderedDict
OrderedDict.empty
|> OrderedDict.insert 1 "one"
|> OrderedDict.insert 2 "two"
|> OrderedDict.update 1 (\_ -> Just "three")
|> OrderedDict.toList
--> [ (1, "three"), (2, "two") ]
OrderedDict.singleton 1 "one"
|> OrderedDict.update 2 (\_ -> Just "two")
|> OrderedDict.toList
--> [ (1, "one"), (2, "two") ]
OrderedDict.singleton 1 "one"
|> OrderedDict.update 1 (\_ -> Nothing)
|> OrderedDict.toList
--> []Remove a key-value pair from a dictionary. If the key is not found, no changes are made.
Determine if a dictionary is empty.
Determine if a key is in a dictionary.
Get the value associated with a key. If the key is not found, return Nothing.
Determine the number of key-value pairs in the dictionary.
Get all of the keys in a dictionary, in insertion order.
Get all of the values in a dictionary, in insertion order.
Convert a dictionary into an association list of key-value pairs, in insertion order.
Convert an association list into a dictionary.
If a key appears multiple times in the list, only the last occurrence is kept.
Convert an ordered dictionary into a regular
Dict.
Apply a function to all values in a dictionary.
Fold over the key-value pairs in a dictionary in insertion order.
Fold over the key-value pairs in a dictionary in reverse insertion order.
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.
The order will be preserved in these new dictionaries in the sense that elements that are inserted after each other will remain ordered after each other.
A dictionary of keys and values that remembers the order of insertion.