[Haskell-cafe] dear traversable

Ross Paterson ross at soi.city.ac.uk
Sat Jul 31 06:01:11 EDT 2010

On Fri, Jul 30, 2010 at 08:13:43PM -0700, Ben wrote:
> dear traversable geniuses --
> i am looking for "better" implementations of
> unzipMap :: M.Map a (b, c) -> (M.Map a b, M.Map a c)
> unzipMap m = (M.map fst m, M.map snd m)
> unliftMap :: (Ord a) => M.Map a (b -> c) -> M.Map a b -> M.Map a c
> unliftMap mf ma = M.mapWithKey (\k v -> mf M.! k $ v) ma
> the first is obviously inefficient as it traverses the map twice.  the
> second just seems like it is some kind of fmap.

The second one assumes that every key in ma is also in mf.  A generalization
without that assumption is

  unliftMap = intersectionWith id

As Wren said, it looks like a <*> operator, but in this case there's no
corresponding pure function.  To make the laws work, pure x would have
to be a map that took every key to x.

More information about the Haskell-Cafe mailing list