A dictionary mapping unique keys to values. Similar and based on Dict but without restriction on comparable keys.
Insert, remove, and query operations all take O(log n) time.
When writing a function for conversion from the type you want to use for keys to comparable it's very important to make sure every distinct member of type k produces different value in set o of comparables.
Take for instance those two examples:
We can use Bool as a key for our Dict (No matter how unpractical it might seem)
boolToInt : Bool -> Int
boolToInt bool =
case bool of
False -> 0
True -> 1
empty boolToInt
|> insert True "foo"
|> get True
--> Just "foo"or Maybe String.
comparableKey : Maybe String -> (Int, String)
comparableKey maybe =
case maybe of
Nothing -> (0, "")
Just str -> (1, str)
empty comparableKey
|> insert (Just "foo") 42
|> get (Just "foo")
--> Just 42Note that we give Int code to either constructor and in Case of Nothing we default to "" (empty string).
There is still a difference between Nothing and Just "" (Int value in the pair is different).
In fact, you can "hardcode" any value as the second member of the pair
in case of nothing but empty string seems like a reasonable option for this case.
Generally, this is how I would implement toComparable function for most of your custom data types.
Have a look at the longest constructor,
Define tuple where the first key is int (number of the constructor)
and other are types within the constructor and you're good to go.
Create an empty dictionary by suppling function used for comparing keys.
Note that it's important to make sure every key is turned to different comparable. Otherwise keys would conflict and overwrite each other.
Create a dictionary with one key-value pair.
Note that it's important to make sure every key is turned to different comparable. Otherwise keys would conflict and overwrite each other.
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.
Remove all entries from AnyDict.
Useful when you need to create new empty AnyDict using same comparable function for key type.
Determine if a dictionary is empty.
isEmpty (empty identity)
--> True
singleton 1 "foo" identity
|> isEmpty
--> FalseGet 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 | Dog
animalToInt : Animal -> Int
animalToInt animal =
case animal of
Cat -> 0
Mouse -> 1
Dog -> 2
animals : AnyDict Int Animal String
animals =
[ (Cat, "Tom"), (Mouse, "Jerry") ]
|> fromList animalToInt
get Cat animals
-> Just "Tom"
get Mouse animals
--> Just "Jerry"
get Dog animals
--> NothingGet a key associated with key.
This is useful in case of AnyDict because
some parts of a key might not be used
for generating comparable.
This function allows quering AnyDict with old
key to obtain updated one in such cases.
Get all of the keys in a dictionary, sorted from lowest to highest.
Get all of the values in a dictionary, in the order of their keys.
Convert a dictionary into an association list of key-value pairs, sorted by keys.
Convert an association list into a dictionary.
Note that it's important to make sure every key is turned to different comparable. Otherwise keys would conflict and overwrite each other.
Apply a function to all values in a dictionary.
Fold over the key-value pairs in a dictionary, in order from lowest key to highest key.
Fold over the key-value pairs in a dictionary, in order from highest key to lowest key.
Keep a key-value pair when it satisfies a predicate.
Partition a dictionary according to a predicate. The first dictionary contains all key-value pairs which satisfy the predicate, and the second contains the rest.
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:
Only in the left dictionary. In both dictionaries. Only in the right dictionary.
Convert AnyDict to plain dictionary with comparable keys.
Decode a JSON object into an AnyDict.
Be aware that AnyDict stores a function internally.
If you want to use
(==)for comparing two AnyDicts use toDict function to convert them to regularDictfirst.