func v0.4.1 Func.Result
Functions that inspired by Haskell, Data.Either. Data.Maybe)
Summary
Functions
Collects only ok values
Creates the error value
Checks wether the value is the error
Maps and flatten if the value is ok. Otherwise, return itself
Maps and flatten if the value is error. Otherwise, return itself
If the value is ok, applys the first function. If the value is error, applys the second function. Otherwise returns itself
Maps the result if the value is ok. Otherwise, return itself
Maps the result if the value is error. Otherwise, return itself
Creates the ok value
Checks wether the value is the ok
Functions
Collects only ok values.
iex> Func.Result.collect_oks([{:ok, 1}, {:ok, 2}, {:error, 3}, 4])
[1, 2]
Checks wether the value is the error.
iex> Func.Result.error?({:ok, 1})
false
iex> Func.Result.error?({:error, 1})
true
iex> Func.Result.ok?(1)
false
Maps and flatten if the value is ok. Otherwise, return itself.
iex> Func.Result.flat_map({:ok, 1}, &{:ok, &1 + 1})
{:ok, 2}
iex> Func.Result.flat_map({:error, 1}, &{:error, &1 + 1})
{:error, 1}
iex> Func.Result.flat_map(1, &{:ok, &1 + 1})
1
Maps and flatten if the value is error. Otherwise, return itself.
iex> Func.Result.flat_map_error({:ok, 1}, &{:ok, &1 + 1})
{:ok, 1}
iex> Func.Result.flat_map_error({:error, 1}, &{:error, &1 + 1})
{:error, 2}
iex> Func.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
iex> Func.Result.join({:ok, 1}, &Integer.to_string/1, &Atom.to_string/1)
"1"
iex> Func.Result.join({:error, :atom}, &Integer.to_string/1, &Atom.to_string/1)
"atom"
iex> Func.Result.join(1, &Integer.to_string/1, &Atom.to_string/1)
1
Maps the result if the value is ok. Otherwise, return itself.
iex> Func.Result.map({:ok, 1}, &Integer.to_string/1)
{:ok, "1"}
iex> Func.Result.map({:error, 1}, &Integer.to_string/1)
{:error, 1}
iex> Func.Result.map(1, &Integer.to_string/1)
1
Maps the result if the value is error. Otherwise, return itself.
iex> Func.Result.map_error({:ok, 1}, &Integer.to_string/1)
{:ok, 1}
iex> Func.Result.map_error({:error, 1}, &Integer.to_string/1)
{:error, "1"}
iex> Func.Result.map_error(1, &Integer.to_string/1)
1