View Source Dreamy.Monodic (dreamy v1.0.0)

Functions for use with a monads defined by Dreamy, including Result, Either & Option monads

Summary

Functions

Alias for Dreamy.Monodic.~>>/2

Function that flattens monads containing other monads

Alias for Dreamy.Monodic.~>/2

Function for applying result tuples & options on success only

Function for applying result tuples & options on success only

Types

Functions

@spec flat_map(Dreamy.Result.t(res, err), (res -> Dreamy.Result.t(x, err))) ::
  Dreamy.Result.t(x, err)
when res: var
@spec flat_map(Dreamy.Option.t(v), (v -> Dreamy.Option.t(x))) :: Dreamy.Option.t(x)
when v: var
@spec flat_map(Dreamy.Either.t(l, r), (l -> Dreamy.Result.t(x, r))) ::
  Dreamy.Either.t(x, r)
when l: var

Alias for Dreamy.Monodic.~>>/2

@spec flatten(Dreamy.Result.t(Dreamy.Result.t(ok, err), term())) ::
  Dreamy.Result.t(ok, err)
@spec flatten(Dreamy.Result.t(term(), Dreamy.Result.t(ok, err))) ::
  Dreamy.Result.t(ok, err)
@spec flatten(Dreamy.Option.t(Dreamy.Option.t(v))) :: Dreamy.Option.t(v)
@spec flatten(Dreamy.Either.t(Dreamy.Either.t(l, r), term())) :: Dreamy.Either.t(l, r)
@spec flatten(Dreamy.Either.t(term(), Dreamy.Either.t(l, r))) :: Dreamy.Either.t(l, r)

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}

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

Either Examples

iex> use Dreamy
...> flatten(left(right("Hello World")))
{Either, nil, "Hello World"}

iex> use Dreamy
...> flatten(right(left("Hello World")))
{Either, "Hello World", nil}
@spec map(Dreamy.Result.t(res, err), (res -> x)) :: Dreamy.Result.t(x, err)
when res: var, x: var
@spec map(Dreamy.Option.t(v), (v -> x)) :: Dreamy.Option.t(x) when v: var, x: var
@spec map(Dreamy.Either.t(l, r), (l -> x)) :: Dreamy.Either.t(x, r)
when l: var, x: var

Alias for Dreamy.Monodic.~>/2

@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.Either.t(l, r) ~> (l -> x) :: Dreamy.Either.t(x, r) when l: var, x: 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}

## Either Examples

iex> use Dreamy ...> left(:l) ...> ~> &Atom.to_string/1 {Dreamy.Either, "l", nil}

iex> use Dreamy ...> left(100) ...> ~> (fn l -> l + 1 end) {Dreamy.Either, 101, nil}

@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
@spec Dreamy.Either.t(l, r) ~>> (l -> Dreamy.Result.t(x, r)) :: Dreamy.Either.t(x, r)
when l: var

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}