[Haskell-beginners] Signature of monadic functions

Daniel Trstenjak daniel.trstenjak at gmail.com
Thu Oct 17 10:57:49 UTC 2013


Hi Lorenzo,

all the nice abstractions like Functor, Traversable or Foldable operate
on the values of the Map. So there's 'Data.Traversable.mapM', which
almost does what you want, but only for the values of the Map.

Ok, here's a solution that does what you want:

import Data.Map
import Control.Monad

mapKeysM :: (Ord k1, Ord k2, Monad m) => (k1 -> m k2) -> Map k1 v -> m (Map k2 v)
mapKeysM f map = return . fromList =<< mapM g (toList map)
  where
   g (key, value) = do
      key' <- f key
      return (key', value)


Certainly there has to be a solution using the lens library ...


Greetings,
Daniel


More information about the Beginners mailing list