View Source Dreamy.Result (dreamy v0.2.3)

Functions for use with :ok, and :error tuples

Summary

Types

Wrapper for :ok, and :error tuple

Functions

Returns an error Result

Function for flipping results

Build an Result from an Option

Returns an empty Option

Function to convert results to options

Function for extracting values from :ok records

Function for extracting values from :error tuples

Function getting the first :ok result if one exists

Types

@type t(ok, error) :: {:ok, ok} | {:error, error}

Wrapper for :ok, and :error tuple

Functions

Returns an error Result

Examples

iex> use Dreamy
...> error("err")
{:error, "err"}
@spec flip(t(res, err)) :: t(err, res)

Function for flipping results

Examples

iex> use Dreamy
...> flip({:ok, 3})
{:error, 3}

iex> use Dreamy
...> flip({:error, 3})
{:ok, 3}

Build an Result from an Option

Examples

iex> use Dreamy
...> empty()
...> |> from_option()
{:error, nil}

iex> use Dreamy
...> option("OK")
...> |> from_option()
{:ok, "OK"}

Returns an empty Option

Examples

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

Function to convert results to options

Examples

iex> use Dreamy
...> to_option({:ok, 3})
{Dreamy.Option, 3}

iex> use Dreamy
...> to_option({:error, 3})
{Dreamy.Option, :empty}
@spec unwrap(t(x, y)) :: x | {:error, y} when x: var, y: var

Function for extracting values from :ok records

Examples

iex> use Dreamy
...> {:ok, "hello"}
...> |> unwrap
"hello"

iex> use Dreamy
...> {:error, "world"}
...> |> unwrap
{:error, "world"}
@spec unwrap_error(t(x, y)) :: {:ok, x} | y when x: var, y: var

Function for extracting values from :error tuples

Examples

iex> use Dreamy
...> {:ok, "hello"}
...> |> unwrap_error
{:ok, "hello"}

iex> use Dreamy
...> {:error, "world"}
...> |> unwrap_error
"world"
@spec t(a, err_a) ||| t(b, any()) :: {:ok, a} | {:ok, b} | {:error, err_a}
when a: var, b: var, err_a: var

Function getting the first :ok result if one exists

Examples

iex> use Dreamy
...> {:ok, "success"} ||| {:ok, "success 2"}
{:ok, "success"}

iex> use Dreamy
...> {:error, "oops"} ||| {:ok, "success 2"}
{:ok, "success 2"}

iex> use Dreamy
...> {:error, "oops"} ||| {:error, "darn"}
{:error, "oops"}

iex> use Dreamy
...> {:ok, "first try"} ||| {:error, "darn"} ||| {:ok, "finally"}
{:ok, "first try"}

iex> use Dreamy
...> {:error, "oops"} ||| {:error, "darn"} ||| {:ok, "finally"}
{:ok, "finally"}