Permalink
Browse files

mapEitherT

  • Loading branch information...
ekmett committed Jul 14, 2012
1 parent 7b63c06 commit 1f0487444d8bd8c909e803f4b3f2f69382d75c44
Showing with 11 additions and 1 deletion.
  1. +10 −0 Control/Monad/Trans/Either.hs
  2. +1 −1 either.cabal
@@ -2,6 +2,7 @@
module Control.Monad.Trans.Either
( EitherT(..)
, eitherT
+ , mapEitherT
, hoistEither
, left
, right
@@ -39,10 +40,12 @@ instance Eq (m (Either e a)) => Eq (EitherT e m a) where
instance Ord (m (Either e a)) => Ord (EitherT e m a) where
compare = compare `on` runEitherT
+
eitherT :: Monad m => (a -> m c) -> (b -> m c) -> EitherT a m b -> m c
eitherT f g (EitherT m) = m >>= \z -> case z of
Left a -> f a
Right b -> g b
+{-# INLINE eitherT #-}
left :: Monad m => e -> EitherT e m a
left = EitherT . return . Left
@@ -52,6 +55,13 @@ right :: Monad m => a -> EitherT e m a
right = return
{-# INLINE right #-}
+
+mapEitherT :: Functor m => (e -> f) -> (a -> b) -> EitherT e m a -> EitherT f m b
+mapEitherT f g (EitherT m) = EitherT (fmap h m) where
+ h (Left e) = Left (f e)
+ h (Right a) = Right (g a)
+{-# INLINE mapEitherT #-}
+
hoistEither :: Monad m => Either e a -> EitherT e m a
hoistEither = EitherT . return
{-# INLINE hoistEither #-}
View
@@ -1,6 +1,6 @@
name: either
category: Control, Monads
-version: 3.0.1
+version: 3.0.2
license: BSD3
cabal-version: >= 1.6
license-file: LICENSE

0 comments on commit 1f04874

Please sign in to comment.