Moduleeither-5.0.3Haskell2010
Data.Either.Combinators
Functions for probing and unwrapping values inside of Either.
Most of these combinators are provided for pedagogical purposes and exist in more general forms in other libraries. To that end alternative definitions are supplied below.
- 19 values
- Packageeither-5.0.3
- Exports19
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceCombinators.hs
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
fromRight' (Right 12)12
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 = (+++)
mapBoth (*2) (*3) (Left 4)Left 8
mapBoth (*2) (*3) (Right 4)Right 12
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
mapLeft (*2) (Left 4)Left 8
mapLeft (*2) (Right "hello")Right "hello"
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
mapRight (*2) (Left "hello")Left "hello"
mapRight (*2) (Right 4)Right 8
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
whenRight (Right 12) print12
A synonym of whenRight.
A synonym of whenLeft.
Maybe get the Left side of an Either.
leftToMaybe ≡ either Just (const Nothing)
Using Control.Lens:
leftToMaybe ≡ preview _Left
leftToMaybe x ≡ x^?_Left
leftToMaybe (Left 12)Just 12
leftToMaybe (Right 12)Nothing
Maybe get the Right side of an Either.
rightToMaybe ≡ either (const Nothing) Just
Using Control.Lens:
rightToMaybe ≡ preview _Right
rightToMaybe x ≡ x^?_Right
rightToMaybe (Left 12)Nothing
rightToMaybe (Right 12)Just 12
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.