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 type result()
result() :: {atom(), any()}
Link to this type results()
results() :: [result()]

Link to this section Functions

Link to this function collect_errors(results)
collect_errors(results()) :: list()

Collects only error value.

iex> FunFunc.Result.collect_errors([{:ok, 1}, {:error, 2}, 3])
[2]
Link to this function collect_oks(results)
collect_oks(results()) :: list()

Collects only ok value.

iex> FunFunc.Result.collect_oks([{:ok, 1}, {:error, 2}, 3])
[1]
Link to this function error(x)
error(any()) :: result()

Creates a error value.

Examples

iex> FunFunc.Result.error(1)
{:error, 1}
Link to this function error?(result)
error?(any()) :: boolean()

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
Link to this function error_or(result, default)
error_or(any(), any()) :: any()

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
Link to this function error_or_else(result, default)
error_or_else(any(), (... -> any())) :: any()

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
Link to this function flat_map(result, f)
flat_map(result(), (... -> any())) :: result()

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
Link to this function flat_map_error(result, f)
flat_map_error(result(), (... -> any())) :: result()

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
Link to this function join(result, f, g)
join(result(), (... -> any()), (... -> any())) :: any()

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
Link to this function map(result, f)
map(result(), (... -> any())) :: result()

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
Link to this function map_error(result, f)
map_error(result(), (... -> any())) :: result()

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
Link to this function ok(x)
ok(any()) :: result()

Creates a ok value.

Examples

iex> FunFunc.Result.ok(1)
{:ok, 1}
Link to this function ok?(result)
ok?(any()) :: boolean()

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
Link to this function ok_or(result, default)
ok_or(any(), any()) :: any()

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
Link to this function ok_or_else(result, default)
ok_or_else(any(), (... -> any())) :: any()

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
Link to this function result?(result)
result?(any()) :: boolean()

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
Link to this function split(results)
split(results()) :: {list(), list()}

Splits the result list. Non result value is ignored.

iex> FunFunc.Result.split([{:ok, 1}, {:error, 2}, 3])
{[1], [2]}