Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Inject Either into a MaybeT (Writer e)
import Data.Monoid
import Control.Monad
import Control.Applicative
import Control.Error
import Control.Monad.Trans.Class
import Control.Monad.Trans.Writer
collectEither :: (Monoid (es a), Applicative es) => Either a b -> MaybeT (Writer (es a)) b
collectEither (Left e) = lift (tell (pure e)) >> mzero
collectEither (Right a) = return a
runCollectLeft :: MaybeT (Writer es) b -> Either es b
runCollectLeft maybeWriter =
case runWriter (runMaybeT maybeWriter) of
(Just x, _) -> Right x
(Nothing, es) -> Left es
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment