fe v0.1.5 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
If a list of FE.Result
s is all FE.Result.ok
s, returns a FE.Result.ok
where the value is a list of the unwrapped values
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
Runs the first function on a success value, or the second function on error value, returning the results
Link to this section Types
Link to this section Functions
If a list of FE.Result
s is all FE.Result.ok
s, returns a FE.Result.ok
where the value is a list of the unwrapped values.
Otherwise, returns FE.Result.error
with the first erroneous value.
Examples
iex> FE.Result.all_ok([FE.Result.ok(:a), FE.Result.ok(:b), FE.Result.ok(:c)])
FE.Result.ok([:a, :b, :c])
iex> FE.Result.all_ok([FE.Result.ok(:a), FE.Result.error("BAD APPLE"), FE.Result.ok(:c)])
FE.Result.error("BAD APPLE")
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.error("foo") |> FE.Result.and_then(&FE.Result.ok(String.length(&1)))
FE.Result.error("foo")
iex> FE.Result.ok("bar") |> FE.Result.and_then(&FE.Result.ok(String.length(&1)))
FE.Result.ok(3)
iex> FE.Result.ok("bar") |> FE.Result.and_then(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.
Returns the FE.Result.ok
values from a list of FE.Result
s.
Examples
iex> FE.Result.oks([FE.Result.ok(:good), FE.Result.error(:bad), FE.Result.ok(:better)])
[:good, :better]
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"
unwrap_with(t(a, b), (a -> c), (b -> d)) :: c | d when a: var, c: var, b: var, d: var
Runs the first function on a success value, or the second function on error value, returning the results.
Examples
iex> FE.Result.ok(1) |> FE.Result.unwrap_with(&inspect/1, &("error: "<> inspect(&1)))
"1"
iex> FE.Result.error("db down") |> FE.Result.unwrap_with(&inspect/1, &("error: "<> &1))
"error: db down"