Map library

Mario Blazevic mblazevic at
Thu Jun 2 10:29:03 EDT 2005

This being the topic, I'll list the issues I ran into with the new 
Data.Map library:

1. The function Map.union is left-biased whereas the old 
FiniteMap.unionFM was right-biased. The change seems rather arbitrary. 
There are also other changes of this kind, the following two notable 
among them:

2. The functions passed to Map.insertWith and Map.insertWithKey expect 
their arguments in opposite order from the old FiniteMap.addToFM_C. To 
make matters worse, the correct order is not documented so the user is 
forced to guess. When I converted my source to use Data.Map instead of 
FiniteMap, I guessed wrong and it took me a while to find what was wrong.

3. The Data.Map looks much better than the FiniteMap library, and its 
export list is very complete.  There are, however, two (or four) more 
functions that would be really nice to have in there, as they are 
impossible to write efficiently with the functions currently provided:

   mapFilter :: (a -> Maybe b) -> Map k a -> Map k b
   mapFilter f = map Maybe.fromJust . filter Maybe.isJust . map f

   mapPartition :: (a -> Either b c) -> Map k a -> (Map k b, Map k c)
   mapPartition f = removeTags . partition isLeft . map f
       where isLeft (Either.Left _) = True
                 isLeft (Either.Right _) = False
                 removeTags (leftMap, rightMap) = (map (\ (Left x) -> x) 
                                                   map (\ (Right x) -> 
x) rightMap)

For completeness, mapFilterWithKey and mapPartitionWithKey could be 
thrown in too.

More information about the Glasgow-haskell-users mailing list