[Haskell-cafe] dear traversable

Stephen Tetley stephen.tetley at gmail.com
Sat Jul 31 03:40:37 EDT 2010

On 31 July 2010 06:45, wren ng thornton <wren at freegeek.org> wrote:
> 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:

Unless I'm missing something. This one has one traversal...

unzipMap :: Ord a => M.Map a (b, c) -> (M.Map a b, M.Map a c)
unzipMap = M.foldrWithKey fn (M.empty,M.empty)
    fn k a (m1,m2) = (M.insert k (fst a) m1, M.insert k (snd a) m2)

More information about the Haskell-Cafe mailing list