Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0237de

Browse files
committed
updated: throw and catch
1 parent eac9f2c commit f0237de

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

‎ErrorT.hs‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,29 @@ newtype MyErrorT m a = MyErrorT { runMyErrorT :: m (Either String a) }
99
instance Monad m => Monad (MyErrorT m) where
1010
return = pure
1111
x >>= y = MyErrorT $ do
12-
res <- x
12+
res <- runMyErrorT x
1313
case res of
14-
Right val -> return$runMyErrorT $ y res
14+
Right val -> runMyErrorT $ y val
1515
Left err -> return $ Left err
1616

1717
instance Monad m => Functor (MyErrorT m) where
1818
fmap = liftM
1919

2020
instance Monad m => Applicative (MyErrorT m) where
21-
pure a = MyErrorT $ return $ Right a
21+
pure = MyErrorT . return . Right
2222
(<*>) = ap
2323

2424
instance MonadTrans MyErrorT where
25-
lift m = MyErrorT $ liftM Right m
26-
27-
25+
lift m = MyErrorT $ do
26+
m' <- m
27+
return $ Right m'
28+
29+
throw :: Monad m => String -> MyErrorT m a
30+
throw = MyErrorT . return . Left
31+
32+
catch :: Monad m => MyErrorT m a -> (String -> MyErrorT m a) -> MyErrorT m a
33+
catch m c = MyErrorT $ do
34+
m' <- runMyErrorT m
35+
case m' of
36+
Left err -> runMyErrorT $ c err
37+
Right _ -> runMyErrorT m

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /