View Source Reather (reather_lite v0.2.7)

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.

Create a new Reather from a reather and function. The function will be called after the reather is run.

Inspect the reather result when run.

Map a function to the reather.

Create a new Reather from the function.

Create a Reather from the value.

Run the reather.

Transform a list of reathers to an reather of a list.

Create a Reather from the value. If the value is Reather, it will be returned as is.

Link to this section Types

@type reather() :: %Reather{reather: (... -> any())}

Link to this section Functions

@spec ask() :: reather()

Get the current environment.

@spec chain(reather(), (any() -> reather())) :: reather()

Create a new Reather from a reather and function. The function will be called after the reather is run.

@spec inspect(
  reather(),
  keyword()
) :: reather()

Inspect the reather result when run.

@spec map(reather(), (any() -> any())) :: reather()

Map a function to the reather.

map is lazy, so it's never computed until explicitly call Reather.run/2.

examples

Examples

iex> r = reather do
...>       x <- {:ok, 1}
...>       x
...>     end
iex> r
...> |> Reather.map(fn x -> x + 1 end)
...> |> Reather.run()
{:ok, 2}
@spec new((... -> any())) :: reather()

Create a new Reather from the function.

@spec of(any()) :: reather()

Create a Reather from the value.

Link to this function

run(reather, env \\ %{})

View Source
@spec run(reather(), %{}) :: any()

Run the reather.

@spec traverse([reather()]) :: reather()

Transform a list of reathers to an reather of a list.

This operation is lazy, so it's never computed until explicitly call Reather.run/2.

examples

Examples

iex> r = [{:ok, 1}, {:ok, 2}, {:ok, 3}]
...>     |> Enum.map(&Reather.of/1)
...>     |> Reather.traverse()
iex> Reather.run(r)
{:ok, [1, 2, 3]}

iex> r = [{:ok, 1}, {:error, "error"}, {:ok, 3}]
...>     |> Enum.map(&Reather.of/1)
...>     |> Reather.traverse()
iex> Reather.run(r)
{:error, "error"}
@spec wrap(any()) :: reather()

Create a Reather from the value. If the value is Reather, it will be returned as is.

examples

Examples

iex> %Reather{} = Reather.wrap(:ok)

iex> r = %Reather{}
iex> ^r = Reather.wrap(r)