Data.Result
- Package
- purescript-result
- Repository
- ad-si/purescript-result
#Result Source
data Result error value
The Result
type is used to represent computations
that may succeed or fail.
Constructors
Instances
Functor (Result a)
Invariant (Result a)
Bifunctor Result
Apply (Result e)
Applicative (Result e)
The
Applicative
instance enables lifting of values intoResult
with thepure
function:pure x :: Result _ _ == Ok x
Combining
Functor
's<$>
withApply
's<*>
andApplicative
'spure
can be used to pass a mixture ofResult
and non-Result
typed values to a function that does not usually expect them, by usingpure
for any value that is not alreadyResult
typed:f <$> Ok x <*> pure y == Ok (f x y)
Even though
pure = Ok
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 replaceOk
with a new constructor.Alt (Result e)
The
Alt
instance allows for a choice to be made between twoResult
values with the<|>
operator, where the firstOk
encountered is taken.Ok x <|> Ok y == Ok x Error x <|> Ok y == Ok y Error x <|> Error y == Error y
Bind (Result e)
The
Bind
instance allows sequencing ofResult
values and functions that return anResult
by using the>>=
operator:Error x >>= f = Error x Ok x >>= f = f x
Monad (Result e)
The
Monad
instance guarantees that there are bothApplicative
andBind
instances forResult
. 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 (Result e)
The
Extend
instance allows sequencing ofResult
values and functions that accept anResult
and return a non-Result
result using the<<=
operator.f <<= Error x = Error x f <<= Ok x = Ok (f (Ok x))
(Show a, Show b) => Show (Result a b)
The
Show
instance allowsResult
values to be rendered as a string withshow
whenever there is anShow
instance for both type theResult
can 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 Result
Traversable (Result a)
Bitraversable Result
(Semigroup b) => Semigroup (Result a b)
#fromEither Source
fromEither :: forall v e. Either e v -> Result e v
Convert 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 -> c
Takes 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
Apply
instance allows functions contained within aOk
to transform a value contained within aOk
using the(<*>)
operator:Error
values are left untouched:Combining
Functor
's<$>
withApply
's<*>
can be used to transform a pure function to takeResult
-typed arguments sof :: a -> b -> c
becomesf :: 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 isError
means the whole result becomesError
also, taking the firstError
value found: