Created
September 8, 2012 22:24
-
-
Save singpolyma/3680366 to your computer and use it in GitHub Desktop.
Inject Either into a MaybeT (Writer e)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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