tagged v0.4.2 Tagged.Status View Source

Resoning in terms of the status of a result.

iex> require Tagged.Status
iex> import Tagged.Status
iex> ok(:computer)
{:ok, :computer}
iex> with ok(it) <- Keyword.fetch([a: "bacon"], :a), do: "Chunky #{it}!"
"Chunky bacon!"
iex> (Keyword.fetch([], :a)
...>  |> with_error(fn -> ok("bacon") end)
...>  |> with_ok(& "Chunky #{&1}!"))
"Chunky bacon!"
iex> is_error(error())
true
iex> is_error(error("OH NO!"))
true

Link to this section Summary

Functions

Guard macro for testing if term is a ok tagged tuple, with constructor ok/1.

Constructor ok/1 for :ok tagged value tuples. Can also be used to destructure tuples.

Calls f/1 with the wrapped value, when term matches a ok tagged tuple. When term does not match, is is returned as-is.

Link to this section Types

Specs

error() :: :error | {:error, reason :: term()}

Specs

ok() :: {:ok, value :: term()}

Specs

t() :: error() | ok()

Link to this section Functions

Link to this macro

is_error(term)

View Source (macro)

Guard macro for testing if term is a ok tagged tuple, with constructor ok/1.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> f = fn x when is_ok(x) -> x; _ -> nil end
iex> ok(1) |> f.()
{:ok, 1}
iex> {:not_ok, 1} |> f.()
nil

Constructor ok/1 for :ok tagged value tuples. Can also be used to destructure tuples.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> with ok(_) <- {:ok, 1}, do: true
true
iex> with ok(_) <- {:not_ok, 1}, do: true
{:not_ok, 1}

Calls f/1 with the wrapped value, when term matches a ok tagged tuple. When term does not match, is is returned as-is.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> {:ok, 1}
...> |> with_ok(fn _ -> :match end)
:match
iex> {:not_ok, :miss} |> with_ok(& &1)
{:not_ok, :miss}