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

Modulererebase-1.21.2Haskell2010

Data.Either.Combinators

  • 19 values
  • Packagererebase-1.21.2
  • Exports19
  • LanguageHaskell2010
  • LicenceMIT
  • SourceCombinators.hs
valuemapLeft :: (a -> c) -> Either a b -> Either c b
#

The mapLeft function takes a function and applies it to an Either value iff the value takes the form Left _.

Using Data.Bifunctor:

mapLeft = first

Using Control.Arrow:

mapLeft = (left)

Using Control.Lens:

mapLeft = over _Left
Example1 expression
mapLeft (*2) (Left 4)Left 8
Example1 expression
mapLeft (*2) (Right "hello")Right "hello"
valuemapRight :: (b -> c) -> Either a b -> Either a c
#

The mapRight function takes a function and applies it to an Either value iff the value takes the form Right _.

Using Data.Bifunctor:

mapRight = second

Using Control.Arrow:

mapRight = (right)

Using Control.Lens:

mapRight = over _Right
Example1 expression
mapRight (*2) (Left "hello")Left "hello"
Example1 expression
mapRight (*2) (Right 4)Right 8
valueeitherToError :: MonadError e m => Either e a -> m a
#

Generalize Either e as MonadError e m.

If the argument has form Left e, an error is produced in the monad via throwError. Otherwise, the Right a part is forwarded.

valuefromLeft' :: Either a b -> a
#

Extracts the element out of a Left and throws an error if its argument take the form Right _.

Using Control.Lens:

fromLeft' x ≡ x^?!_Left
Example1 expression
fromLeft' (Left 12)12
valuefromRight' :: Either a b -> b
#

Extracts the element out of a Right and throws an error if its argument take the form Left _.

Using Control.Lens:

fromRight' x ≡ x^?!_Right
Example1 expression
fromRight' (Right 12)12
valuemapBoth :: (a -> c) -> (b -> d) -> Either a b -> Either c d
#

The mapBoth function takes two functions and applies the first if iff the value takes the form Left _ and the second if the value takes the form Right _.

Using Data.Bifunctor:

mapBoth = bimap

Using Control.Arrow:

mapBoth = (+++)
Example1 expression
mapBoth (*2) (*3) (Left 4)Left 8
Example1 expression
mapBoth (*2) (*3) (Right 4)Right 12
valuemaybeToLeft :: b -> Maybe a -> Either a b
#

Maybe produce a Left, otherwise produce a Right.

Example1 expression
maybeToLeft "default" (Just 12)Left 12
Example1 expression
maybeToLeft "default" NothingRight "default"
valuemaybeToRight :: b -> Maybe a -> Either b a
#

Maybe produce a Right, otherwise produce a Left.

Example1 expression
maybeToRight "default" (Just 12)Right 12
Example1 expression
maybeToRight "default" NothingLeft "default"
valueswapEither :: Either e a -> Either a e
#

Swap the Left and Right sides of an Either.

Example1 expression
swapEither (Right 3)Left 3
Example1 expression
swapEither (Left "error")Right "error"
valuewhenLeft :: Applicative m => Either a b -> (a -> m ()) -> m ()
#

The whenLeft function takes an Either value and a function which returns a monad. The monad is only executed when the given argument takes the form Left _, otherwise it does nothing.

Using Control.Lens:

whenLeft ≡ forOf_ _Left
Example1 expression
whenLeft (Left 12) print12
valuewhenRight :: Applicative m => Either a b -> (b -> m ()) -> m ()
#

The whenRight function takes an Either value and a function which returns a monad. The monad is only executed when the given argument takes the form Right _, otherwise it does nothing.

Using Data.Foldable:

whenRight ≡ forM_

Using Control.Lens:

whenRight ≡ forOf_ _Right
Example1 expression
whenRight (Right 12) print12
valuefromLeft :: a -> Either a b -> a
#

Extract the left value or a default.

fromLeft b ≡ either id (const b)
Example1 expression
fromLeft "hello" (Right 42)"hello"
Example1 expression
fromLeft "hello" (Left "world")"world"
valuefromRight :: b -> Either a b -> b
#

Extract the right value or a default.

fromRight b ≡ either (const b) id
Example1 expression
fromRight "hello" (Right "world")"world"
Example1 expression
fromRight "hello" (Left 42)"hello"
valueisLeft :: Either a b -> Bool
#

The isLeft function returns True iff its argument is of the form Left _.

Using Control.Lens:

isLeft ≡ has _Left
Example1 expression
isLeft (Left 12)True
Example1 expression
isLeft (Right 12)False
valueisRight :: Either a b -> Bool
#

The isRight function returns True iff its argument is of the form Right _.

Using Control.Lens:

isRight ≡ has _Right
Example1 expression
isRight (Left 12)False
Example1 expression
isRight (Right 12)True