elmcraft / core-extra

Utility functions for an improved experience with elm/core
https://package.elm-lang.org/packages/elmcraft/core-extra/latest/
Other
23 stars 11 forks source link

Add Dict.Extra.updateIfExists #58

Closed gampleman closed 2 months ago

gampleman commented 4 months ago

Dict.Extra.updateIfExists : comparable -> (a -> a) -> Dict comparable a -> Dict comparable a

Updates a value if the key is present in the dictionary, leaves the dictionary untouched otherwise.

Motivating use case

Dict.update has an overly complex signature for many use cases. It's fairly common to combine it with Maybe.map, but this adds a fair amount of noise in code.

miniBill commented 4 months ago

(Considering how update is implemented, this should be implemented via get/if/insert and not update (Maybe.map))

gampleman commented 2 months ago

Also, we could add Dict.Extra.upsert : comparable -> a -> (a -> a) -> Dict comparable a -> Dict comparable a which also would deal neatly with a very common usage pattern.

(I don't think I've ever used Dict.update with returning Nothing if the key was found)