
2 Jun
2005
2 Jun
'05
3:35 p.m.
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