View Source Dreamy.Either (dreamy v1.0.0)

Datatype for representing Either, Or

Summary

Types

Monodic type representing a left or right tuple

Functions

Returns an Either tuple

Applies the function to the left value and flattens

Applies the function to the right value and flattens

Returns the left value from an Either tuple

Returns the right value from an Either tuple

Returns an Either tuple with left value populated, and the right nil

Applies the function to the left value

Applies the function to the right value

Returns an empty Either tuple, aka a Neither

Returns an Either tuple with right value populated, and the left nil

Wraps the left value in a Dreamy.Option, returns an empty if the value is nil

Wraps the right value in a Dreamy.Option, returns an empty if the value is nil

Types

@type left(l) :: t(l, nil)
@type neither() :: t(nil, nil)
@type right(r) :: t(nil, r)
@type t(l, r) :: {Dreamy.Either, l, r}

Monodic type representing a left or right tuple

Functions

@spec either(l, r) :: t(l, r) when l: var, r: var

Returns an Either tuple

Examples

iex> use Dreamy
...> either(:l, :r)
{Dreamy.Either, :l, :r}
@spec flat_map_left(left(l), (l -> left(res))) :: left(res) when l: var, res: var
@spec flat_map_left(right(r), (nil -> left(term()))) :: right(r) when r: var
@spec flat_map_left(neither(), (nil -> left(term()))) :: neither()

Applies the function to the left value and flattens

Examples

iex> use Dreamy
...> left("left")
...> |> flat_map_left(fn l -> left(l <> "!") end)
{Dreamy.Either, "left!", nil}

iex> use Dreamy
...> right(:r)
...> |> flat_map_left(fn l -> left(l <> "!") end)
{Dreamy.Either, nil, :r}

iex> use Dreamy
...> neither()
...> |> flat_map_left(fn l -> left(l <> "!") end)
{Dreamy.Either, nil, nil}
@spec flat_map_right(right(r), (r -> right(res))) :: right(res) when r: var, res: var
@spec flat_map_right(left(l), (nil -> right(term()))) :: left(l) when l: var
@spec flat_map_right(neither(), (nil -> right(term()))) :: neither()

Applies the function to the right value and flattens

Examples

iex> use Dreamy
...> right("right")
...> |> flat_map_right(fn r -> right(r <> "!") end)
{Dreamy.Either, nil, "right!"}

iex> use Dreamy
...> left(:l)
...> |> flat_map_right(fn r -> right(r <> "!") end)
{Dreamy.Either, :l, nil}

iex> use Dreamy
...> neither()
...> |> flat_map_right(fn r -> right(r <> "!") end)
{Dreamy.Either, nil, nil}
@spec get_left(t(l, term())) :: l when l: var

Returns the left value from an Either tuple

Examples

iex> use Dreamy
...> either(:l, :r)
...> |> get_left()
:l
@spec get_right(t(term(), r)) :: r when r: var

Returns the right value from an Either tuple

Examples

iex> use Dreamy
...> either(:l, :r)
...> |> get_right()
:r
@spec left(l) :: left(l) when l: var

Returns an Either tuple with left value populated, and the right nil

Examples

iex> use Dreamy
...> left(:l)
{Dreamy.Either, :l, nil}
@spec map_left(left(l), (l -> res)) :: left(res) when l: var, res: var
@spec map_left(right(r), (nil -> term())) :: right(r) when r: var
@spec map_left(neither(), (nil -> term())) :: neither()

Applies the function to the left value

Examples

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

iex> use Dreamy
...> right(:r)
...> |> map_left(&Atom.to_string/1)
{Dreamy.Either, nil, :r}

iex> use Dreamy
...> neither()
...> |> map_left(&Atom.to_string/1)
{Dreamy.Either, nil, nil}
@spec map_right(right(r), (r -> res)) :: right(res) when r: var, res: var
@spec map_right(left(l), (nil -> term())) :: left(l) when l: var
@spec map_right(neither(), (nil -> term())) :: neither()

Applies the function to the right value

Examples

iex> use Dreamy
...> right(:r)
...> |> map_right(&Atom.to_string/1)
{Dreamy.Either, nil, "r"}

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

iex> use Dreamy
...> neither()
...> |> map_right(&Atom.to_string/1)
{Dreamy.Either, nil, nil}
@spec neither() :: neither()

Returns an empty Either tuple, aka a Neither

Examples

iex> use Dreamy
...> neither()
{Dreamy.Either, nil, nil}
@spec right(r) :: right(r) when r: var

Returns an Either tuple with right value populated, and the left nil

Examples

iex> use Dreamy
...> right(:r)
{Dreamy.Either, nil, :r}
@spec to_option_left(left(l)) :: Dreamy.Types.option(l) when l: var
@spec to_option_left(right(term())) :: Dreamy.Option.empty()

Wraps the left value in a Dreamy.Option, returns an empty if the value is nil

Examples

iex> use Dreamy
...> left(:l)
...> |> to_option_left()
{Dreamy.Option, :l}

iex> use Dreamy
...> right(:r)
...> |> to_option_left()
{Dreamy.Option, :empty}
@spec to_option_right(right(r)) :: Dreamy.Types.option(r) when r: var
@spec to_option_right(left(term())) :: Dreamy.Option.empty()

Wraps the right value in a Dreamy.Option, returns an empty if the value is nil

Examples

iex> use Dreamy
...> right(:r)
...> |> to_option_right()
{Dreamy.Option, :r}

iex> use Dreamy
...> left(:l)
...> |> to_option_right()
{Dreamy.Option, :empty}