Result (Reactive Interaction Gateway v3.0.0-alpha.2) View Source

Tools for working with result tuples.

Influenced by the Rust Option/Result implementation.

Link to this section Summary

Functions

Maps a Result to another Result by applying a function to a contained value, leaving non-ok tuples untouched.

Turns an error into a result that contains that error.

Returns false if a result has a contained value and true otherwise.

Unwraps ok-results and rejects error-results.

Unwraps error-results and rejects ok-results.

Turns a list of results into a result with lists of either values or errors.

Maps a Result to another Result by applying a function to a contained value, leaving non-ok tuples untouched.

Maps a Result to another Result by applying a function to a contained error, leaving ok tuples untouched.

Turns a value into a result that contains that value.

Returns true if a result has a contained value and false otherwise.

Maps a Result to another Result by applying a function to a contained value, leaving ok tuples untouched.

Turns a result that holds a list of values or errors into a list of results.

Returns the contained value or throw an error.

Returns the contained error or throw an error.

Returns the contained error or a default.

Returns the contained value or a default.

Link to this section Types

Specs

err() :: err(any())

Specs

err(error_type) :: {:error, error_type}

Specs

error() :: any()

Specs

fmt_errors() :: ([error()] -> any())

Specs

ok() :: ok(any())

Specs

ok(value_type) :: {:ok, value_type}

Specs

t() :: t(any(), any())
Link to this type

t(value_type, error_type)

View Source

Specs

t(value_type, error_type) :: ok(value_type) | err(error_type)

Specs

value() :: any()

Link to this section Functions

Link to this function

and_then(result, value_to_result_fn)

View Source

Specs

and_then(t(), (value() -> t())) :: t()

Maps a Result to another Result by applying a function to a contained value, leaving non-ok tuples untouched.

Note that the given function is expected to return a result. See Result.map/2 if you want to pass a function that returns a value.

Examples

iex> {:ok, :a} |> Result.and_then(fn :a -> {:ok, :b} end)
{:ok, :b}
iex> {:ok, :a} |> Result.and_then(fn :a -> {:error, :b} end)
{:error, :b}
iex> {:error, :a} |> Result.and_then(fn :a -> {:ok, :b} end)
{:error, :a}
Link to this function

err(error_or_err_result)

View Source

Specs

err(error() | err()) :: err()

Turns an error into a result that contains that error.

If the error is already a result, it is returned unchanged.

Examples

iex> Result.err(:a)
{:error, :a}
iex> Result.err(Result.err(:a))
{:error, :a}

Specs

err?(t()) :: boolean()

Returns false if a result has a contained value and true otherwise.

Examples

iex> Enum.filter([ok: 1, error: 2, ok: 3], &Result.err?/1)
[error: 2]
Link to this function

filter_and_unwrap(results)

View Source

Specs

filter_and_unwrap([t()]) :: [value()]

Unwraps ok-results and rejects error-results.

Examples

iex> [ok: :a, ok: :b, error: :c] |> Result.filter_and_unwrap()
[:a, :b]
Link to this function

filter_and_unwrap_err(results)

View Source

Specs

filter_and_unwrap_err([t()]) :: [error()]

Unwraps error-results and rejects ok-results.

Examples

iex> [ok: :a, ok: :b, error: :c] |> Result.filter_and_unwrap_err()
[:c]
Link to this function

list_to_result(results, fmt_errors \\ &(&1))

View Source

Specs

list_to_result([t()], fmt_errors()) :: ok(list()) | err(list())

Turns a list of results into a result with lists of either values or errors.

As soon as there is at least one error in the given list, the result is an error-type Result.

Example

iex> [ok: :a, ok: :b] |> Result.list_to_result()
{:ok, [:a, :b]}
iex> [ok: :a, ok: :b, error: :c, error: :d] |> Result.list_to_result()
{:error, [:c, :d]}
Link to this function

map(result, value_to_value_fn)

View Source

Specs

map(t(), (value() -> value())) :: t()

Maps a Result to another Result by applying a function to a contained value, leaving non-ok tuples untouched.

Note that the given function is expected to return a value. See Result.and_then/2 if you want to pass a function that returns a result.

Examples

iex> {:ok, :a} |> Result.map(fn :a -> :b end)
{:ok, :b}
iex> {:error, :a} |> Result.map(fn :a -> :b end)
{:error, :a}
Link to this function

map_err(result, error_to_error_fn)

View Source

Specs

map_err(t(), (error() -> error())) :: t()

Maps a Result to another Result by applying a function to a contained error, leaving ok tuples untouched.

This function can be used to compose the results of two functions, where the map function returns an error.

Examples

iex> {:error, :a} |> Result.map_err(fn :a -> :b end)
{:error, :b}
iex> {:ok, :a} |> Result.map_err(fn :a -> :b end)
{:ok, :a}

Specs

ok(value() | ok()) :: ok()

Turns a value into a result that contains that value.

If the value is already a result, it is returned unchanged.

Examples

iex> Result.ok(:a)
{:ok, :a}
iex> Result.ok(Result.ok(:a))
{:ok, :a}

Specs

ok?(t()) :: boolean()

Returns true if a result has a contained value and false otherwise.

Examples

iex> Enum.filter([ok: 1, error: 2, ok: 3], &Result.ok?/1)
[ok: 1, ok: 3]

iex> Enum.split_with([ok: 1, error: 2, ok: 3], &Result.ok?/1)
{[ok: 1, ok: 3], [error: 2]}
Link to this function

or_else(result, error_to_result_fn)

View Source

Specs

or_else(t(), (error() -> t())) :: t()

Maps a Result to another Result by applying a function to a contained value, leaving ok tuples untouched.

Note that the given function is expected to return a result. See Result.map_err/2 if you want to pass a function that returns a value.

Examples

iex> {:ok, :a} |> Result.or_else(fn :a -> {:ok, :b} end)
{:ok, :a}
iex> {:error, :a} |> Result.or_else(fn :a -> {:ok, :b} end)
{:ok, :b}
iex> {:error, :a} |> Result.or_else(fn :a -> {:error, :b} end)
{:error, :b}

Specs

result_to_list(t()) :: [ok()] | [err()]

Turns a result that holds a list of values or errors into a list of results.

Example

iex> {:ok, [:a, :b]} |> Result.result_to_list()
[ok: :a, ok: :b]
iex> {:error, [:c, :d]} |> Result.result_to_list()
[error: :c, error: :d]
Link to this function

unwrap(ok_result, expectation \\ nil)

View Source

Specs

unwrap(ok(), expectation :: String.t() | nil) :: value()

Returns the contained value or throw an error.

Specs

unwrap_err(err()) :: error()

Returns the contained error or throw an error.

Link to this function

unwrap_err_or(result, default_value)

View Source

Specs

unwrap_err_or(t(), default :: value() | error()) :: value() | error()

Returns the contained error or a default.

Link to this function

unwrap_or(result, default_value)

View Source

Specs

unwrap_or(t(), default :: value()) :: value()

Returns the contained value or a default.