relude-1.2.2.0: Safe, performant, user-friendly and lightweight Haskell Standard Library

Copyright | (c) 2016 Stephen Diehl (c) 2016-2018 Serokell (c) 2018-2023 Kowainik
License | MIT
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | Safe
Language | Haskell2010

Relude.Monad.Either

Contents

- Combinators
- Monadic combinators
- Orphan instances

Description

Utilities to work with `Either` data type.

Synopsis

- fromLeft :: a -> Either a b -> a
- fromRight :: b -> Either a b -> b
- maybeToLeft :: r -> Maybe l -> Either l r
- maybeToRight :: l -> Maybe r -> Either l r
- leftToMaybe :: Either l r -> Maybe l
- rightToMaybe :: Either l r -> Maybe r
- whenLeft :: Applicative f => a -> Either l r -> (l -> f a) -> f a
- whenLeft_ :: Applicative f => Either l r -> (l -> f ()) -> f ()
- whenLeftM :: Monad m => a -> m (Either l r) -> (l -> m a) -> m a
- whenLeftM_ :: Monad m => m (Either l r) -> (l -> m ()) -> m ()
- whenRight :: Applicative f => a -> Either l r -> (r -> f a) -> f a
- whenRight_ :: Applicative f => Either l r -> (r -> f ()) -> f ()
- whenRightM :: Monad m => a -> m (Either l r) -> (r -> m a) -> m a
- whenRightM_ :: Monad m => m (Either l r) -> (r -> m ()) -> m ()

# Combinators

fromLeft :: a -> Either a b -> a #

Return the contents of a `Left`-value or a default value otherwise.

@since base-4.10.0.0

#### Examples

Expand

Basic usage:

    >>> fromLeft 1 (Left 3)
    3
    >>> fromLeft 1 (Right "foo")
    1

fromRight :: b -> Either a b -> b #

Return the contents of a `Right`-value or a default value otherwise.

@since base-4.10.0.0

#### Examples

Expand

Basic usage:

    >>> fromRight 1 (Right 3)
    3
    >>> fromRight 1 (Left "foo")
    1

maybeToLeft :: r -> Maybe l -> Either l r #

Maps `Maybe` to `Either` wrapping default value into `Right`.

    >>> maybeToLeft True (Just "aba")
    Left "aba"
    >>> maybeToLeft True Nothing
    Right True

maybeToRight :: l -> Maybe r -> Either l r #

Maps `Maybe` to `Either` wrapping default value into `Left`.

    >>> maybeToRight True (Just "aba")
    Right "aba"
    >>> maybeToRight True Nothing
    Left True

leftToMaybe :: Either l r -> Maybe l #

Maps left part of `Either` to `Maybe`.

    >>> leftToMaybe (Left True)
    Just True
    >>> leftToMaybe (Right "aba")
    Nothing

rightToMaybe :: Either l r -> Maybe r #

Maps right part of `Either` to `Maybe`.

    >>> rightToMaybe (Left True)
    Nothing
    >>> rightToMaybe (Right "aba")
    Just "aba"

whenLeft :: Applicative f => a -> Either l r -> (l -> f a) -> f a #

Applies given action to `Either` content if `Left` is given and returns the result. In case of `Right` the default value will be returned.

    >>> whenLeft "bar" (Left 42) (\a -> "success!" <$ print a)
    42
    "success!"

    >>> whenLeft "bar" (Right 42) (\a -> "success!" <$ print a)
    "bar"

whenLeft_ :: Applicative f => Either l r -> (l -> f ()) -> f () #

Applies given action to `Either` content if `Left` is given.

    >>> whenLeft_ (Right 42) putTextLn
    >>> whenLeft_ (Left "foo") putTextLn
    foo

# Monadic combinators

whenLeftM :: Monad m => a -> m (Either l r) -> (l -> m a) -> m a #

Monadic version of `whenLeft`.

    >>> whenLeftM "bar" (pure $ Left 42) (\a -> "success!" <$ print a)
    42
    "success!"

    >>> whenLeftM "bar" (pure $ Right 42) (\a -> "success!" <$ print a)
    "bar"

whenLeftM_ :: Monad m => m (Either l r) -> (l -> m ()) -> m () #

Monadic version of `whenLeft_`.

    >>> whenLeftM_ (pure $ Right 42) putTextLn
    >>> whenLeftM_ (pure $ Left "foo") putTextLn
    foo

whenRight :: Applicative f => a -> Either l r -> (r -> f a) -> f a #

Applies given action to `Either` content if `Right` is given and returns the result. In case of `Left` the default value will be returned.

    >>> whenRight "bar" (Left "foo") (\a -> "success!" <$ print a)
    "bar"

    >>> whenRight "bar" (Right 42) (\a -> "success!" <$ print a)
    42
    "success!"

whenRight_ :: Applicative f => Either l r -> (r -> f ()) -> f () #

Applies given action to `Either` content if `Right` is given.

    >>> whenRight_ (Left "foo") print
    >>> whenRight_ (Right 42) print
    42

whenRightM :: Monad m => a -> m (Either l r) -> (r -> m a) -> m a #

Monadic version of `whenRight`.

    >>> whenRightM "bar" (pure $ Left "foo") (\a -> "success!" <$ print a)
    "bar"

    >>> whenRightM "bar" (pure $ Right 42) (\a -> "success!" <$ print a)
    42
    "success!"

whenRightM_ :: Monad m => m (Either l r) -> (r -> m ()) -> m () #

Monadic version of `whenRight_`.

    >>> whenRightM_ (pure $ Left "foo") print
    >>> whenRightM_ (pure $ Right 42) print
    42

# Orphan instances

IsString str => MonadFail (Either str) # | For convenient work with `MonadFail`. Since: 0.1.0
Instance details Methods fail :: String -> Either str a #