fe v0.1.0 FE.Result View Source
FE.Result
is a data type for representing output of a computation that either succeeded or failed.
Link to this section Summary
Functions
Creates a FE.Result
representing an errorneous output of a computation
Folds over provided list of elements applying it and current accumulator to the provided function
Transforms a success value in a FE.Result
using a provided function
Transforms an errorneous value in a FE.Result
using a provided function
Returns the success value stored in a FE.Result
, raises an FE.Result.Error
if an error is passed
Returns the success value stored in a FE.Result
or a provided default value if an error is passed
Link to this section Types
Link to this section Functions
Applies success value of a FE.Result
to a provided function and returns its return value,
that should be of FE.Result
type.
Useful for chaining together a computation consisting of multiple steps, each of which
takes success value wrapped in FE.Result
as an argument and returns a FE.Result
.
Examples
iex> FE.Result.and_then(FE.Result.error("foo"), &FE.Result.ok(String.length(&1)))
FE.Result.error("foo")
iex> FE.Result.and_then(FE.Result.ok("bar"), &FE.Result.ok(String.length(&1)))
FE.Result.ok(3)
iex> FE.Result.and_then(FE.Result.ok("bar"), fn _ -> FE.Result.error(:baz) end)
FE.Result.error(:baz)
Creates a FE.Result
representing an errorneous output of a computation.
Works like fold/3
, except that the first element of the provided list is removed
from it, converted to a success FE.Result
and treated as the initial accumulator.
Then, fold is executed over the remainder of the provided list.
Examples
iex> FE.Result.fold([1], fn _, _ -> FE.Result.error(:one) end)
FE.Result.ok(1)
iex> FE.Result.fold([1, 2, 3], &(FE.Result.ok(&1 + &2)))
FE.Result.ok(6)
iex> FE.Result.fold([1, 2, 3], fn
...> _, 3 -> FE.Result.error(:three)
...> x, y -> FE.Result.ok(x + y)
...> end)
FE.Result.error(:three)
Folds over provided list of elements applying it and current accumulator to the provided function.
The provided function returns a new accumulator, that should be a FE.Result
.
The provided FE.Result
is the initial accumulator.
Returns last value returned by the function.
Stops and returns error if at any moment the function returns error.
Examples
iex> FE.Result.fold(FE.Result.error(:error), [], &FE.Result.ok(&1 + &2))
FE.Result.error(:error)
iex> FE.Result.fold(FE.Result.ok(5), [], &FE.Result.ok(&1 + &2))
FE.Result.ok(5)
iex> FE.Result.fold(FE.Result.error(:foo), [1, 2], &FE.Result.ok(&1 + &2))
FE.Result.error(:foo)
iex> FE.Result.fold(FE.Result.ok(5), [1, 2, 3], &FE.Result.ok(&1 * &2))
FE.Result.ok(30)
iex> FE.Result.fold(FE.Result.ok(5), [1, 2, 3], fn
...> _, 10 -> FE.Result.error("it's a ten!")
...> x, y -> FE.Result.ok(x * y)
...> end)
FE.Result.error("it's a ten!")
Transforms a success value in a FE.Result
using a provided function.
Examples
iex> FE.Result.map(FE.Result.error("foo"), &String.length/1)
FE.Result.error("foo")
iex> FE.Result.map(FE.Result.ok("foo"), &String.length/1)
FE.Result.ok(3)
Transforms an errorneous value in a FE.Result
using a provided function.
Examples
iex> FE.Result.map_error(FE.Result.ok("foo"), &String.length/1)
FE.Result.ok("foo")
iex> FE.Result.map_error(FE.Result.error("foo"), &String.length/1)
FE.Result.error(3)
Creates a FE.Result
representing a successful output of a computation.
Transforms FE.Result
to a FE.Maybe
.
A FE.Result
with successful value becomes a FE.Maybe
with the same value.
An errornous FE.Result
becomes a FE.Maybe
without a value.
Examples
iex> FE.Result.to_maybe(FE.Result.ok(13))
FE.Maybe.just(13)
iex> FE.Result.to_maybe(FE.Result.error("something went wrong"))
FE.Maybe.nothing()
to_review(t(a, b) | t(a, [b])) :: FE.Review.t(a, b)
Transforms FE.Result
to a FE.Review
.
A FE.Result
with successful value becomes an accepted FE.Review
with
the same value.
An errornous FE.Result
with error output being a list becomes a rejected
FE.Review
with issues being exactly this list.
An errornous FE.Result
with error output being other term becomes a rejected
FE.Review
with one issue, being this term.
Examples
iex> FE.Result.to_review(FE.Result.ok(23))
FE.Review.accepted(23)
iex> FE.Result.to_review(FE.Result.error(["wrong", "bad", "very bad"]))
FE.Review.rejected(["wrong", "bad", "very bad"])
iex> FE.Result.to_review(FE.Result.error("error"))
FE.Review.rejected(["error"])
Returns the success value stored in a FE.Result
, raises an FE.Result.Error
if an error is passed.
Returns the success value stored in a FE.Result
or a provided default value if an error is passed.
Examples
iex> FE.Result.unwrap_or(FE.Result.error("foo"), "default")
"default"
iex> FE.Result.unwrap_or(FE.Result.ok("bar"), "default")
"bar"