[Haskell-cafe] Re: dear traversable
Ben
midfield at gmail.com
Sat Jul 31 22:42:26 EDT 2010
dear applicative geniuses --
thanks for all the help. some comments :
1) the fromList / unzip versions are nice but as others have pointed
out, construction is more expensive than traversal (or traversal w/
copying, like Data.Map.map.) hopefully the maintainers of Data.Map et
al will consider adding one-traversal unzip functions.
2) i too would love John Meacham's unionWithJoin function. i might
call that "outerJoin" myself. there was discussion about this at some
point in time on the libraries list, i'm not sure what happened to it.
3) wren, thanks for the (applicative) pointer. when i tried to
interrogate hoogle it pointed me there too -- though i had to boil
down my question several times. i think it went something like this
Map a (b -> c) -> Map a b -> Map a c
--> no answer
m a (b -> c) -> m a b -> m a c
--> no answer
m (b -> c) -> m b -> m c
--> <*> and a million other hits
at which point i'd practically answered the question myself. this is
not a knock on hoogle -- i often have this experience, it seems to
lead me in the right direction Socratically.
4) ross, i had to ask ghci to even believe your code type-checks! i
didn't realize currying worked that way -- i've never thought to pass
in functions of different arities. as an experiment, i tried
Prelude Data.Map> :t intersectionWith 1
intersectionWith 1
:: (Num (a -> b -> c), Ord k) => Map k a -> Map k b -> Map k c
Prelude Data.Map> :t intersectionWith (const 1)
intersectionWith (const 1)
:: (Num (b -> c), Ord k) => Map k a -> Map k b -> Map k c
Prelude Data.Map> :t intersectionWith id
intersectionWith id
:: (Ord k) => Map k (b -> c) -> Map k b -> Map k c
Prelude Data.Map> :t intersectionWith (\a b c -> c)
intersectionWith (\a b c -> c)
:: (Ord k) => Map k a -> Map k b -> Map k (t -> t)
all of which make sense to me now, but still honestly blow my mind!
best regards, b
ps actually the first two don't make much sense to me, when i think
about it.....
On Fri, Jul 30, 2010 at 8:13 PM, Ben <midfield at gmail.com> 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.
>
> any ideas?
>
> ben
>
More information about the Haskell-Cafe
mailing list