View Source Reather (reather_lite v0.1.1)
Reather is the combined form of Reader and Either monad.
A Reather
wrapps an environment and the child functions can
use the environment to access the values.
The evaluation of Reather
is lazy, so it's never computed until
explicitly call Reather.run/2
.
Link to this section Summary
Functions
Get the current environment.
Convert the value with ok
or error
tuple.
Inspect the reather.
Map the function to the reather.
Run the reather.
Link to this section Functions
Get the current environment.
Convert the value with ok
or error
tuple.
examples
Examples
iex> Reather.either(:ok)
{:ok, nil}
iex> Reather.either(:error)
{:error, nil}
iex> Reather.either({:ok, 3})
{:ok, 3}
iex> Reather.either({:error, "error!"})
{:error, "error!"}
iex> Reather.either({:ok, 1, 2})
{:ok, {1, 2}}
iex> Reather.either({:error, "error", :invalid})
{:error, {"error", :invalid}}
iex> Reather.either({1, 2})
{:ok, {1, 2}}
iex> Reather.either({})
{:ok, {}}
iex> Reather.either(1)
{:ok, 1}
Inspect the reather.
Warning: It runs the reather. So, the provided reather should not have side effects.
Map the function to the reather.
map
is lazy, so it's never computed until explicitly call
Reather.run/2
.
iex> r = reather do
...> x <- {:ok, 1}
...> x
...> end
iex> r
...> |> Reather.map(fn x -> x + 1 end)
...> |> Reather.run()
{:ok, 2}
Create a new Reather
from the function.
Create a Reather
from the value.
Run the reather.
Create a Reather
from the value.
If the value is Reather
, it will be returned as is.
example
Example
iex> %Reather{} = Reather.wrap(:ok)
iex> r = %Reather{}
iex> ^r = Reather.wrap(r)