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
Link to this section Functions
Specs
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}
Specs
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
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]
Specs
Unwraps ok-results and rejects error-results.
Examples
iex> [ok: :a, ok: :b, error: :c] |> Result.filter_and_unwrap()
[:a, :b]
Specs
Unwraps error-results and rejects ok-results.
Examples
iex> [ok: :a, ok: :b, error: :c] |> Result.filter_and_unwrap_err()
[:c]
Specs
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]}
Specs
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}
Specs
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
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
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]}
Specs
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
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]
Specs
Returns the contained value or throw an error.
Specs
Returns the contained error or throw an error.
Specs
Returns the contained error or a default.
Specs
Returns the contained value or a default.