Map library

Christian Maeder maeder at tzi.de
Thu Jun 2 11:35:55 EDT 2005


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

How about using Map.foldWithKey (and adding "Ord k =>" to the type
signature)?

mapFilter f = Map.foldWithKey ( \ k -> maybe id (Map.insert k) . f)
    Map.empty

mapPartition f = Map.foldWithKey ( \ k v (l, r) -> either
           ( \ x -> (Map.insert k x l, r))
           ( \ x -> (l, Map.insert k x r)) $ f v)
           (Map.empty, Map.empty)

Could it be more efficient?

Cheers Christian


More information about the Glasgow-haskell-users mailing list