tagged v0.3.0 Tagged.Outcome View Source

Reasoning in terms of the outcome of an action.

iex> require Tagged.Outcome
iex> import Tagged.Outcome, only: [failure: 1, success: 1]
iex> failure(:is_human)
{:error, :is_human}
iex> with success(it) <- {:ok, "Computer"}, do: "OK, #{it}!"
"OK, Computer!"

Link to this section Summary

Types

Tagged value tuple with a wrapped type t \\ term()

Tagged value tuple with a wrapped type t \\ term()

Functions

Constructor for error tagged value tuples. Can also be used to destructure tuples.

Guard macro for testing if term is a error tagged tuple.

Guard macro for testing if term is a ok tagged tuple.

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

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

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

failure() :: failure(term())

Specs

failure(t)

Tagged value tuple with a wrapped type t \\ term()

Specs

success() :: success(term())

Specs

success(t)

Tagged value tuple with a wrapped type t \\ term()

Link to this section Functions

Link to this macro

failure(value)

View Source (macro)

Constructor for error tagged value tuples. Can also be used to destructure tuples.

iex> require Elixir.Tagged.Outcome
iex> import Elixir.Tagged.Outcome
iex> with failure(val) <- {:error, :match}, do: val
:match
iex> with failure(_) <- {:not_error, :match}, do: true
{:not_error, :match}
Link to this macro

is_failure(term)

View Source (macro)

Guard macro for testing if term is a error tagged tuple.

iex> require Elixir.Tagged.Outcome
iex> import Elixir.Tagged.Outcome
iex> f = fn x when is_failure(x) -> x; _ -> nil end
iex> {:error, true} |> f.()
{:error, true}
iex> {:not_error, true} |> f.()
nil
Link to this macro

is_success(term)

View Source (macro)

Guard macro for testing if term is a ok tagged tuple.

iex> require Elixir.Tagged.Outcome
iex> import Elixir.Tagged.Outcome
iex> f = fn x when is_success(x) -> x; _ -> nil end
iex> {:ok, true} |> f.()
{:ok, true}
iex> {:not_ok, true} |> f.()
nil
Link to this macro

success(value)

View Source (macro)

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

iex> require Elixir.Tagged.Outcome
iex> import Elixir.Tagged.Outcome
iex> with success(val) <- {:ok, :match}, do: val
:match
iex> with success(_) <- {:not_ok, :match}, do: true
{:not_ok, :match}
Link to this macro

with_failure(term, f)

View Source (macro)

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

iex> require Elixir.Tagged.Outcome
iex> import Elixir.Tagged.Outcome
iex> {:error, :match} |> with_failure(& &1)
:match
iex> {:not_error, :match} |> with_failure(& &1)
{:not_error, :match}
Link to this macro

with_success(term, f)

View Source (macro)

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.Outcome
iex> import Elixir.Tagged.Outcome
iex> {:ok, :match} |> with_success(& &1)
:match
iex> {:not_ok, :match} |> with_success(& &1)
{:not_ok, :match}