HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Modulerelude-1.2.2.2Haskell2010

Relude.Monad.Either

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

Utilities to work with Either data type.

Contents
  1. Combinators
  2. Monadic combinators
Synopsis
  • Packagerelude-1.2.2.2
  • Exports14
  • LanguageHaskell2010
  • LicenceMIT
  • SourceEither.hs

Combinators

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
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

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!"
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