View Source Rail.Either (rail v0.1.0)
Link to this section Summary
Functions
Alias to chain/2
.
Apply a function to the either when it is ok
compatible value.
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_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
Alias to chain/2
.
examples
Examples
iex> 1 >>> fn v -> v + 10 end
11
iex> {:ok, 1} >>> fn v -> v + 10 end
11
iex> :error >>> fn v -> v + 10 end
{:error, nil}
iex> {:error, :noent} >>> fn v -> v + 10 end
{:error, :noent}
Apply a function to the either when it is ok
compatible value.
examples
Examples
iex> 1 |> Either.chain(fn v -> v + 10 end)
11
iex> {:ok, 1} |> Either.chain(fn v -> v + 10 end)
11
iex> :error |> Either.chain(fn v -> v + 10 end)
{:error, nil}
iex> {:error, :noent} |> Either.chain(fn v -> v + 10 end)
{:error, :noent}
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}}
@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(any()) :: either_like()
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}
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!"}
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"