tagged v0.4.1 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

Functions

failure(arg1) deprecated

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

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

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

success(arg1) deprecated

Constructor success/1 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() :: {:error, reason :: term()}

Specs

success() :: {:ok, result :: term()}

Link to this section Functions

This macro is deprecated. Define your own module, with the proper type constraints..

Constructor failure/1 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(_) <- {:error, 1}, do: true
true
iex> with failure(_) <- {:not_error, 1}, do: true
{:not_error, 1}
Link to this macro

is_failure(term)

View Source (macro)

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

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

is_success(term)

View Source (macro)

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

iex> require Elixir.Tagged.Outcome
iex> import Elixir.Tagged.Outcome
iex> f = fn x when is_success(x) -> x; _ -> nil end
iex> success(1) |> f.()
{:ok, 1}
iex> {:not_ok, 1} |> f.()
nil
This macro is deprecated. Define your own module, with the proper type constraints..

Constructor success/1 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(_) <- {:ok, 1}, do: true
true
iex> with success(_) <- {:not_ok, 1}, do: true
{:not_ok, 1}
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, 1}
...> |> with_failure(fn _ -> :match end)
:match
iex> {:not_error, :miss} |> with_failure(& &1)
{:not_error, :miss}
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, 1}
...> |> with_success(fn _ -> :match end)
:match
iex> {:not_ok, :miss} |> with_success(& &1)
{:not_ok, :miss}