OkThen.Result.tagged_or_else
tagged_or_else
, go back to OkThen.Result module for more information.
tagged_or_else(result, tag, func_or_value)
Specs
tagged_or_else(result_input(), atom(), func_or_value(atom(), out)) :: out when out: any()
If result
is not tagged with the specified tag
atom, passes the tag and wrapped value into
the provided function and returns the result. If the function has arity 1, then only the wrapped
value is passed in. An arity-0 function is also accepted. If func_or_value
is not a function,
then it is used directly as the new value.
If result
is tagged with the specified tag
atom, result
is returned as-is.
Use this function in a pipeline to branch away from the happy path, or as a kind of case
expression to handle multiple types of result without the boilerplate of copying through a
successful result.
Be aware that no attempt is made to ensure the return value from the function is a tagged tuple.
However, all functions are tolerant of untagged results, and on input will interpret them as an
{:untagged, value}
tuple.
Examples
iex> :error
...> |> Result.tagged_or_else(:ok, fn
...> :error, {} -> {:ok, "hello"}
...> end)
{:ok, "hello"}
iex> {:error, 1}
...> |> Result.tagged_or_else(:ok, fn
...> :error, 1 -> {:ok, "hello"}
...> end)
{:ok, "hello"}
iex> {:error, 1, 2}
...> |> Result.tagged_or_else(:ok, fn
...> :error, {1, 2} -> {:ok, "matched error"}
...> :none, {} -> {:ok, "matched none"}
...> end)
{:ok, "matched error"}
iex> :none
...> |> Result.tagged_or_else(:ok, fn
...> :error, {1, 2} -> {:ok, "matched error"}
...> :none, {} -> {:ok, "matched none"}
...> end)
{:ok, "matched none"}
iex> {:ok, "just ok"}
...> |> Result.tagged_or_else(:ok, fn
...> :error, {1, 2} -> {:ok, "matched error"}
...> :none, {} -> {:ok, "matched none"}
...> end)
{:ok, "just ok"}
iex> {:error, 1, 2}
...> |> Result.tagged_or_else(:ok, fn
...> {1, 2} -> {:ok, "matched two terms"}
...> 1 -> {:ok, "matched one term"}
...> {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched two terms"}
iex> {:error, 1}
...> |> Result.tagged_or_else(:ok, fn
...> {1, 2} -> {:ok, "matched two terms"}
...> 1 -> {:ok, "matched one term"}
...> {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched one term"}
iex> :error
...> |> Result.tagged_or_else(:ok, fn
...> {1, 2} -> {:ok, "matched two terms"}
...> 1 -> {:ok, "matched one term"}
...> {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched no terms"}
iex> :none
...> |> Result.tagged_or_else(:ok, fn
...> {1, 2} -> {:ok, "matched two terms"}
...> 1 -> {:ok, "matched one term"}
...> {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched no terms"}
iex> :none
...> |> Result.tagged_or_else(:ok, fn -> {:ok, "catch-all value"} end)
{:ok, "catch-all value"}
iex> :error |> Result.tagged_or_else(:ok, {:ok, "hello"})
{:ok, "hello"}
iex> {:error, 1} |> Result.tagged_or_else(:ok, {:ok, "hello"})
{:ok, "hello"}
iex> {:error, 1} |> Result.tagged_or_else(:ok, "bare value")
"bare value"
iex> "bare value" |> Result.tagged_or_else(:ok, fn _ -> :none end)
:none
iex> "bare value" |> Result.tagged_or_else(:ok, fn
...> :untagged, "bare value" -> :none
...> end)
:none
iex> "bare value" |> Result.tagged_or_else(:untagged, fn _ -> :none end)
"bare value"