[commit: ghc] master: Refactor TcRnMonad.mapAndRecoverM (66f0056)
git at git.haskell.org
git at git.haskell.org
Thu Nov 22 18:46:39 UTC 2018
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/66f0056ae1279c3149053aa600c7fe09575212b1/ghc
>---------------------------------------------------------------
commit 66f0056ae1279c3149053aa600c7fe09575212b1
Author: Simon Jakobi <simon.jakobi at gmail.com>
Date: Thu Nov 22 11:51:53 2018 -0500
Refactor TcRnMonad.mapAndRecoverM
This version doesn't require the 'reverse' step after the monadic
fold.
Test Plan: ./validate
Reviewers: bgamari, tdammers
Reviewed By: tdammers
Subscribers: monoidal, rwbarton, carter
Differential Revision: https://phabricator.haskell.org/D5343
>---------------------------------------------------------------
66f0056ae1279c3149053aa600c7fe09575212b1
compiler/typecheck/TcRnMonad.hs | 2 +-
compiler/utils/Maybes.hs | 5 +++++
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/compiler/typecheck/TcRnMonad.hs b/compiler/typecheck/TcRnMonad.hs
index eb5a63a..a033bc4 100644
--- a/compiler/typecheck/TcRnMonad.hs
+++ b/compiler/typecheck/TcRnMonad.hs
@@ -990,7 +990,7 @@ recoverM recover thing
-- | Drop elements of the input that fail, so the result
-- list can be shorter than the argument list
mapAndRecoverM :: (a -> TcRn b) -> [a] -> TcRn [b]
-mapAndRecoverM f = fmap reverse . foldAndRecoverM (\xs x -> (:xs) <$> f x ) []
+mapAndRecoverM f = mapMaybeM (fmap rightToMaybe . try_m . f)
-- | The accumulator is not updated if the action fails
foldAndRecoverM :: (b -> a -> TcRn b) -> b -> [a] -> TcRn b
diff --git a/compiler/utils/Maybes.hs b/compiler/utils/Maybes.hs
index 3a139a5..14bc46b 100644
--- a/compiler/utils/Maybes.hs
+++ b/compiler/utils/Maybes.hs
@@ -18,6 +18,7 @@ module Maybes (
firstJust, firstJusts,
whenIsJust,
expectJust,
+ rightToMaybe,
-- * MaybeT
MaybeT(..), liftMaybeT, tryMaybeT
@@ -62,6 +63,10 @@ whenIsJust Nothing _ = return ()
orElse :: Maybe a -> a -> a
orElse = flip fromMaybe
+rightToMaybe :: Either a b -> Maybe b
+rightToMaybe (Left _) = Nothing
+rightToMaybe (Right x) = Just x
+
{-
************************************************************************
* *
More information about the ghc-commits
mailing list