Added a new function to Utils, and made one of the existing functions simpler and more efficient
This commit is contained in:
parent
1a3df54ee7
commit
d2260e1736
|
@ -359,14 +359,19 @@ mapMapM f m = mapMapWithKeyM (const f) m
|
||||||
|
|
||||||
-- A version of mapM that acts on the values in maps.
|
-- 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 :: (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
|
mapMapWithKeyM f = T.mapM (uncurry f) . Map.mapWithKey (,)
|
||||||
where
|
|
||||||
f' (x,y) = do y' <- f x y
|
|
||||||
return (x, y')
|
|
||||||
|
|
||||||
filterMapByKey :: Ord k => (k -> Bool) -> Map.Map k v -> Map.Map k v
|
filterMapByKey :: Ord k => (k -> Bool) -> Map.Map k v -> Map.Map k v
|
||||||
filterMapByKey f = Map.filterWithKey (\k _ -> f k)
|
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
|
-- | 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.
|
-- into Nothing. If it is a Right value, it is transformed into Just.
|
||||||
eitherToMaybe :: Either a b -> Maybe b
|
eitherToMaybe :: Either a b -> Maybe b
|
||||||
|
|
Loading…
Reference in New Issue
Block a user