From d2260e1736cd747f1f177cc1eb9f3d84e6202fb0 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Tue, 14 Apr 2009 12:07:12 +0000 Subject: [PATCH] Added a new function to Utils, and made one of the existing functions simpler and more efficient --- common/Utils.hs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/common/Utils.hs b/common/Utils.hs index bc4ea6a..632af8d 100644 --- a/common/Utils.hs +++ b/common/Utils.hs @@ -359,14 +359,19 @@ mapMapM f m = mapMapWithKeyM (const f) m -- A version of mapM that acts on the values in maps. mapMapWithKeyM :: (Ord a, Monad m) => (a -> b -> m c) -> Map.Map a b -> m (Map.Map a c) -mapMapWithKeyM f m = liftM Map.fromAscList $ mapM f' $ Map.toAscList m - where - f' (x,y) = do y' <- f x y - return (x, y') +mapMapWithKeyM f = T.mapM (uncurry f) . Map.mapWithKey (,) filterMapByKey :: Ord k => (k -> Bool) -> Map.Map k v -> Map.Map k v filterMapByKey f = Map.filterWithKey (\k _ -> f k) +filterMapByKeyM :: (Ord k, Monad m) => (k -> m Bool) -> Map.Map k v -> m (Map.Map k v) +-- There are several ways we could implement this, but this seems okay: +filterMapByKeyM f m = do mDecision <- mapMapWithKeyM addDecision m + return $ Map.map snd $ Map.filter fst mDecision + where + addDecision k v = do d <- f k + return (d, v) + -- | Transforms an Either into a Maybe. If it's a Left value, it is transformed -- into Nothing. If it is a Right value, it is transformed into Just. eitherToMaybe :: Either a b -> Maybe b