I'm writing a monadic parser instance that transforms Data.Map.Map's:
instance (Ord a, FromEDN a, FromEDN b) => FromEDN (M.Map a b) where
parseEDNv (E.Map m) = mapMmap parseEDNv parseEDN m
parseEDNv v = typeMismatch "Map" v
Data.Map doesn't provide it's own mapM version like Data.Vector does, so i had to write it from scratch:
mapMmap :: (Ord a2, Monad m) => (a1 -> m a2) -> (b1 -> m b2) -> M.Map a1 b1 -> m (M.Map a2 b2)
mapMmap kf vf m = do
let pairsIn = M.assocs m
pairsOut <- mapM fs pairsIn
return $! M.fromList pairsOut
where
fs (k, v) = do
newK <- kf k
newV <- vf v
return (newK, newV)
It works, but very verbose. How to trim into a more succinct INLINEable version without too much black^W monadic magic?