Data.Either
- Package
- purescript-either
- Repository
- purescript/purescript-either
#Either Source
data Either a bThe 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)Generic (Either a b) _Invariant (Either a)Apply (Either e)The
Applyinstance allows functions contained within aRightto transform a value contained within aRightusing the(<*>)operator:Right f <*> Right x == Right (f x)Leftvalues are left untouched:Left f <*> Right x == Left f Right f <*> Left y == Left yCombining
Functor's<$>withApply's<*>can be used to transform a pure function to takeEither-typed arguments sof :: a -> b -> cbecomesf :: Either l a -> Either l b -> Either l c:f <$> Right x <*> Right y == Right (f x y)The
Left-preserving behaviour of both operators means the result of an expression like the above but where any one of the values isLeftmeans the whole result becomesLeftalso, taking the firstLeftvalue found:f <$> Left x <*> Right y == Left x f <$> Right x <*> Left y == Left y f <$> Left x <*> Left y == Left xApplicative (Either e)The
Applicativeinstance enables lifting of values intoEitherwith thepurefunction:pure x :: Either _ _ == Right xCombining
Functor's<$>withApply's<*>andApplicative'spurecan be used to pass a mixture ofEitherand non-Eithertyped values to a function that does not usually expect them, by usingpurefor any value that is not alreadyEithertyped:f <$> Right x <*> pure y == Right (f x y)Even though
pure = Rightit is recommended to usepurein situations like this as it allows the choice ofApplicativeto be changed later without having to go through and replaceRightwith a new constructor.Alt (Either e)The
Altinstance allows for a choice to be made between twoEithervalues with the<|>operator, where the firstRightencountered is taken.Right x <|> Right y == Right x Left x <|> Right y == Right y Left x <|> Left y == Left yBind (Either e)The
Bindinstance allows sequencing ofEithervalues and functions that return anEitherby using the>>=operator:Left x >>= f = Left x Right x >>= f = f xEither's "do notation" can be understood to work like this:x :: forall e a. Either e a x = -- y :: forall e b. Either e b y = -- foo :: forall e a. (a -> b -> c) -> Either e c foo f = do x' <- x y' <- y pure (f x' y')...which is equivalent to...
x >>= (\x' -> y >>= (\y' -> pure (f x' y')))...and is the same as writing...
foo :: forall e a. (a -> b -> c) -> Either e c foo f = case x of Left e -> Left e Right x -> case y of Left e -> Left e Right y -> Right (f x y)Monad (Either e)The
Monadinstance guarantees that there are bothApplicativeandBindinstances forEither.Extend (Either e)The
Extendinstance allows sequencing ofEithervalues and functions that accept anEitherand return a non-Eitherresult using the<<=operator.f <<= Left x = Left x f <<= Right x = Right (f (Right x))(Show a, Show b) => Show (Either a b)The
Showinstance allowsEithervalues to be rendered as a string withshowwhenever there is anShowinstance for both type theEithercan contain.(Eq a, Eq b) => Eq (Either a b)The
Eqinstance allowsEithervalues to be checked for equality with==and inequality with/=whenever there is anEqinstance for both types theEithercan contain.(Eq a) => Eq1 (Either a)(Ord a, Ord b) => Ord (Either a b)The
Ordinstance allowsEithervalues to be compared withcompare,>,>=,<and<=whenever there is anOrdinstance for both types theEithercan contain.Any
Leftvalue is considered to be less than aRightvalue.(Ord a) => Ord1 (Either a)(Bounded a, Bounded b) => Bounded (Either a b)(Semigroup b) => Semigroup (Either a b)
#either Source
either :: forall a b c. (a -> c) -> (b -> c) -> Either a b -> cTakes 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
#fromLeft' Source
fromLeft' :: forall a b. (Unit -> a) -> Either a b -> aSimilar to fromLeft but for use in cases where the default value may be
expensive to compute. As PureScript is not lazy, the standard fromLeft
has to evaluate the default value before returning the result,
whereas here the value is only computed when the Either is known
to be Right.
#fromRight' Source
fromRight' :: forall a b. (Unit -> b) -> Either a b -> bSimilar to fromRight but for use in cases where the default value may be
expensive to compute. As PureScript is not lazy, the standard fromRight
has to evaluate the default value before returning the result,
whereas here the value is only computed when the Either is known
to be Left.
The
Functorinstance allows functions to transform the contents of aRightwith the<$>operator:Leftvalues are untouched: