We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent aa25952 commit 789c406Copy full SHA for 789c406
chapter26/eithert_monad_transformer.hs
@@ -0,0 +1,20 @@
1
+newtype EitherT e m a =
2
+ EitherT { runEitherT :: m (Either e a) }
3
+
4
+instance Functor m => Functor (EitherT e m) where
5
+ fmap f (EitherT etm) = EitherT $ (fmap . fmap) f etm
6
7
+instance Applicative m => Applicative (EitherT e m) where
8
+ pure x = EitherT $ (pure . pure) x
9
+ (EitherT f) <*> (EitherT g) =
10
+ EitherT $ (<*>) <$> f <*> g
11
12
+instance Monad m => Monad (EitherT e m) where
13
+ return = pure
14
+ (EitherT etm) >>= f =
15
+ EitherT $ do
16
+ m <- etm
17
+ case m of
18
+ Left e -> pure (Left e)
19
+ Right a ->
20
+ runEitherT (f a)
0 commit comments