Copyright | (c) Dennis Gosnell 2016 |
---|---|

License | BSD-style (see LICENSE file) |

Maintainer | cdep.illabout@gmail.com |

Stability | experimental |

Portability | POSIX |

Safe Haskell | Safe |

Language | Haskell2010 |

- fromEitherM :: Applicative m => (e -> m a) -> Either e a -> m a
- fromEitherOrM :: Applicative m => Either e a -> (e -> m a) -> m a
- fromEitherM_ :: (Applicative m, Monoid b) => (e -> m b) -> Either e a -> m b
- fromEitherOrM_ :: (Applicative m, Monoid b) => Either e a -> (e -> m b) -> m b
- fromMaybeM :: Applicative m => m a -> Maybe a -> m a
- fromMaybeOrM :: Applicative m => Maybe a -> m a -> m a
- fromMaybeM_ :: (Applicative m, Monoid b) => m b -> Maybe a -> m b
- fromMaybeOrM_ :: (Applicative m, Monoid b) => Maybe a -> m b -> m b
- fromEitherMM :: Monad m => (e -> m a) -> m (Either e a) -> m a
- fromEitherOrMM :: Monad m => m (Either e a) -> (e -> m a) -> m a
- fromMaybeMM :: Monad m => m a -> m (Maybe a) -> m a
- fromMaybeOrMM :: Monad m => m (Maybe a) -> m a -> m a
- fromEither :: (e -> a) -> Either e a -> a
- fromEitherOr :: Either e a -> (e -> a) -> a
- fromMaybe :: a -> Maybe a -> a
- fromMaybeOr :: Maybe a -> a -> a
- collapseEither :: Either a a -> a
- collapseExceptT :: Monad m => ExceptT a m a -> m a

# Monadic in return value

fromEitherM :: Applicative m => (e -> m a) -> Either e a -> m a Source #

A monadic version of `fromEither`

.

`fromEitherM`

leftAction ===`either`

leftAction`pure`

`>>>`

[5]`fromEitherM (\s -> [length s]) $ Right 5`

`>>>`

[3]`fromEitherM (\s -> [length s]) $ Left ("foo" :: String)`

fromEitherOrM :: Applicative m => Either e a -> (e -> m a) -> m a Source #

A `flip`

ed version of `fromEitherM`

.

`>>>`

[5]`fromEitherOrM (Right 5) $ \s -> [length s]`

This can be nice to use as an error handler.

`>>>`

5`fromEitherOrM (Right 5) $ \s -> putStrLn ("error: " ++ s) >> undefined`

`>>>`

error: foo ...`fromEitherOrM (Left "foo") $ \s -> putStrLn ("error: " ++ s) >> undefined`

fromEitherM_ :: (Applicative m, Monoid b) => (e -> m b) -> Either e a -> m b Source #

Similar to `fromEitherM`

, but only run the monadic `leftAction`

if the
`Either`

argument is `Left`

. Otherwise, return `pure`

`mempty`

.

`fromEitherM_`

leftAction ===`either`

leftAction (`const`

`$`

`pure`

`mempty`

)

`>>>`

""`fromEitherM_ (\err -> putStrLn err >> pure "bye") $ Right 5`

`>>>`

there was an error "bye"`fromEitherM_ (\err -> putStrLn err >> pure "bye") $ Left "there was an error"`

This can be convenient when you want to run some sort of logging function
whenever an `Either`

is `Left`

. If you imagine the logging function is
`b -> `

, then the effective type of `IO`

'()'`fromEitherM_`

becomes

, because
'()' has a `fromEitherM_`

:: (e -> `IO`

'()') -> `Either`

e a -> `IO`

'()'`Monoid`

instance, and `IO`

, has an `Applicative`

instance.

`>>>`

there was an error`fromEitherM_ putStrLn $ Left "there was an error"`

fromEitherOrM_ :: (Applicative m, Monoid b) => Either e a -> (e -> m b) -> m b Source #

A `flip`

ed version of `fromEitherM_`

.

fromMaybeM :: Applicative m => m a -> Maybe a -> m a Source #

A monadic version of `fromMaybe`

.

`fromMaybeM`

nothingAction ===`maybe`

nothingAction`pure`

`>>>`

[5]`fromMaybeM [] $ Just 5`

`>>>`

[]`fromMaybeM [] Nothing`

fromMaybeOrM :: Applicative m => Maybe a -> m a -> m a Source #

A `flip`

ed version of `fromMaybeM`

.

`>>>`

[5]`fromMaybeOrM (Just 5) []`

This can be nice to use as an error handler.

`>>>`

5`fromMaybeOrM (Just 5) $ putStrLn "some error occurred" >> undefined`

`>>>`

some error occurred ...`fromMaybeOrM (Nothing) $ putStrLn "some error occurred" >> undefined`

fromMaybeM_ :: (Applicative m, Monoid b) => m b -> Maybe a -> m b Source #

Similar to `fromMaybeM`

, but only run the monadic `nothingAction`

if the
`Maybe`

argument is `Nothing`

. Otherwise, return `pure`

`mempty`

.

`fromMaybeM_`

nothingAction ===`maybe`

nothingAction (`const`

`$`

`pure`

`mempty`

)

`>>>`

""`fromMaybeM_ (putStrLn "hello" >> pure "bye") $ Just 5`

`>>>`

hello "bye"`fromMaybeM_ (putStrLn "hello" >> pure "bye") Nothing`

This can be convenient when you want to run some sort of logging function
whenever a `Maybe`

is `Nothing`

. If you imagine the logging function is

, then the effective type of `IO`

'()'`fromMaybeM_`

becomes

, because '()' has a
`fromMaybeM_`

:: `IO`

'()' -> `Maybe`

a -> `IO`

'()'`Monoid`

instance, and `IO`

, has an `Applicative`

instance.

`>>>`

hello`fromMaybeM_ (putStrLn "hello") Nothing`

fromMaybeOrM_ :: (Applicative m, Monoid b) => Maybe a -> m b -> m b Source #

A `flip`

ed version of `fromMaybeM`

.

# Monadic in both return and sum-type value

fromEitherMM :: Monad m => (e -> m a) -> m (Either e a) -> m a Source #

Similar to `fromEitherM`

but the `Either`

argument is also a monadic value.

`>>>`

[5,10]`fromEitherMM (\s -> [length s]) [Right 5, Right 10]`

`>>>`

[3,100]`fromEitherMM (\s -> [length s]) [Left ("foo" :: String), Right 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.

fromEitherOrMM :: Monad m => m (Either e a) -> (e -> m a) -> m a Source #

A `flip`

ed version of `fromEitherMM`

.

fromMaybeMM :: Monad m => m a -> m (Maybe a) -> m a Source #

Similar to `fromMaybeM`

but the `Maybe`

argument is also a monadic value.

`>>>`

[6,5]`fromMaybeMM [] [Just 6, Just 5]`

`>>>`

[6,7]`fromMaybeMM [] [Just 6, Nothing, Just 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.

fromMaybeOrMM :: Monad m => m (Maybe a) -> m a -> m a Source #

A `flip`

ed version of `fromMaybeMM`

.

# Completely non-monadic functions

fromEither :: (e -> a) -> Either e a -> a Source #

Similar to `fromMaybe`

.

`>>>`

"5"`fromEither show $ Left 5`

`>>>`

"hello"`fromEither show $ Right "hello"`

fromEitherOr :: Either e a -> (e -> a) -> a Source #

A `flip`

ed version of `fromEither`

.

fromMaybe :: a -> Maybe a -> a #

The `fromMaybe`

function takes a default value and and `Maybe`

value. If the `Maybe`

is `Nothing`

, it returns the default values;
otherwise, it returns the value contained in the `Maybe`

.

#### Examples

Basic usage:

`>>>`

"Hello, World!"`fromMaybe "" (Just "Hello, World!")`

`>>>`

""`fromMaybe "" Nothing`

Read an integer from a string using `readMaybe`

. If we fail to
parse an integer, we want to return `0`

by default:

`>>>`

`import Text.Read ( readMaybe )`

`>>>`

5`fromMaybe 0 (readMaybe "5")`

`>>>`

0`fromMaybe 0 (readMaybe "")`

# Collapsing funtions

collapseEither :: Either a a -> a Source #

Collapse an

to an `Either`

a a`a`

. Defined as

.`fromEither`

`id`

Note: Other libraries export this function as `fromEither`

, but our
`fromEither`

function is slightly more general.

`>>>`

3`collapseEither (Right 3)`

`>>>`

"hello"`collapseEither (Left "hello")`

collapseExceptT :: Monad m => ExceptT a m a -> m a Source #

Similar to `collapseEither`

, but for `ExceptT`

.

`>>>`

`import Control.Monad.Except (ExceptT(ExceptT))`

`>>>`

3`collapseExceptT (ExceptT $ pure (Right 3))`

`>>>`

"hello"`collapseExceptT (ExceptT $ pure (Left "hello"))`