# 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 into`Result`

with the`pure`

function:`pure x :: Result _ _ == Ok x`

Combining

`Functor`

's`<$>`

with`Apply`

's`<*>`

and`Applicative`

's`pure`

can be used to pass a mixture of`Result`

and non-`Result`

typed values to a function that does not usually expect them, by using`pure`

for any value that is not already`Result`

typed:`f <$> Ok x <*> pure y == Ok (f x y)`

Even though

`pure = Ok`

it is recommended to use`pure`

in situations like this as it allows the choice of`Applicative`

to be changed later without having to go through and replace`Ok`

with a new constructor.`Alt (Result e)`

The

`Alt`

instance allows for a choice to be made between two`Result`

values with the`<|>`

operator, where the first`Ok`

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 of`Result`

values and functions that return an`Result`

by using the`>>=`

operator:`Error x >>= f = Error x Ok x >>= f = f x`

`Monad (Result e)`

The

`Monad`

instance guarantees that there are both`Applicative`

and`Bind`

instances for`Result`

. This also enables the`do`

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 of`Result`

values and functions that accept an`Result`

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 allows`Result`

values to be rendered as a string with`show`

whenever there is an`Show`

instance for both type the`Result`

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 a`Ok`

to transform a value contained within a`Ok`

using the`(<*>)`

operator:`Error`

values are left untouched:Combining

`Functor`

's`<$>`

with`Apply`

's`<*>`

can be used to transform a pure function to take`Result`

-typed arguments so`f :: a -> b -> c`

becomes`f :: 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 is`Error`

means the whole result becomes`Error`

also, taking the first`Error`

value found: