A single general modification function for Map and IntMap proposal

Milan Straka fox at ucw.cz
Tue Apr 30 17:20:58 CEST 2013

Hi all,

> -----Original message-----
> From: Nikita Volkov <nikita.y.volkov at gmail.com>
> Sent: 30 Apr 2013, 17:18
> Because of the above I have very often found myself in requirement for the following function:
>    withItem ::
>      (Ord k) =>
>      k ->
>      (Maybe i -> (r, Maybe i)) ->
>      Map k i -> (r, Map k i)
>    withItem k f m = 
>      let
>        item = Map.lookup k m
>        (r, item') = f item
>        m' = Map.update (const item') k m
>      in (r, m')

last time we talked about adding
  lens :: k -> Map k a -> (Maybe a -> Map k a, Maybe a)
the performance of the direct implementation was actually worse than
using lookup+insert or such, see the thread
and the benchmark results at

I am also a bit worried whether withItem is the "right" general
function. For example Shachaf Ben-Kiki mentions the
  at :: (Ord k, Functor f) => k -> (Maybe i -> f (Maybe i)) -> Map k i -> f (Map k i)
from the lens package in other mail, but maybe there are others.

>    1. Implement an efficient version of "withItem" for lazy and strict versions of "Map" and "IntMap".

Maybe we will need a benchmark to see whether withItem is really faster
than combination of lookup+update.

>    3. Begin the deprecation process of the following functions: insertWith, insertWithKey, insertLookupWithKey, adjust, adjustWithKey, update, updateWithKey, updateLookupWithKey, alter.

I am against deprecating insertWith, insertWithKey, updateWithKey,
updateWithKey, because they are useful.

I am against deprecating adjust, adjustWithKey and alter, mostly because
of compatibility reasons, although I agree that their name is not

I am indifferent about insertLookupWithKey and updateLookupWithKey. It
would be nice to show that their implementation is really faster than
lookup+insert / lookup+update combo.


More information about the Libraries mailing list