Convenience functions for working with Dict
Create a dictionary from a list of values, by passing a function that can get a key from any such value.
If the function does not return unique keys, earlier values are discarded.
This can, for instance, be useful when constructing Dicts from a List of records with id fields:
mary = {id=1, name="Mary"}
jack = {id=2, name="Jack"}
jill = {id=1, name="Jill"}
fromListBy .id [mary, jack, jill] == Dict.fromList [(1, jack), (2, jill)]Remove elements which satisfies the predicate.
dict = Dict.fromList [("Mary", 1), ("Jack", 2), ("Jill", 1)]
removeWhen (\_ v -> v == 1) dict == Dict.fromList [("Jack", 2)]Remove a key-value pair if its key appears in the set.
dict = Dict.fromList [("Mary", 1), ("Jack", 2), ("Jill", 1)]
removeMany (Set.fromList ["Mary", "Jill"]) dict == Dict.fromList [("Jack", 2)]Keep a key-value pair if its key appears in the set.
dict = Dict.fromList [("Mary", 1), ("Jack", 2), ("Jill", 1)]
removeMany (Set.fromList ["Jack"]) dict == Dict.fromList [("Jack", 2)]Apply a function to all keys in a dictionary.
mapKeys ((+) 1) (Dict.fromList [(5, "Jack"), (10, "Jill")])
== Dict.fromList [(6, "Jack"), (11, "Jill")]Apply a function that may or may not succeed to all entries in a dictionary, but only keep the successes.
isTeen : Int -> String -> Maybe String
isTeen n a =
if 13 <= n && n <= 19 then
Just <| String.toUpper a
else
Nothing
Dict.fromList
[ ( 5, "Jack" )
, ( 15, "Jill" )
, ( 20, "Jones" )
]
|> filterMap isTeen
== Dict.singleton 15 "JILL"Inverts the keys and values of an array.
dict = Dict.fromList [("Jill", 5), ("Jack", 10)]
inverted = Dict.fromList [(5, "Jill"), ("Jack", 10)]
Dict.invert dict == inverted
Takes a key-fn and a list. Creates a
Dictwhich maps the key to a list of matching elements.