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.
>>> fromListBy String.length [ "tree" , "apple" , "leaf" ]
Dict.fromList [ ( 4, "leaf" ), ( 5, "apple" ) ]Like Dict.fromList, but you provide a way to deal with
duplicate keys. Create a dictionary from a list of pairs of keys and
values, providing a function that is used to combine multiple values
paired with the same key.
>>> fromListDedupe
... (\a b -> a ++ " " ++ b)
... [ ( "class", "menu" ), ( "width", "100%" ), ( "class", "big" ) ]
Dict.fromList [ ( "class", "menu big" ), ( "width", "100%" ) ]fromListBy and fromListDedupe rolled into one.
>>> fromListDedupeBy (\first second -> first) String.length [ "tree" , "apple" , "leaf" ]
Dict.fromList [ ( 4, "tree" ), ( 5, "apple" ) ]Remove elements which satisfies the predicate.
>>> Dict.fromList [ ( "Mary", 1 ), ( "Jack", 2 ), ( "Jill", 1 ) ]
... |> removeWhen (\_ value -> value == 1 )
Dict.fromList [ ( "Jack", 2 ) ]Remove a key-value pair if its key appears in the set.
>>> import Set
>>> Dict.fromList [ ( "Mary", 1 ), ( "Jack", 2 ), ( "Jill", 1 ) ]
... |> removeMany (Set.fromList [ "Mary", "Jill" ])
Dict.fromList [ ( "Jack", 2 ) ]Keep a key-value pair if its key appears in the set.
>>> Dict.fromList [ ( "Mary", 1 ), ( "Jack", 2 ), ( "Jill", 1 ) ]
... |> keepOnly (Set.fromList [ "Jack", "Jill" ])
Dict.fromList [ ( "Jack", 2 ), ( "Jill", 1 ) ]Insert an element at the given key, providing a combining function that used in the case that there is already an element at that key. The combining function is called with original element and the new element as arguments and returns the element to be inserted.
>>> Dict.fromList [ ( "expenses", 38.25 ), ( "assets", 100.85 ) ]
... |> insertDedupe (+) "expenses" 2.50
... |> insertDedupe (+) "liabilities" -2.50
Dict.fromList [ ( "expenses", 40.75 ), ( "assets", 100.85 ), ( "liabilities", -2.50 ) ]Apply a function to all keys in a dictionary.
>>> Dict.fromList [ ( 5, "Jack" ), ( 10, "Jill" ) ]
... |> mapKeys (\x -> x + 1)
Dict.fromList [ ( 6, "Jack" ), ( 11, "Jill" ) ]
>>> Dict.fromList [ ( 5, "Jack" ), ( 10, "Jill" ) ]
... |> mapKeys toString
Dict.fromList [ ( "5", "Jack" ), ( "10", "Jill" ) ]Apply a function that may or may not succeed to all entries in a dictionary, but only keep the successes.
>>> let
... isTeen n a =
... if 13 <= n && n <= 19 then
... Just <| String.toUpper a
... else
... Nothing
... in
... Dict.fromList [ ( 5, "Jack" ), ( 15, "Jill" ), ( 20, "Jones" ) ]
... |> filterMap isTeen
Dict.fromList [ ( 15, "JILL" ) ]Inverts the keys and values of an array.
>>> Dict.fromList [ ("key", "value") ]
... |> invert
Dict.fromList [ ( "value", "key" ) ]Find the first key/value pair that matches a predicate.
>>> Dict.fromList [ ( 9, "Jill" ), ( 7, "Jill" ) ]
... |> find (\_ value -> value == "Jill")
Just ( 7, "Jill" )
>>> Dict.fromList [ ( 9, "Jill" ), ( 7, "Jill" ) ]
... |> find (\key _ -> key == 5)
Nothing
Takes a key-fn and a list. Creates a
Dictwhich maps the key to a list of matching elements.