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

collect_oks(vals)
collect_oks([{any, any}]) :: [any]

Collects only ok values.

iex> Func.Result.collect_oks([{:ok, 1}, {:ok, 2}, {:error, 3}, 4])
[1, 2]
error(val)
error(any) :: {any, any}

Creates the error value.

iex> Func.Result.error(1)
{:error, 1}
error?(arg1)
error?({any, any}) :: boolean

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
flat_map(val, f)
flat_map({any, any}, (... -> any)) :: {any, any}

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
flat_map_error(val, f)
flat_map_error({any, any}, (... -> any)) :: {any, any}

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
join(val, f, g)
join({any, any}, (... -> any), (... -> any)) :: any

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
map(val, f)
map({any, any}, (... -> any)) :: {any, any}

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
map_error(val, f)
map_error({any, any}, (... -> any)) :: {any, any}

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
ok(val)
ok(any) :: {any, any}

Creates the ok value

iex> Func.Result.ok(1)
{:ok, 1}
ok?(arg1)
ok?({any, any}) :: boolean

Checks wether the value is the ok.

iex> Func.Result.ok?({:ok, 1})
true
iex> Func.Result.ok?({:error, 1})
false
iex> Func.Result.ok?(1)
false