packages feed

transformers-either (empty) → 0.0.1

raw patch · 4 files changed

+194/−0 lines, 4 filesdep +basedep +transformerssetup-changed

Dependencies added: base, transformers

Files

+ LICENSE view
@@ -0,0 +1,29 @@+BSD 3-Clause License++Copyright (c) 2017, Tim McGilchrist+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+  list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+  this list of conditions and the following disclaimer in the documentation+  and/or other materials provided with the distribution.++* Neither the name of the copyright holder nor the names of its+  contributors may be used to endorse or promote products derived from+  this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Control/Monad/Trans/Either.hs view
@@ -0,0 +1,129 @@+{-# LANGUAGE NoImplicitPrelude #-}+{-# LANGUAGE PatternSynonyms #-}+{-# LANGUAGE RankNTypes #-}+-----------------------------------------------------------------------------+-- |+-- Module      :  Control.Monad.Trans.Either+-- Copyright   :  (C) 2017 Tim McGilchrist+-- License     :  BSD-style (see the file LICENSE)+--+-- Maintainer  :  timmcgil@gmail.com+-- Stability   :  experimental+-- Portability :  portable+--+-- This monad transformer extends "Control.Monad.Trans.Except" with a more+-- familar "Either" naming.+-----------------------------------------------------------------------------+module Control.Monad.Trans.Either (+  -- * Control.Monad.Trans.Either+    EitherT+  , newEitherT+  , pattern EitherT+  , runEitherT+  , eitherT+  , left+  , right+  , mapEitherT+  , hoistEither+  , bimapEitherT++  -- * Extensions+  , firstEitherT+  , secondEitherT+  , hoistMaybe+  , hoistEitherT+  ) where++import           Control.Monad (Monad(..), (=<<))+import           Control.Monad.Trans.Except (ExceptT(..))++import           Data.Maybe (Maybe, maybe)+import           Data.Either (Either(..), either)+import           Data.Function ((.), id)+import           Data.Functor (Functor(..))++------------------------------------------------------------------------+-- Control.Monad.Trans.Either++-- | Type alias for "ExceptT"+--+type EitherT = ExceptT++pattern EitherT :: m (Either x a) -> ExceptT x m a+pattern EitherT m = ExceptT m++-- | Extractor for computations in the either monad.+-- (The inverse of 'newEitherT').+runEitherT :: EitherT x m a -> m (Either x a)+runEitherT (ExceptT m) = m+{-# INLINE runEitherT #-}++-- | Constructor for computations in the either monad.+-- (The inverse of 'runEitherT').+newEitherT :: m (Either x a) -> EitherT x m a+newEitherT =+  ExceptT+{-# INLINE newEitherT #-}++eitherT :: Monad m => (x -> m b) -> (a -> m b) -> EitherT x m a -> m b+eitherT f g m =+  either f g =<< runEitherT m+{-# INLINE eitherT #-}++-- | Constructor for left computations.+left :: Monad m => x -> EitherT x m a+left =+  EitherT . return . Left+{-# INLINE left #-}++-- | Constructor for right computations.+right :: Monad m => a -> EitherT x m a+right =+  return+{-# INLINE right #-}++-- |+mapEitherT :: (m (Either x a) -> n (Either y b)) -> EitherT x m a -> EitherT y n b+mapEitherT f =+  EitherT . f . runEitherT+{-# INLINE mapEitherT #-}++-- | Hoist an "Either" into an "EitherT m"+hoistEither :: Monad m => Either x a -> EitherT x m a+hoistEither =+  EitherT . return+{-# INLINE hoistEither #-}++-- | Map the unwrapped computation using the given function.+bimapEitherT :: Functor m => (x -> y) -> (a -> b) -> EitherT x m a -> EitherT y m b+bimapEitherT f g =+  let+    h (Left  e) = Left  (f e)+    h (Right a) = Right (g a)+  in+    mapEitherT (fmap h)+{-# INLINE bimapEitherT #-}++-- | Map the 'Left' unwrapped computation using the given function.+firstEitherT :: Functor m => (x -> y) -> EitherT x m a -> EitherT y m a+firstEitherT f =+  bimapEitherT f id+{-# INLINE firstEitherT #-}++-- | Map the 'Right' unwrapped computation using the given function.+secondEitherT :: Functor m => (a -> b) -> EitherT x m a -> EitherT x m b+secondEitherT =+  bimapEitherT id+{-# INLINE secondEitherT #-}++-- | Hoist a 'Maybe a' into a 'Right a'+hoistMaybe :: Monad m => x -> Maybe a -> EitherT x m a+hoistMaybe x =+  maybe (left x) return+{-# INLINE hoistMaybe #-}++-- | Hoist+hoistEitherT :: (forall b. m b -> n b) -> EitherT x m a -> EitherT x n a+hoistEitherT f =+  EitherT . f . runEitherT+{-# INLINE hoistEitherT #-}
+ transformers-either.cabal view
@@ -0,0 +1,34 @@+name:                  transformers-either+version:               0.0.1+license:               BSD3+license-file:          LICENSE+author:                Tim McGilchrist <timmcgil@gmail.com>+maintainer:            Tim McGilchrist <timmcgil@gmail.com>+copyright:             (c) 2017 Tim McGilchrist+synopsis:              An Either monad transformer+category:              System+cabal-version:         >= 1.8+build-type:            Simple+description:+            Drop in alternative to ExceptT.++            Uses a pattern synonym to maintain compatibility with the old EitherT types+            but is actually ExceptT under the covers.+       +source-repository head+  type: git+  location: https://github.com/tmcgilchrist/transformers-either.git+                +library+  build-depends:+                       base                            >= 3          && < 5+                     , transformers                    >= 0.4        && < 0.6++  ghc-options:+                       -Wall++  hs-source-dirs:+                       src++  exposed-modules:+                       Control.Monad.Trans.Either