Skip to content

Commit

Permalink
Change dependency from mtl to transformers.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdepillabout committed Dec 18, 2019
1 parent db2a801 commit a6ff450
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion from-sum.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ library
hs-source-dirs: src
exposed-modules: Control.FromSum
build-depends: base >= 4.6 && < 5
, mtl
, transformers
default-language: Haskell2010
ghc-options: -Wall

Expand Down
48 changes: 46 additions & 2 deletions src/Control/FromSum.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module Control.FromSum
, fromEitherOr
, fromMaybe
, fromMaybeOr
-- * Converting from 'Maybe' to 'Either'
, maybeToEither
, maybeToEitherOr
, eitherToMaybe
-- * Collapsing funtions
, collapseEither
, collapseExceptT
Expand All @@ -42,7 +46,7 @@ module Control.FromSum
import Control.Applicative
#endif
import Control.Monad ((<=<))
import Control.Monad.Except (ExceptT, runExceptT)
import Control.Monad.Trans.Except (ExceptT, runExceptT)
import Data.Maybe (fromMaybe)

-- | A monadic version of 'fromEither'.
Expand Down Expand Up @@ -244,10 +248,50 @@ collapseEither = fromEither id

-- | Similar to 'collapseEither', but for 'ExceptT'.
--
-- >>> import Control.Monad.Except (ExceptT(ExceptT))
-- >>> import Control.Monad.Trans.Except (ExceptT(ExceptT))
-- >>> collapseExceptT (ExceptT $ pure (Right 3))
-- 3
-- >>> collapseExceptT (ExceptT $ pure (Left "hello"))
-- "hello"
collapseExceptT :: Monad m => ExceptT a m a -> m a
collapseExceptT = pure . collapseEither <=< runExceptT

-- | Convert a 'Maybe' to an 'Either'.
--
-- If the 'Maybe' is 'Just', then return the value in 'Right'.
--
-- >>> maybeToEither 3 $ Just "hello"
-- Right "hello"
--
-- If the 'Maybe' is 'Nothing', then use the given @e@ as 'Left'.
--
-- >>> maybeToEither 3 Nothing
-- Left 3
maybeToEither :: e -> Maybe a -> Either e a
maybeToEither e Nothing = Left e
maybeToEither _ (Just a) = Right a

-- | A 'flip'ed version of 'maybeToEither'.
--
-- >>> maybeToEitherOr (Just "hello") 3
-- Right "hello"
--
-- >>> maybeToEitherOr Nothing 3
-- Left 3
maybeToEitherOr :: Maybe a -> e -> Either e a
maybeToEitherOr = flip maybeToEither

-- | Convert an 'Either' to a 'Maybe'.
--
-- A 'Right' value becomes 'Just'.
--
-- >>> eitherToMaybe $ Right 3
-- Just 3
--
-- A 'Left' value becomes 'Nothing'.
--
-- >>> eitherToMaybe $ Left "bye"
-- Nothing
eitherToMaybe :: Either e a -> Maybe a
eitherToMaybe (Left _) = Nothing
eitherToMaybe (Right a) = Just a

0 comments on commit a6ff450

Please sign in to comment.