HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerelude-1.2.0.0Haskell2010

Relude.Monad.Either

SPDX-License-Identifier : MIT Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

Utilities to work with Relude.Either data type.

  • 14 values
  • Packagerelude-1.2.0.0
  • Exports14
  • LanguageHaskell2010
  • LicenceMIT
  • SourceEither.hs

Combinators

8 declarations
valuefromLeft :: a -> Either a b -> a
#

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

Examples

Basic usage:

Example2 expressions
fromLeft 1 (Left 3)3fromLeft 1 (Right "foo")1
valuefromRight :: b -> Either a b -> b
#

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

Examples

Basic usage:

Example2 expressions
fromRight 1 (Right 3)3fromRight 1 (Left "foo")1
valuemaybeToLeft :: r -> Maybe l -> Either l r
#

Maps Maybe to Either wrapping default value into Right.

Example2 expressions
maybeToLeft True (Just "aba")Left "aba"maybeToLeft True NothingRight True
valuemaybeToRight :: l -> Maybe r -> Either l r
#

Maps Maybe to Either wrapping default value into Left.

Example2 expressions
maybeToRight True (Just "aba")Right "aba"maybeToRight True NothingLeft True
valueleftToMaybe :: Either l r -> Maybe l
#

Maps left part of Either to Maybe.

Example2 expressions
leftToMaybe (Left True)Just TrueleftToMaybe (Right "aba")Nothing
valuerightToMaybe :: Either l r -> Maybe r
#

Maps right part of Either to Maybe.

Example2 expressions
rightToMaybe (Left True)NothingrightToMaybe (Right "aba")Just "aba"
valuewhenLeft :: 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.

Example1 expression
whenLeft "bar" (Left 42) (\a -> "success!" <$ print a)42"success!"
Example1 expression
whenLeft "bar" (Right 42) (\a -> "success!" <$ print a)"bar"
valuewhenLeft_ :: Applicative f => Either l r -> (l -> f ()) -> f ()
#

Applies given action to Either content if Left is given.

Example2 expressions
whenLeft_ (Right 42) putTextLnwhenLeft_ (Left "foo") putTextLnfoo

Monadic combinators

6 declarations
valuewhenLeftM :: Monad m => a -> m (Either l r) -> (l -> m a) -> m a
#

Monadic version of whenLeft.

Example1 expression
whenLeftM "bar" (pure $ Left 42) (\a -> "success!" <$ print a)42"success!"
Example1 expression
whenLeftM "bar" (pure $ Right 42) (\a -> "success!" <$ print a)"bar"
valuewhenLeftM_ :: Monad m => m (Either l r) -> (l -> m ()) -> m ()
#

Monadic version of whenLeft_.

Example2 expressions
whenLeftM_ (pure $ Right 42) putTextLnwhenLeftM_ (pure $ Left "foo") putTextLnfoo
valuewhenRight :: 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.

Example1 expression
whenRight "bar" (Left "foo") (\a -> "success!" <$ print a)"bar"
Example1 expression
whenRight "bar" (Right 42) (\a -> "success!" <$ print a)42"success!"
valuewhenRight_ :: Applicative f => Either l r -> (r -> f ()) -> f ()
#

Applies given action to Either content if Right is given.

Example2 expressions
whenRight_ (Left "foo") printwhenRight_ (Right 42) print42
valuewhenRightM :: Monad m => a -> m (Either l r) -> (r -> m a) -> m a
#

Monadic version of whenRight.

Example1 expression
whenRightM "bar" (pure $ Left "foo") (\a -> "success!" <$ print a)"bar"
Example1 expression
whenRightM "bar" (pure $ Right 42) (\a -> "success!" <$ print a)42"success!"
valuewhenRightM_ :: Monad m => m (Either l r) -> (r -> m ()) -> m ()
#

Monadic version of whenRight_.

Example2 expressions
whenRightM_ (pure $ Left "foo") printwhenRightM_ (pure $ Right 42) print42

Orphan instances

1 instance