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.Results is all FE.Result.oks, returns a FE.Result.ok where the value is a list of the unwrapped values

Applies success value of a FE.Result to a provided function and returns its return value, that should be of FE.Result type

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

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

Creates a FE.Result representing a successful output of a computation

Returns the FE.Result.ok values from a list of FE.Results

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 type t(a, b) View Source
t(a, b) :: {:ok, a} | {:error, b}

Link to this section Functions

Link to this function all_ok(list) View Source
all_ok([t(a, any())]) :: t([a], any())

If a list of FE.Results is all FE.Result.oks, 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")
Link to this function and_then(result, f) View Source
and_then(t(a, b), (a -> t(c, b))) :: t(c, b) when a: var

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)
Link to this function error(value) View Source
error(a) :: t(any(), a) when a: var

Creates a FE.Result representing an errorneous output of a computation.

Link to this function fold(elems, f) View Source
fold([c], (c, a -> t(a, b))) :: t(a, b) when c: var, a: var

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)
Link to this function fold(result, elems, f) View Source
fold(t(a, b), [c], (c, a -> t(a, b))) :: t(a, b) when c: var, a: var

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!")
Link to this function map(result, f) View Source
map(t(a, b), (a -> c)) :: t(c, b) when a: var, c: var

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)
Link to this function map_error(result, f) View Source
map_error(t(a, b), (b -> c)) :: t(a, c) when b: var, c: var

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)
Link to this function ok(value) View Source
ok(a) :: t(a) when a: var

Creates a FE.Result representing a successful output of a computation.

Link to this function oks(e) View Source
oks([t(a, any())]) :: [a] when a: var

Returns the FE.Result.ok values from a list of FE.Results.

Examples

iex> FE.Result.oks([FE.Result.ok(:good), FE.Result.error(:bad), FE.Result.ok(:better)])
[:good, :better]
Link to this function to_maybe(result) View Source
to_maybe(t(a, any())) :: FE.Maybe.t(a)

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()
Link to this function to_review(result) View Source
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"])
Link to this function unwrap!(result) View Source
unwrap!(t(a)) :: a | no_return() when a: var

Returns the success value stored in a FE.Result, raises an FE.Result.Error if an error is passed.

Link to this function unwrap_or(result, default) View Source
unwrap_or(t(a), a) :: a when a: var

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"
Link to this function unwrap_with(result, on_ok, on_error) View Source
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"