FunFunc v0.2.0 FunFunc.Result
Functions for result values.
The result value is {:ok, x}
or {:error, x}
.
Link to this section Summary
Functions
Collects only error value
Collects only ok value
Creates a error value
Checks the value is a error value
Gets the value in the error result, or gets the default value
Gets the value in the error result, or gets the result of the function
Maps the inside value and flatten if the value is the ok value. Otherwise it returns the value itself
Maps the inside value and flatten if the value is the error value. Otherwise it returns the value itself
If the value is ok, applys the first function. If the value is error, applys the second function. Otherwise returns itself
Maps the inside value if the value is the ok value. Otherwise it returns the value itself
Maps the inside value if the value is the error value. Otherwise it returns the value itself
Creates a ok value
Checks the value is a ok value
Gets the value in the ok result, or gets the default value
Gets the value in the ok result, or gets the result of the function
Checks the value is the ok value or the error value
Splits the result list. Non result value is ignored
Link to this section Types
Link to this section Functions
Collects only error value.
iex> FunFunc.Result.collect_errors([{:ok, 1}, {:error, 2}, 3])
[2]
Collects only ok value.
iex> FunFunc.Result.collect_oks([{:ok, 1}, {:error, 2}, 3])
[1]
Checks the value is a error value.
Examples
iex> FunFunc.Result.error?({:ok, 1})
false
iex> FunFunc.Result.error?({:error, 1})
true
iex> FunFunc.Result.error?(1)
false
Gets the value in the error result, or gets the default value.
iex> FunFunc.Result.error_or({:ok, 1}, 2)
2
iex> FunFunc.Result.error_or({:error, 1}, 2)
1
iex> FunFunc.Result.error_or(1, 2)
2
Gets the value in the error result, or gets the result of the function.
iex> FunFunc.Result.error_or_else({:ok, 1}, fn -> 2 end)
2
iex> FunFunc.Result.error_or_else({:error, 1}, fn -> 2 end)
1
iex> FunFunc.Result.error_or_else(1, fn -> 2 end)
2
Maps the inside value and flatten if the value is the ok value. Otherwise it returns the value itself.
Examples
iex> FunFunc.Result.flat_map({:ok, 1}, &{:ok, &1 + 1})
{:ok, 2}
iex> FunFunc.Result.flat_map({:error, 1}, &{:ok, &1 + 1})
{:error, 1}
iex> FunFunc.Result.flat_map(1, &{:ok, &1 + 1})
1
Maps the inside value and flatten if the value is the error value. Otherwise it returns the value itself.
Examples
iex> FunFunc.Result.flat_map_error({:ok, 1}, &{:error, &1 + 1})
{:ok, 1}
iex> FunFunc.Result.flat_map_error({:error, 1}, &{:error, &1 + 1})
{:error, 2}
iex> FunFunc.Result.flat_map_error(1, &{:ok, &1 + 1})
1
If the value is ok, applys the first function. If the value is error, applys the second function. Otherwise returns itself.
Examples
iex> FunFunc.Result.join({:ok, 1}, &Integer.to_string/1, &Atom.to_string/1)
"1"
iex> FunFunc.Result.join({:error, :atom}, &Integer.to_string/1, &Atom.to_string/1)
"atom"
iex> FunFunc.Result.join(1, &Integer.to_string/1, &Atom.to_string/1)
1
Maps the inside value if the value is the ok value. Otherwise it returns the value itself.
Examples
iex> FunFunc.Result.map({:ok, 1}, &Integer.to_string/1)
{:ok, "1"}
iex> FunFunc.Result.map({:error, 1}, &Integer.to_string/1)
{:error, 1}
iex> FunFunc.Result.map(1, &Integer.to_string/1)
1
Maps the inside value if the value is the error value. Otherwise it returns the value itself.
Examples
iex> FunFunc.Result.map_error({:ok, 1}, &Integer.to_string/1)
{:ok, 1}
iex> FunFunc.Result.map_error({:error, 1}, &Integer.to_string/1)
{:error, "1"}
iex> FunFunc.Result.map_error(1, &Integer.to_string/1)
1
Checks the value is a ok value.
Examples
iex> FunFunc.Result.ok?({:ok, 1})
true
iex> FunFunc.Result.ok?({:error, 1})
false
iex> FunFunc.Result.ok?(1)
false
Gets the value in the ok result, or gets the default value.
iex> FunFunc.Result.ok_or({:ok, 1}, 2)
1
iex> FunFunc.Result.ok_or({:error, 1}, 2)
2
iex> FunFunc.Result.ok_or(1, 2)
2
Gets the value in the ok result, or gets the result of the function.
iex> FunFunc.Result.ok_or_else({:ok, 1}, fn -> 2 end)
1
iex> FunFunc.Result.ok_or_else({:error, 1}, fn -> 2 end)
2
iex> FunFunc.Result.ok_or_else(1, fn -> 2 end)
2
Checks the value is the ok value or the error value.
Examples
iex> FunFunc.Result.result?({:ok, 1})
true
iex> FunFunc.Result.result?({:error, 1})
true
iex> FunFunc.Result.result?(1)
false
Splits the result list. Non result value is ignored.
iex> FunFunc.Result.split([{:ok, 1}, {:error, 2}, 3])
{[1], [2]}