View Source Flamel.Result (flamel v1.10.0)
Some helper functions for dealing with result tuples
Summary
Functions
Takes an {:ok, value} tuple and returns the value
Returns true if the value is an :error
, {:error, value}
, {:error, value, value}
Takes an {:error, value} tuple and returns the value or nil
Applies a function to the value of an {:ok, value}
. The return value of the function is wrapped in an {:ok, value}
tuple.
If the tuple passed to map is an {:error, reason}
tuple then the function is not applied and the {:error, reason}
tuple is returned.
Takes an {:ok, value} tuple and returns the value
Returns true if the value is an :ok
, {:ok, value}
, {:ok, value, value}
Takes an {:ok, value} tuple and returns the value or nil
Essentially has the same behavior as Flamel.Result.map/2
but the return value of the function is not automatically wrapped in an {:ok, value}
tuple. You should return either a {:ok, value}
tuple or {:error, reason}
tuple from the function.
Flamel.Result.then/2
should be used when the function can produce an error.
Functions
Takes an {:ok, value} tuple and returns the value
Examples
iex> Flamel.Result.error!({:ok, []})
** (ArgumentError) {:ok, []} is not an :error tuple
iex> Flamel.Result.error!({:error, "message"})
"message"
Returns true if the value is an :error
, {:error, value}
, {:error, value, value}
Examples
iex> Flamel.Result.error?({:error, "error message"})
true
iex> Flamel.Result.error?({:ok, "message"})
false
Takes an {:error, value} tuple and returns the value or nil
Examples
iex> Flamel.Result.error_or_nil({:ok, []})
nil
iex> Flamel.Result.error_or_nil({:error, "message"})
"message"
Applies a function to the value of an {:ok, value}
. The return value of the function is wrapped in an {:ok, value}
tuple.
If the tuple passed to map is an {:error, reason}
tuple then the function is not applied and the {:error, reason}
tuple is returned.
Examples
iex> Flamel.Result.map({:ok, []}, fn v -> ["test" | v] end)
{:ok, ["test"]}
iex> Flamel.Result.map({:error, "message"}, fn v -> ["test" | v] end)
{:error, "message"}
Takes an {:ok, value} tuple and returns the value
Examples
iex> Flamel.Result.ok!({:ok, []})
[]
iex> Flamel.Result.ok!({:error, "message"})
** (ArgumentError) {:error, "message"} is not an :ok tuple
Returns true if the value is an :ok
, {:ok, value}
, {:ok, value, value}
Examples
iex> Flamel.Result.ok?({:ok, []})
true
iex> Flamel.Result.ok?({:error, "message"})
false
Takes an {:ok, value} tuple and returns the value or nil
Examples
iex> Flamel.Result.ok_or_nil({:ok, []})
[]
iex> Flamel.Result.ok_or_nil({:error, "message"})
nil
Essentially has the same behavior as Flamel.Result.map/2
but the return value of the function is not automatically wrapped in an {:ok, value}
tuple. You should return either a {:ok, value}
tuple or {:error, reason}
tuple from the function.
Flamel.Result.then/2
should be used when the function can produce an error.
Examples
iex> Flamel.Result.then({:ok, []}, fn v -> {:ok, ["test" | v]} end)
{:ok, ["test"]}
iex> Flamel.Result.then({:ok, []}, fn _v -> {:error, :bad_arg} end)
{:error, :bad_arg}
iex> Flamel.Result.then({:error, "message"}, fn v -> ["test" | v] end)
{:error, "message"}