Open axman6 opened 2 years ago
https://github.com/haskell-unordered-containers/unordered-containers/issues/245 is a feature request for a very similar function.
An implementation in terms of alterF
should look similar to the one in https://github.com/haskell-unordered-containers/unordered-containers/issues/245#issuecomment-637399985.
Note that the current alterF
implementation still takes two map traversals. A PR that optimizes the implementation would be very welcome though.
Since the implementation in terms of alterF
is so simple, I'd currently prefer not to add a dedicated insertLookupWithKey
function to the API.
Interesting, I'll have to see if I can make it work with alterF
Following on from #172, adding
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k -> (Maybe a, Map k a)
would be quite useful for avoiding multiple traversals of a map in various algorithms. An example I was working on recently was implementing the Misra-Gries summary for finding the most frequent elements in a streaming dataset:I'd also like to have a HashMap based implementation but doing so involves several more traversals of the HashMap.