Data.Result
- Package
- purescript-result
- Repository
- ad-si/purescript-result
#Result Source
data Result error valueThe Result type is used to represent computations
that may succeed or fail.
Constructors
Instances
Functor (Result a)Invariant (Result a)Bifunctor ResultApply (Result e)Applicative (Result e)The
Applicativeinstance enables lifting of values intoResultwith thepurefunction:pure x :: Result _ _ == Ok xCombining
Functor's<$>withApply's<*>andApplicative'spurecan be used to pass a mixture ofResultand non-Resulttyped values to a function that does not usually expect them, by usingpurefor any value that is not alreadyResulttyped:f <$> Ok x <*> pure y == Ok (f x y)Even though
pure = Okit is recommended to usepurein situations like this as it allows the choice ofApplicativeto be changed later without having to go through and replaceOkwith a new constructor.Alt (Result e)The
Altinstance allows for a choice to be made between twoResultvalues with the<|>operator, where the firstOkencountered is taken.Ok x <|> Ok y == Ok x Error x <|> Ok y == Ok y Error x <|> Error y == Error yBind (Result e)The
Bindinstance allows sequencing ofResultvalues and functions that return anResultby using the>>=operator:Error x >>= f = Error x Ok x >>= f = f xMonad (Result e)The
Monadinstance guarantees that there are bothApplicativeandBindinstances forResult. This also enables thedosyntactic sugar:do x' <- x y' <- y pure (f x' y')Which is equivalent to:
x >>= (\x' -> y >>= (\y' -> pure (f x' y')))Extend (Result e)The
Extendinstance allows sequencing ofResultvalues and functions that accept anResultand return a non-Resultresult using the<<=operator.f <<= Error x = Error x f <<= Ok x = Ok (f (Ok x))(Show a, Show b) => Show (Result a b)The
Showinstance allowsResultvalues to be rendered as a string withshowwhenever there is anShowinstance for both type theResultcan contain.(Eq a, Eq b) => Eq (Result a b)(Eq a) => Eq1 (Result a)(Ord a, Ord b) => Ord (Result a b)(Ord a) => Ord1 (Result a)(Bounded a, Bounded b) => Bounded (Result a b)Foldable (Result a)Bifoldable ResultTraversable (Result a)Bitraversable Result(Semigroup b) => Semigroup (Result a b)
#fromEither Source
fromEither :: forall v e. Either e v -> Result e vConvert an Either to a Result.
fromEither (Left "error") == Error "error"
fromEither (Right "value") == Ok "value"
#result Source
result :: forall c b a. (a -> c) -> (b -> c) -> Result a b -> cTakes two functions and an Result value, if the value is a Error the
inner value is applied to the first function, if the value is a Ok
the inner value is applied to the second function.
Result f g (Error x) == f x
Result f g (Ok y) == g y
- Modules
- Data.
Result
The
Applyinstance allows functions contained within aOkto transform a value contained within aOkusing the(<*>)operator:Errorvalues are left untouched:Combining
Functor's<$>withApply's<*>can be used to transform a pure function to takeResult-typed arguments sof :: a -> b -> cbecomesf :: Result l a -> Result l b -> Result l c:The
Error-preserving behaviour of both operators means the result of an expression like the above but where any one of the values isErrormeans the whole result becomesErroralso, taking the firstErrorvalue found: