View Source Reather.Either (reather_lite v0.2.1)
Link to this section Summary
Functions
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.
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.
Link to this section Functions
Wrap a value with an error tuple.
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}
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.
iex> Either.ok(1)
{:ok, 1}
iex> Either.ok({:error, 1})
{:ok, {:error, 1}}
Check if the value is an ok tuple.
iex> Either.ok?({:ok, 1})
true
iex> Either.ok?({:error, 1})
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.
iex> Either.unwrap({:ok, 1})
1
iex> Either.unwrap({:error, 1})
** (RuntimeError) 1