I have come across situations where I both want to update the key in an order-preserving way and also change the value.
I suggest to add a function mapWithKeyMonotonic : (k -> a -> (k, b)) -> Map k a -> Map k b and respective variants for the other map types (e.g. mapWithKeyMonotonic : (Int -> a -> (Int, b)) -> IntMap a -> IntMap b).
This structure preserving rewrite can be done in a single pass. The current API only offers an implementation in two passes, mapWithKey and mapKeysMonotonic, and only for the cases where the function k -> a -> (k, b) can be split into (k -> k, k -> a -> b).
Bikeshed colorings:
mapWithKeyMonotonic
mapValuesAndKeysMonotonic
mapKeysAndValuesMonotonic
transformMonotonic
bimapMonotonic if we want to follow https://hackage-content.haskell.org/package/dbus-1.4.2/docs/DBus-Internal-Types.html#v:bimap
I guess I prefer 1 since it is a close variant of existing names, but I am open to alternatives.
Example uses: Derivative of polynomial implemented as Map Natural c of IntMap c: derive = Map.mapWithKeyMonotonic (\ d k -> (d-1, d*k)) . Map.delete 0.
I have come across situations where I both want to update the key in an order-preserving way and also change the value.
I suggest to add a function
mapWithKeyMonotonic : (k -> a -> (k, b)) -> Map k a -> Map k band respective variants for the other map types (e.g.mapWithKeyMonotonic : (Int -> a -> (Int, b)) -> IntMap a -> IntMap b).This structure preserving rewrite can be done in a single pass. The current API only offers an implementation in two passes,
mapWithKeyandmapKeysMonotonic, and only for the cases where the functionk -> a -> (k, b)can be split into(k -> k, k -> a -> b).Bikeshed colorings:
mapWithKeyMonotonicmapValuesAndKeysMonotonicmapKeysAndValuesMonotonictransformMonotonicbimapMonotonicif we want to follow https://hackage-content.haskell.org/package/dbus-1.4.2/docs/DBus-Internal-Types.html#v:bimapI guess I prefer 1 since it is a close variant of existing names, but I am open to alternatives.
Example uses: Derivative of polynomial implemented as
Map Natural cofIntMap c:derive = Map.mapWithKeyMonotonic (\ d k -> (d-1, d*k)) . Map.delete 0.