elm-community/dict-extra/1.2.0

NOTE: this package is not compatible with Elm 0.19.1

Dict.Extra

Convenience functions for working with Dict

List operations

groupBy : (a -> comparable) -> List a -> Dict comparable (List a)

Takes a key-fn and a list. Creates a Dict which maps the key to a list of matching elements.

mary = {id=1, name="Mary"}
jack = {id=2, name="Jack"}
jill = {id=1, name="Jill"}
groupBy .id [mary, jack, jill] == Dict.fromList [(1, [jill, mary]), (2, [jack])]
fromListBy : (a -> comparable) -> List a -> Dict comparable a

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:

jack = {id=2, name="Jack"}
jill = {id=1, name="Jill"}
fromListBy .id [jack, jill] == Dict.fromList [(1, jack), (2, jill)]

Manipulation

removeWhen : (comparable -> v -> Bool) -> Dict comparable v -> Dict comparable v

Remove elements which satisfies the predicate.

removeWhen (\c v -> v == 1) Dict.fromList [("Mary", 1), ("Jack", 2), ("Jill", 1)] == Dict.fromList [("Jack", 2)]
removeMany : Set comparable -> Dict comparable v -> Dict comparable v

Remove a key-value pair if its key appears in the set.

keepOnly : Set comparable -> Dict comparable v -> Dict comparable v

Keep a key-value pair if its key appears in the set.