View Source Dreamy.Monodic (dreamy v0.2.1)

Functions for use with both Result and Option monads

Summary

Functions

Alias for ~>>

Function that flattens monads containing other monads

Alias for ~>

Function for applying result tuples & options on success only

Function for applying result tuples & options on success only

Functions

Alias for ~>>

Function that flattens monads containing other monads

Result Examples

iex> use Dreamy
...> flatten(ok(ok("Hello World")))
{:ok, "Hello World"}

iex> use Dreamy
...> flatten(ok(error("Hello World")))
{:error, "Hello World"}

iex> use Dreamy
...> flatten(error(ok("Hello World")))
{:ok, "Hello World"}

iex> use Dreamy
...> flatten(error(error("Hello World")))
{:error, "Hello World"}

Option Examples

iex> use Dreamy
...> flatten(option(option("Hello World")))
{Option, "Hello World"}

iex> use Dreamy
...> flatten(option(empty()))
{Option, :empty}

Alias for ~>

@spec Dreamy.Result.t(res, err) ~> (res -> x) :: Dreamy.Result.t(x, err)
when res: var, x: var
@spec Dreamy.Option.t(v) ~> (v -> x) :: Dreamy.Option.t(x) when v: var, x: var
@spec Dreamy.Result.t(res, err) ~> (res -> Dreamy.Result.t(x, err)) ::
  Dreamy.Result.t(x, err)
when res: var
@spec Dreamy.Option.t(v) ~> (v -> Dreamy.Option.t(x)) :: Dreamy.Option.t(x)
when v: var

Function for applying result tuples & options on success only

Result Examples

iex> use Dreamy
...> {:ok, 1}
...> ~> fn x -> {:ok, x + 1} end
...> ~> fn y -> y + 1 end
3

iex> use Dreamy
...> {:error, 1}
...> ~> fn x -> x + 1 end
{:error, 1}

Option Examples

iex> use Dreamy
...> empty()
...> ~> (fn _ -> :ok end)
{Dreamy.Option, :empty}

iex> use Dreamy
...> option(1)
...> ~> (fn x -> x + 1 end)
{Dreamy.Option, 2}

Function for applying result tuples & options on success only

Result Examples

iex> use Dreamy
...> {:ok, 1}
...> ~>> fn x -> {:ok, x + 1} end
...> ~>> fn x -> {:ok, x * 2} end
{:ok, 4}

iex> use Dreamy
...> {:error, 1}
...> ~>> fn {:ok, x} -> {:ok, x + 1} end
{:error, 1}

Option Examples

iex> use Dreamy
...> empty()
...> ~>> (fn x -> option(x + 2) end)
{Dreamy.Option, :empty}

iex> use Dreamy
...> option(1)
...> ~>> (fn x -> option(x + 2) end)
{Dreamy.Option, 3}