Data.Either
- Package
- purescript-either
- Repository
- purescript/purescript-either
#Either Source
data Either a b
The Either
type is used to represent a choice between two types of value.
A common use case for Either
is error handling, where Left
is used to
carry an error value and Right
is used to carry a success value.
Constructors
Instances
Functor (Either a)
Invariant (Either a)
Bifunctor Either
Apply (Either e)
Applicative (Either e)
The
Applicative
instance enables lifting of values intoEither
with thepure
function:pure x :: Either _ _ == Right x
Combining
Functor
's<$>
withApply
's<*>
andApplicative
'spure
can be used to pass a mixture ofEither
and non-Either
typed values to a function that does not usually expect them, by usingpure
for any value that is not alreadyEither
typed:f <$> Right x <*> pure y == Right (f x y)
Even though
pure = Right
it is recommended to usepure
in situations like this as it allows the choice ofApplicative
to be changed later without having to go through and replaceRight
with a new constructor.Alt (Either e)
The
Alt
instance allows for a choice to be made between twoEither
values with the<|>
operator, where the firstRight
encountered is taken.Right x <|> Right y == Right x Left x <|> Right y == Right y Left x <|> Left y == Left y
Bind (Either e)
The
Bind
instance allows sequencing ofEither
values and functions that return anEither
by using the>>=
operator:Left x >>= f = Left x Right x >>= f = f x
Monad (Either e)
The
Monad
instance guarantees that there are bothApplicative
andBind
instances forEither
. This also enables thedo
syntactic sugar:do x' <- x y' <- y pure (f x' y')
Which is equivalent to:
x >>= (\x' -> y >>= (\y' -> pure (f x' y')))
Extend (Either e)
The
Extend
instance allows sequencing ofEither
values and functions that accept anEither
and return a non-Either
result using the<<=
operator.f <<= Left x = Left x f <<= Right x = Right (f (Right x))
(Show a, Show b) => Show (Either a b)
The
Show
instance allowsEither
values to be rendered as a string withshow
whenever there is anShow
instance for both type theEither
can contain.(Eq a, Eq b) => Eq (Either a b)
(Eq a) => Eq1 (Either a)
(Ord a, Ord b) => Ord (Either a b)
(Ord a) => Ord1 (Either a)
(Bounded a, Bounded b) => Bounded (Either a b)
Foldable (Either a)
Bifoldable Either
Traversable (Either a)
Bitraversable Either
(Semigroup b) => Semigroup (Either a b)
#either Source
either :: forall c b a. (a -> c) -> (b -> c) -> Either a b -> c
Takes two functions and an Either
value, if the value is a Left
the
inner value is applied to the first function, if the value is a Right
the inner value is applied to the second function.
either f g (Left x) == f x
either f g (Right y) == g y
The
Apply
instance allows functions contained within aRight
to transform a value contained within aRight
using the(<*>)
operator:Left
values are left untouched:Combining
Functor
's<$>
withApply
's<*>
can be used to transform a pure function to takeEither
-typed arguments sof :: a -> b -> c
becomesf :: Either l a -> Either l b -> Either l c
:The
Left
-preserving behaviour of both operators means the result of an expression like the above but where any one of the values isLeft
means the whole result becomesLeft
also, taking the firstLeft
value found: