View Source Dreamy.Result (dreamy v1.0.0)

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 If the right side is a function, only calling that function when the left is an error

Types

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

Wrapper for :ok, and :error tuple

Functions

@spec error(v) :: error(v) when v: var

Returns an error Result

Examples

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

Function for flipping results

Examples

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

iex> use Dreamy
...> flip({:error, 3})
{:ok, 3}
@spec from_option(Dreamy.Option.t(v)) :: t(v, nil) when v: var

Build an Result from an Option

Examples

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

iex> use Dreamy
...> option("OK")
...> |> from_option()
{:ok, "OK"}
@spec ok(v) :: ok(v) when v: var

Returns an empty Option

Examples

iex> use Dreamy
...> ok("Hello World")
{:ok, "Hello World"}
@spec to_option(t(v, term())) :: Dreamy.Option.t(v) when v: var

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"}

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

iex> use Dreamy
...> {:error, "world"}
...> |> unwrap("backup")
"backup"
@spec unwrap(t(x, term()), fallback) :: x | fallback when x: var, fallback: var
@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, term()) :: {:ok, a} | {:ok, b} | {:error, err_a}
when a: var, err_a: var, b: var

Function getting the first :ok result if one exists If the right side is a function, only calling that function when the left is an error

Examples

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

iex> use Dreamy
...> {:ok, "success"} ||| fn -> {:ok, "done"} end
{:ok, "success"}

iex> use Dreamy
...> {:error, "some error"} ||| fn -> {:ok, "done"} end
{:ok, "done"}

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"}

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