A dictionary mapping unique keys to values. The keys can be any type that can be transformed to a String.
Create an empty opaque dictionary providing a function that transforms a key type to a string.
type UserId
= UserId Int
userIdToString : UserId -> String
userIdToString (UserId id) =
id |> String.fromInt
-- OpaqueDict.empty userIdToString == [] OpaqueDict.fromList userIdToStringCreate a dictionary with one key-value pair.
singleton userIdToString (UserId 1) "Bob"
-- == ([ ( UserId 1, "Bob" ) ]
-- |> fromList userIdToString
-- )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.
Determine whether the opaque 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. This is useful when you are not sure if a key will be in the
dictionary.
animals = fromList animalFamilyToString [ (Feline, Cat), (Rodent, Mouse) ]
get Feline animals == Just Cat
get Rodent animals == Just Mouse
get Canine animals == NothingDetermine the number of key-value pairs in the dictionary.
Get all of the keys in a dictionary, sorted from lowest to highest by their string form.
Get the dictionary values.
Convert a dictionary into an association list of key-value pairs, sorted by keys.
Make the opaque dictionary from a list.
Apply a function to all values in a dictionary.
Fold over the key-value pairs in a dictionary from lowest key to highest key.
getAges : OpaqueDict UserId User -> List Int
getAges users =
OpaqueDict.foldl addAge [] users
addAge : UserId -> User -> List UserId -> List Int
addAge _ user ages =
user.age :: ages
-- getAges users == [33,19,28]Fold over the key-value pairs in a dictionary from highest key to lowest key.
getAges : OpaqueDict UserId User -> List Int
getAges users =
OpaqueDict.foldr addAge [] users
addAge : UserId -> User -> List UserId -> List Int
addAge _ user ages =
user.age :: ages
-- getAges users == [28,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:
Only in the left dictionary.
In both dictionaries.
Only in the right dictionary. You then traverse all the keys from lowest to highest, building up whatever you want.
Decode a JSON object to an OpaqueDict, analogous Json.Decode.dict.
decodeString (decode animalGroupFromString animalGroupToString int) "{ \"feline\": 3, \"canine\": 5 }"
== Ok (OpaqueDict.fromList animalGroupToString [ ( Feline, 3 ), ( Canine, 5 ) ])If you need the keys (like "feline" and "canine") available in the OpaqueDict
values as well, you can use a (private) intermediate data structure like
Info in this example:
import OpaqueDict exposing (OpaqueDict)
import Json.Decode exposing (..)
type AnimalGroup
= Feline
| Canine
| Rodent
{-| Get the string form of an animal group.
-}
animalGroupToString : AnimalGroup -> String
animalGroupToString group =
case group of
Feline ->
"feline"
Canine ->
"canine"
Rodent ->
"rodent"
{-| Maybe get an animal group from a string.
-}
animalGroupFromString : String -> Maybe AnimalGroup
animalGroupFromString groupName =
case groupName of
"feline" ->
Just Feline
"canine" ->
Just Canine
"rodent" ->
Just Rodent
_ ->
Nothing
decoder : Decoder (OpaqueDict AnimalGroup Int)
decoder =
Json.Decode.map (OpaqueDict.map infoToUser) (OpaqueDict.decode Json.Decode.int)
decodeString (decode animalGroupFromString animalGroupToString int) "{ \"feline\": 3, \"canine\": 5 }"
== Ok (OpaqueDict.fromList animalGroupToString [ ( Feline, 3 ), ( Canine, 5 ) ])Turn an OpaqueDict into a JSON object. Analogous to Json.Encode.dict.
animalCounts : OpaqueDict AnimalGroup Int
animalCounts =
[ ( Canine, 5 )
, ( Feline, 3 )
]
|> fromList animalGroupToString
Encode.encode 0 (OpaqueDict.encode animalGroupToString Encode.int animalCounts)
== "{\"canine\":5,\"feline\":3}"
A dictionary of keys and values. So a
OpaqueDict UserId Useris a dictionary that lets you look up aUserId(such as a user Id) and find the associatedUser. import OpaqueDict exposing (OpaqueDict)