public
Created

Inject Either into a MaybeT (Writer e)

  • Download Gist
CollectLeft2.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.