[Haskell-cafe] dear traversable

wren ng thornton wren at freegeek.org
Sat Jul 31 01:45:25 EDT 2010


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)

I don't think you can give a more efficient implementation using the 
public interface of Data.Map. You need to have a sort of mapping 
function that allows you to thread them together, either via 
continuations or via a primitive:

     splitMap :: (a -> (b,c)) -> Map k a -> (Map k b, Map k c)

This splitMap and the mapEither primitive could be combined:

     data Or a b = Fst a | Both a b | Snd b

     eitherOr (Left  a) = Fst a
     eitherOr (Right b) = Snd a

     mapOr :: (a -> Or b c) -> Map k a -> (Map k b, Map k c)

     mapEither f = mapOr (eitherOr . f)
     splitMap  f = mapOr (uncurry Both)

And the type of John's primitive could be prettied up:

     unionWithJoin :: (Or a b -> c) -> Map k a -> Map k b -> Map k c

-- 
Live well,
~wren


More information about the Haskell-Cafe mailing list