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
- fromMaybeM :: Applicative m => m a -> Maybe a -> m a
- fromEitherMM :: Monad m => (e -> m a) -> m (Either e a) -> m a
- fromMaybeMM :: Monad m => m a -> m (Maybe a) -> m a
- fromEither :: (e -> a) -> Either e a -> a
- fromMaybe :: a -> Maybe a -> a
Monadic in return value
fromEitherM :: Applicative m => (e -> m a) -> Either e a -> m a Source #
A monadic version of fromEither
.
>>>
fromEitherM (\s -> [length s]) $ Right 5
[5]>>>
fromEitherM (\s -> [length s]) $ Left ("foo" :: String)
[3]
fromMaybeM :: Applicative m => m a -> Maybe a -> m a Source #
A monadic version of fromMaybe
.
>>>
fromMaybeM [] $ Just 5
[5]>>>
fromMaybeM [] Nothing
[]
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 Either
argument is also a monadic value.
>>>
fromEitherMM (\s -> [length s]) . pure $ Right 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 suggestion for a better name, please submit a PR or issue.
fromMaybeMM :: Monad m => m a -> m (Maybe a) -> m a Source #
Completely non-monadic functions
fromEither :: (e -> a) -> Either e a -> a Source #
Similar to fromMaybe
.
>>>
fromEither show $ Left 5
"5">>>
fromEither show $ Right "hello"
"hello"
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:
>>>
fromMaybe "" (Just "Hello, World!")
"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 )
>>>
fromMaybe 0 (readMaybe "5")
5>>>
fromMaybe 0 (readMaybe "")
0