Skip to content

Instantly share code, notes, and snippets.

@singpolyma
Created September 8, 2012 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singpolyma/3680366 to your computer and use it in GitHub Desktop.
Save singpolyma/3680366 to your computer and use it in GitHub Desktop.
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