from-sum 0.1.0.0 → 0.1.1.0
raw patch · 3 files changed
+109/−14 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Control.FromSum: fromEitherOrM :: Applicative m => Either e a -> (e -> m a) -> m a
+ Control.FromSum: fromEitherOrMM :: Monad m => m (Either e a) -> (e -> m a) -> m a
+ Control.FromSum: fromMaybeOrM :: Applicative m => Maybe a -> m a -> m a
+ Control.FromSum: fromMaybeOrMM :: Monad m => m (Maybe a) -> m a -> m a
Files
- README.md +37/−0
- from-sum.cabal +2/−2
- src/Control/FromSum.hs +70/−12
+ README.md view
@@ -0,0 +1,37 @@++Control.FromSum+===============++[](http://travis-ci.org/cdepillabout/from-sum)+[](https://hackage.haskell.org/package/from-sum)+[](http://stackage.org/lts/package/from-sum)+[](http://stackage.org/nightly/package/from-sum)++This Haskell module exports the `fromEitherM` and `fromMaybeM` convenience+functions.++```haskell+fromMaybeM :: m a -> Maybe a -> m a++fromEitherM :: (e -> m a) -> Either e a -> m a+```++`fromEitherM leftAction eitherValue` is the same as `either leftAction pure+eitherValue`.++`fromMaybeM nothingAction maybeValue` is the same as `maybe nothingAction pure+maybeValue`.++## Usage++```haskell+>>> import Control.FromSum (fromEitherM, fromMaybeM)+>>> fromMaybeM [] $ Just 5+[5]+>>> fromMaybeM [] Nothing+[]+>>> fromEitherM (\s -> [length s]) $ Right 5+[5]+>>> fromEitherM (\s -> [length s]) $ Left "foo"+[3]+```
from-sum.cabal view
@@ -1,5 +1,5 @@ name: from-sum-version: 0.1.0.0+version: 0.1.1.0 synopsis: Canonical fromMaybeM and fromEitherM functions. description: Please see README.md homepage: https://github.com/cdepillabout/from-sum@@ -10,7 +10,7 @@ copyright: 2016 Dennis Gosnell category: Control build-type: Simple--- extra-source-files:+extra-source-files: README.md cabal-version: >=1.10 library
src/Control/FromSum.hs view
@@ -14,10 +14,14 @@ module Control.FromSum ( -- * Monadic in return value fromEitherM+ , fromEitherOrM , fromMaybeM+ , fromMaybeOrM -- * Monadic in both return and sum-type value , fromEitherMM+ , fromEitherOrMM , fromMaybeMM+ , fromMaybeOrMM -- * Completely non-monadic functions , fromEither , fromMaybe@@ -31,6 +35,10 @@ -- | A monadic version of 'fromEither'. --+-- @+-- 'fromEitherM' leftAction === 'either' leftAction 'pure'+-- @+-- -- >>> fromEitherM (\s -> [length s]) $ Right 5 -- [5] -- >>> fromEitherM (\s -> [length s]) $ Left ("foo" :: String)@@ -40,8 +48,29 @@ => (e -> m a) -> Either e a -> m a fromEitherM leftAction = either leftAction pure +-- | A 'flip'ed version of 'fromEitherM'.+--+-- >>> fromEitherOrM (Right 5) $ \s -> [length s]+-- [5]+--+-- This can be nice to use as an error handler.+--+-- >>> fromEitherOrM (Right 5) $ \s -> putStrLn ("error: " ++ s) >> undefined+-- 5+-- >>> fromEitherOrM (Left "foo") $ \s -> putStrLn ("error: " ++ s) >> undefined+-- error: foo+-- ...+fromEitherOrM+ :: Applicative m+ => Either e a -> (e -> m a) -> m a+fromEitherOrM = flip fromEitherM+ -- | A monadic version of 'fromMaybe'. --+-- @+-- 'fromMaybeM' nothingAction === 'maybe' nothingAction 'pure'+-- @+-- -- >>> fromMaybeM [] $ Just 5 -- [5] -- >>> fromMaybeM [] Nothing@@ -51,33 +80,62 @@ => m a -> Maybe a -> m a fromMaybeM nothingAction = maybe nothingAction pure --- | Similar to 'fromEitherM' but 'Either' argument is also a monadic value.+-- | A 'flip'ed version of 'fromMaybeM'. ----- >>> fromEitherMM (\s -> [length s]) . pure $ Right 5+-- >>> fromMaybeOrM (Just 5) [] -- [5]--- >>> fromEitherMM (\s -> [length s]) . pure $ Left ("foo" :: String)--- [3] ----- *NOTE*: I don't particularly like the name of this function. If you have a+-- This can be nice to use as an error handler.+--+-- >>> fromMaybeOrM (Just 5) $ putStrLn "some error occurred" >> undefined+-- 5+-- >>> fromMaybeOrM (Nothing) $ putStrLn "some error occurred" >> undefined+-- some error occurred+-- ...+fromMaybeOrM+ :: Applicative m+ => Maybe a -> m a -> m a+fromMaybeOrM = flip fromMaybeM++-- | Similar to 'fromEitherM' but the 'Either' argument is also a monadic value.+--+-- >>> fromEitherMM (\s -> [length s]) [Right 5, Right 10]+-- [5,10]+-- >>> fromEitherMM (\s -> [length s]) [Left ("foo" :: String), Right 100]+-- [3,100]+--+-- __NOTE__: I don't particularly like the name of this function. If you have a -- suggestion for a better name, please submit a PR or issue. fromEitherMM :: Monad m => (e -> m a) -> m (Either e a) -> m a fromEitherMM eitherAction mEither = fromEitherM eitherAction =<< mEither --- | Similar to 'fromMaybeMA monadic version of 'fromMaybe'.------ >>> fromMaybeMM [] . pure $ Just 5--- [5]--- >>> fromMaybeMM [] $ pure Nothing--- []+-- | A 'flip'ed version of 'fromEitherMM'.+fromEitherOrMM+ :: Monad m+ => m (Either e a) -> (e -> m a) -> m a+fromEitherOrMM = flip fromEitherMM --- *NOTE*: I don't particularly like the name of this function. If you have a+-- | Similar to 'fromMaybeM' but the 'Maybe' argument is also a monadic value.+--+-- >>> fromMaybeMM [] [Just 6, Just 5]+-- [6,5]+-- >>> fromMaybeMM [] [Just 6, Nothing, Just 7]+-- [6,7]+--+-- __NOTE__: I don't particularly like the name of this function. If you have a -- suggestion for a better name, please submit a PR or issue. fromMaybeMM :: Monad m => m a -> m (Maybe a) -> m a fromMaybeMM nothingAction mMaybe = fromMaybeM nothingAction =<< mMaybe++-- | A 'flip'ed version of 'fromMaybeMM'.+fromMaybeOrMM+ :: Monad m+ => m (Maybe a) -> m a -> m a+fromMaybeOrMM = flip fromMaybeMM -- | Similar to 'fromMaybe'. --