Skip to content

Commit 0db4cf3

Browse files
committed
New catchAsLeftM function
1 parent 4908ccb commit 0db4cf3

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/Control/Monad/Oops.hs

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Control.Monad.Oops
3232
runOops,
3333
suspendM,
3434

35+
catchAsLeftM,
3536
catchAndExitFailureM,
3637

3738
throwLeftM,
@@ -160,6 +161,14 @@ suspendM :: forall x m a n b. ()
160161
-> ExceptT x n b
161162
suspendM f = ExceptT . f . runExceptT
162163

164+
-- | Catch the specified exception and return the caught value as 'Left'. If no
165+
-- value was caught, then return the returned value in 'Right'.
166+
catchAsLeftM :: forall x e m a. ()
167+
=> Monad m
168+
=> ExceptT (Variant (x : e)) m a
169+
-> ExceptT (Variant e) m (Either x a)
170+
catchAsLeftM = catchM @x (pure . Left) . fmap Right
171+
163172
-- | Catch the specified exception. If that exception is caught, exit the program.
164173
catchAndExitFailureM :: forall x e m a. ()
165174
=> MonadIO m

0 commit comments

Comments
 (0)