View Source Reather.Either (reather_lite v0.2.6)

Link to this section Summary

Functions

Create a either from a boolean.

Wrap a value with an error tuple.

Map a function to the either. If the either is ok, the function is applied to the value. If the either is error, it returns as is.

Map a function to the error tuple.

Convert a value into ok or error tuple. The result is a tuple having an :ok or :error atom for the first element, and a value for the second element.

Wrap a value with an ok tuple.

Check if the value is an ok tuple.

Transform a list of eithers to an either of a list. If any of the eithers is error, the result is error.

Unwrap a value from an ok tuple.

Unwrap a value from an ok tuple. If the value is an error tuple, use passed default value or function.

Link to this section Types

@type either(t) :: ok(t) | error(any())
@type either_like() :: ok_like() | error_like()
@type error(t) :: {:error, t}
@type error_like() :: any()
@type ok(t) :: {:ok, t}
@type ok_like() :: any()

Link to this section Functions

Link to this function

confirm(boolean, err \\ nil)

View Source
@spec confirm(boolean(), t) :: either(t) when t: any()

Create a either from a boolean.

examples

Examples

iex> Either.confirm(true)
{:ok, nil}
iex> Either.confirm(false, :value_error)
{:error, :value_error}
@spec error(t) :: error(t) when t: any()

Wrap a value with an error tuple.

examples

Examples

iex> Either.error(1)
{:error, 1}
iex> Either.error({:ok, 1})
{:error, {:ok, 1}}
@spec map(either_like(), (any() -> t)) :: either(t) when t: any()

Map a function to the either. If the either is ok, the function is applied to the value. If the either is error, it returns as is.

examples

Examples

iex> {:ok, 1} |> Either.map(fn x -> x + 1 end)
{:ok, 2}
iex> {:error, 1} |> Either.map(fn x -> x + 1 end)
{:error, 1}
iex> :ok |> Either.map(fn _ -> 1 end)
{:ok, 1}
@spec map_err(either_like(), (any() -> t)) :: either(t) when t: any()

Map a function to the error tuple.

examples

Examples

iex> {:error, 1} |> Either.map_err(fn x -> x + 1 end)
{:error, 2}
iex> {:ok, 1} |> Either.map_err(fn x -> x + 1 end)
{:ok, 1}
iex> :error |> Either.map_err(fn _ -> 1 end)
{:error, 1}
@spec new(t1, t2) :: ok(t1) | error(t2) when t1: any(), t2: any()

Convert a value into ok or error tuple. The result is a tuple having an :ok or :error atom for the first element, and a value for the second element.

examples

Examples

iex> Either.new(:ok)
{:ok, nil}
iex> Either.new(:error)
{:error, nil}
iex> Either.new({:ok, 3})
{:ok, 3}
iex> Either.new({:error, "error!"})
{:error, "error!"}
iex> Either.new({:ok, 1, 2})
{:ok, {1, 2}}
iex> Either.new({:error, "error", :invalid})
{:error, {"error", :invalid}}
iex> Either.new({1, 2})
{:ok, {1, 2}}
iex> Either.new({})
{:ok, {}}
iex> Either.new(1)
{:ok, 1}
iex> [1, :error]
...> |> Enum.map(fn x ->
...>   Either.new(x, "error")
...> end)
[{:ok, 1}, {:error, "error"}]
@spec ok(t) :: ok(t) when t: any()

Wrap a value with an ok tuple.

examples

Examples

iex> Either.ok(1)
{:ok, 1}
iex> Either.ok({:error, 1})
{:ok, {:error, 1}}
@spec ok?(either_like()) :: boolean()

Check if the value is an ok tuple.

examples

Examples

iex> Either.ok?({:ok, 1})
true
iex> Either.ok?(:ok)
true
iex> Either.ok?({:error, 1})
false
iex> Either.ok?(:error)
false
@spec traverse([either_like()]) :: either([any()])

Transform a list of eithers to an either of a list. If any of the eithers is error, the result is error.

examples

Examples

iex> [{:ok, 1}, {:ok, 2}] |> Either.traverse()
{:ok, [1, 2]}
iex> [{:ok, 1}, {:error, "error!"}, {:ok, 2}]
...> |> Either.traverse()
{:error, "error!"}
@spec unwrap(ok_like()) :: any()

Unwrap a value from an ok tuple.

examples

Examples

iex> Either.unwrap({:ok, 1})
1
iex> Either.unwrap({:error, 1})
** (RuntimeError) 1
iex> Either.unwrap({:ok, 1, 2, 3})
{1, 2, 3}
@spec unwrap_or(either_like(), t | (() -> t)) :: t when t: any()

Unwrap a value from an ok tuple. If the value is an error tuple, use passed default value or function.

examples

Examples

iex> Either.unwrap_or({:ok, 1}, 0)
1
iex> Either.unwrap_or({:error, ""}, 0)
0
iex> Either.unwrap_or({:error, ""}, fn -> "default" end)
"default"
iex> Either.unwrap_or(:error, "hello")
"hello"