either 4.3.3.2 → 4.3.4
raw patch · 4 files changed
+65/−7 lines, 4 filesdep ~bifunctorsdep ~profunctorsdep ~semigroupoidsPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: bifunctors, profunctors, semigroupoids
API changes (from Hackage documentation)
+ Control.Monad.Trans.Either: bracketEitherT :: Monad m => EitherT e m a -> (a -> EitherT e m b) -> (a -> EitherT e m c) -> EitherT e m c
+ Control.Monad.Trans.Either: bracketEitherT_ :: Monad m => EitherT e m a -> EitherT e m b -> EitherT e m c -> EitherT e m c
+ Control.Monad.Trans.Either: swapEitherT :: Functor m => EitherT e m a -> EitherT a m e
+ Data.Either.Combinators: swapEither :: Either e a -> Either a e
Files
- CHANGELOG.markdown +10/−2
- either.cabal +4/−4
- src/Control/Monad/Trans/Either.hs +32/−0
- src/Data/Either/Combinators.hs +19/−1
CHANGELOG.markdown view
@@ -1,9 +1,17 @@+4.3.4+-----+* Support `bifunctors` 5, `profunctors` 5, and `semigroupoids` 5.++4.3.3.3+-------+* Fixed and enhanced documentation for `eitherToError`.+ 4.3.3.2-------+------- * Support `exceptions` 0.8 4.3.3.1-------+------- * Support `exceptions` 0.7 4.3.3
either.cabal view
@@ -1,6 +1,6 @@ name: either category: Control, Monads-version: 4.3.3.2+version: 4.3.4 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE@@ -28,15 +28,15 @@ library build-depends: base >= 4 && < 5,- bifunctors >= 4 && < 5,+ bifunctors >= 4 && < 6, exceptions >= 0.5 && < 0.9, free >= 4.9 && < 5, monad-control >= 0.3.2 && < 1.1, MonadRandom >= 0.1 && < 0.4, mtl >= 2.0 && < 2.3,- profunctors >= 4 && < 5,+ profunctors >= 4 && < 6, semigroups >= 0.8.3.1 && < 1,- semigroupoids >= 4 && < 5,+ semigroupoids >= 4 && < 6, transformers >= 0.2 && < 0.5, transformers-base >= 0.4 && < 0.5
src/Control/Monad/Trans/Either.hs view
@@ -27,8 +27,11 @@ , bimapEitherT , mapEitherT , hoistEither+ , bracketEitherT+ , bracketEitherT_ , left , right+ , swapEitherT ) where import Control.Applicative@@ -46,6 +49,7 @@ import Control.Monad.Trans.Control (MonadBaseControl(..), MonadTransControl(..), defaultLiftBaseWith, defaultRestoreM) import Control.Monad.Writer.Class import Control.Monad.Random (MonadRandom,getRandom,getRandoms,getRandomR,getRandomRs)+import Data.Either.Combinators ( swapEither ) import Data.Foldable import Data.Function (on) import Data.Functor.Bind@@ -126,6 +130,34 @@ hoistEither :: Monad m => Either e a -> EitherT e m a hoistEither = EitherT . return {-# INLINE hoistEither #-}++-- | Acquire a resource in 'EitherT' and then perform an action with it,+-- cleaning up afterwards regardless of error. Like+-- 'Control.Exception.bracket', but acting only in 'EitherT'.+bracketEitherT :: Monad m => EitherT e m a -> (a -> EitherT e m b) -> (a -> EitherT e m c) -> EitherT e m c+bracketEitherT before after thing = do+ a <- before+ r <- thing a `catchError` (\err -> after a >> left err)+ -- If catchError already triggered, then `after` already ran *and* we are+ -- in a Left state, so `after` will not run again here.+ _ <- after a+ return r++-- | Version of 'bracketEitherT' which discards the result from the initial+-- action.+bracketEitherT_ :: Monad m => EitherT e m a -> EitherT e m b -> EitherT e m c -> EitherT e m c+bracketEitherT_ before after thing = do+ _ <- before+ r <- thing `catchError` (\err -> after >> left err)+ -- If catchError already triggered, then `after` already ran *and* we are+ -- in a Left state, so `after` will not run again here.+ _ <- after+ return r++-- | Monad transformer version of 'swapEither'.+swapEitherT :: (Functor m) => EitherT e m a -> EitherT a m e+swapEitherT = EitherT . fmap swapEither . runEitherT+{-# INLINE swapEitherT #-} instance Monad m => Functor (EitherT e m) where fmap f = EitherT . liftM (fmap f) . runEitherT
src/Data/Either/Combinators.hs view
@@ -33,6 +33,7 @@ , leftToMaybe , rightToMaybe , eitherToError+ , swapEither ) where import Control.Applicative@@ -305,6 +306,23 @@ rightToMaybe :: Either a b -> Maybe b rightToMaybe = either (const Nothing) Just --- | Generalize 'Either e' as 'MonadError e m'.++-- | Generalize @Either e@ as @MonadError e m@.+--+-- If the argument has form @Left e@, an error is produced in the monad via+-- 'throwError'. Otherwise, the @Right a@ part is forwarded. eitherToError :: (MonadError e m) => Either e a -> m a eitherToError = either throwError return++-- | Swap the 'Left' and 'Right' sides of an 'Either'.+--+-- @+-- >>> swapEither (Right 3)+-- Left 3+--+-- >>> swapEither (Left "error")+-- Right "error"+-- @+swapEither :: Either e a -> Either a e+swapEither = either Right Left+{-# INLINE swapEither #-}