Re: [Haskell-cafe] dear traversable

2010-07-31 Thread Henning Thielemann
On Fri, 30 Jul 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) Maybe: mapPair (M.fromAscList, M.fromAscList) $ unzip $ map (\(a,(b,c)) - ((a,b),

Re: [Haskell-cafe] dear traversable

2010-07-31 Thread Stephen Tetley
On 31 July 2010 06:45, wren ng thornton w...@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

Re: [Haskell-cafe] dear traversable

2010-07-31 Thread Ross Paterson
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

Re: [Haskell-cafe] dear traversable

2010-07-31 Thread wren ng thornton
Stephen Tetley wrote: wren ng thornton wrote: Ben wrote: 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

Re: [Haskell-cafe] dear traversable

2010-07-31 Thread Claude Heiland-Allen
On 31/07/10 12:13, wren ng thornton wrote: Stephen Tetley wrote: wren ng thornton wrote: Ben wrote: 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.

Re: [Haskell-cafe] dear traversable

2010-07-31 Thread Stephen Tetley
On 31 July 2010 12:13, wren ng thornton w...@freegeek.org wrote: Well, that's one traversal of the original map, but you have to traverse the new maps repeatedly with all those M.insert calls. And since Data.Map is a balanced tree, that could lead to a whole lot of work rebalancing things.

Re: [Haskell-cafe] dear traversable

2010-07-31 Thread Claude Heiland-Allen
On 31/07/10 13:49, Stephen Tetley wrote: Although I haven't calculated the Big-O scores suspect that original post will actually be the best, the solutions that metamorph into a list and out again look like they're doing needless extra work. They're both O(size m) time, and yes the original is

Re: [Haskell-cafe] dear traversable

2010-07-31 Thread wren ng thornton
Claude Heiland-Allen wrote: On 31/07/10 12:13, wren ng thornton wrote: Stephen Tetley wrote: wren ng thornton wrote: Ben wrote: 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

Re: [Haskell-cafe] dear traversable

2010-07-31 Thread wren ng thornton
wren ng thornton wrote: That O(n)+O(n) is much better than the O(n)*2*O(log n) foldrWithKey/insert version. But it's still about the same as the original 2*O(n) map fst/map snd version. With the primitive I mentioned we could reduce the constant factor by about half. Oops, the

[Haskell-cafe] dear traversable

2010-07-30 Thread Ben
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

Re: [Haskell-cafe] dear traversable

2010-07-30 Thread John Meacham
On Fri, Jul 30, 2010 at 08:13:43PM -0700, Ben wrote: 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 I always thought a useful map primitive would be unionWithJoin :: (a - b - c) -- combine values that appear in both

Re: [Haskell-cafe] dear traversable

2010-07-30 Thread wren ng thornton
Ben wrote: 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. It's the (*) operator of Applicative, which

Re: [Haskell-cafe] dear traversable

2010-07-30 Thread wren ng thornton
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