Skip to content

Commit 789c406

Browse files
committed
chapter 26 - EitherT monad transformer implementation
1 parent aa25952 commit 789c406

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)