View Source Reather.Either (reather_lite v0.2.0)

Link to this section Summary

Functions

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.

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

Link to this section Functions

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> Reather.Either.new(:ok)
{:ok, nil}
iex> Reather.Either.new(:error)
{:error, nil}
iex> Reather.Either.new({:ok, 3})
{:ok, 3}
iex> Reather.Either.new({:error, "error!"})
{:error, "error!"}
iex> Reather.Either.new({:ok, 1, 2})
{:ok, {1, 2}}
iex> Reather.Either.new({:error, "error", :invalid})
{:error, {"error", :invalid}}
iex> Reather.Either.new({1, 2})
{:ok, {1, 2}}
iex> Reather.Either.new({})
{:ok, {}}
iex> Reather.Either.new(1)
{:ok, 1}
iex> [1, :error]
...> |> Enum.map(fn x ->
...>   Reather.Either.new(x, "error")
...> end)
[{:ok, 1}, {:error, "error"}]

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}]
...> |> Reather.Either.traverse()
{:error, "error!"}