View Source Dreamy.Option (dreamy v0.2.3)

Functions for use with Options

Summary

Functions

Build an Option from a Result

Get the value of an option

Get the value of an option, throwing an error if empty

Returns an empty Option

builds an Option from a value

Build an Result from an Option

Types

@type t(t) :: {Dreamy.Option, t | :empty}

Functions

Build an Option from a Result

Examples

iex> use Dreamy
...> from_result({:error, "err"})
{Dreamy.Option, :empty}

iex> use Dreamy
...> from_result({:ok, "OK"})
{Dreamy.Option, "OK"}

Get the value of an option

Examples

iex> use Dreamy
...> empty()
...> |> get()
:error

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

Get the value of an option, throwing an error if empty

Examples

iex> use Dreamy
...> empty()
...> |> get!()
** (RuntimeError) Empty Option

iex> use Dreamy
...> option("Hello World")
...> |> get!()
"Hello World"

Returns an empty Option

Examples

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

builds an Option from a value

Examples

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

iex> use Dreamy
...> empty() == option(nil)
true

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

Build an Result from an Option

Examples

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

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