View Source Reather.Either (reather_lite v0.2.5)

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 Functions

Link to this function

confirm(boolean, err \\ nil)

View Source

Create a either from a boolean.

examples

Examples

iex> Either.confirm(true)
{:ok, nil}
iex> Either.confirm(false, :value_error)
{:error, :value_error}

Wrap a value with an error tuple.

examples

Examples

iex> Either.error(1)
{:error, 1}
iex> Either.error({:ok, 1})
{:error, {:ok, 1}}

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}

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}

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"}]

Wrap a value with an ok tuple.

examples

Examples

iex> Either.ok(1)
{:ok, 1}
iex> Either.ok({:error, 1})
{:ok, {:error, 1}}

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

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!"}

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}

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"