result v1.3.0 Result.Operators
A result operators.
Link to this section Summary
Functions
Chain together a sequence of computations that may fail
Chain together a sequence of computations that may fail for functions with multiple argumets
Return true
if result is error
Fold function returns tuple {:ok, [...]}
if all
tuples in list contain :ok
or {:error, ...}
if
only one tuple contains :error
Apply a function f
to value
if result is Ok
Apply a function if both results are Ok. If not, the first Err will propagate through
Return true
if result is ok
Perform function f
on Ok result and return it
Flatten nested results
Retry count
times the function f
if the result is negative
Return value
if result is ok, otherwise default
Link to this section Functions
Chain together a sequence of computations that may fail.
Examples
iex> val = {:ok, 1}
iex> Result.Operators.and_then(val, fn (x) -> {:ok, x + 1} end)
{:ok, 2}
iex> val = {:error, 1}
iex> Result.Operators.and_then(val, fn (x) -> {:ok, x + 1} end)
{:error, 1}
Chain together a sequence of computations that may fail for functions with multiple argumets.
Examples
iex> args = [{:ok, 1}, {:ok, 2}]
iex> Result.Operators.and_then_x(args, fn (x, y) -> {:ok, x + y} end)
{:ok, 3}
iex> args = [{:ok, 1}, {:error, "ERROR"}]
iex> Result.Operators.and_then_x(args, fn (x, y) -> {:ok, x + y} end)
{:error, "ERROR"}
Return true
if result is error
Examples
iex> Result.Operators.error?({:error, 123})
true
iex> Result.Operators.error?({:ok, 123})
false
Fold function returns tuple {:ok, [...]}
if all
tuples in list contain :ok
or {:error, ...}
if
only one tuple contains :error
.
Examples
iex> val = [{:ok, 3}, {:ok, 5}, {:ok, 12}]
iex> Result.Operators.fold(val)
{:ok, [3, 5, 12]}
iex> val = [{:ok, 3}, {:error, 1}, {:ok, 2}, {:error, 2}]
iex> Result.Operators.fold(val)
{:error, 1}
Apply a function f
to value
if result is Ok.
Examples
iex> ok = {:ok, 3}
iex> Result.Operators.map(ok, fn(x) -> x + 10 end)
{:ok, 13}
iex> error = {:error, 3}
iex> Result.Operators.map(error, fn(x) -> x + 10 end)
{:error, 3}
Apply a function if both results are Ok. If not, the first Err will propagate through.
Examples
iex> Result.Operators.map2({:ok, 1}, {:ok, 2}, fn(x, y) -> x + y end)
{:ok, 3}
iex> Result.Operators.map2({:ok, 1}, {:error, 2}, fn(x, y) -> x + y end)
{:error, 2}
iex> Result.Operators.map2({:error, 1}, {:error, 2}, fn(x, y) -> x + y end)
{:error, 1}
Return true
if result is ok
Examples
iex> Result.Operators.ok?({:ok, 123})
true
iex> Result.Operators.ok?({:error, 123})
false
Perform function f
on Ok result and return it
Examples
iex> Result.Operators.perform({:ok, 123}, fn(x) -> x * 100 end)
{:ok, 123}
iex> Result.Operators.perform({:error, 123}, fn(x) -> IO.puts(x) end)
{:error, 123}
Flatten nested results
resolve :: Result x (Result x a) -> Result x a
Examples
iex> Result.Operators.resolve({:ok, {:ok, 1}})
{:ok, 1}
iex> Result.Operators.resolve({:ok, {:error, "one"}})
{:error, "one"}
iex> Result.Operators.resolve({:error, "two"})
{:error, "two"}
Retry count
times the function f
if the result is negative
retry :: Result err a -> (a -> Result err b) -> Int -> Int -> Result err b
res
- input resultf
- function retruns resultcount
- try counttimeout
- timeout between retries
Examples
iex> Result.Operators.retry({:error, "Error"}, fn(x) -> {:ok, x} end, 3)
{:error, "Error"}
iex> Result.Operators.retry({:ok, "Ok"}, fn(x) -> {:ok, x} end, 3)
{:ok, "Ok"}
iex> Result.Operators.retry({:ok, "Ok"}, fn(_) -> {:error, "Error"} end, 3, 0)
{:error, "Error"}
Return value
if result is ok, otherwise default
Examples
iex> Result.Operators.with_default({:ok, 123}, 456)
123
iex> Result.Operators.with_default({:error, 123}, 456)
456