OkThen.Result.error_or_else

You're seeing just the function error_or_else, go back to OkThen.Result module for more information.
Link to this function

error_or_else(result, func_or_value)

Specs

error_or_else(result_input(), func_or_value(atom(), out)) :: out when out: any()

If result is not tagged with :error, 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. If func_or_value is not a function, then it is used directly as the new value.

If result is tagged with :error, result is returned as-is.

Use this function in a pipeline to branch the happy path into another function, or as a kind of case expression to handle multiple types of result without the boilerplate of copying through an error 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.

Equivalent to tagged_or_else(result, :error, func_or_value). See tagged_or_else/3.

Examples

iex> :ok
...> |> Result.error_or_else(fn
...>   :ok, {} -> {:ok, "hello"}
...> end)
{:ok, "hello"}

iex> {:ok, 1}
...> |> Result.error_or_else(fn
...>   :ok, 1 -> {:ok, "hello"}
...> end)
{:ok, "hello"}

iex> {:ok, 1, 2}
...> |> Result.error_or_else(fn
...>   :ok, {1, 2} -> {:ok, "matched ok"}
...>   :none, {} -> {:ok, "matched none"}
...> end)
{:ok, "matched ok"}

iex> :none
...> |> Result.error_or_else(fn
...>   :ok, {1, 2} -> {:ok, "matched ok"}
...>   :none, {} -> {:ok, "matched none"}
...> end)
{:ok, "matched none"}

iex> {:error, "just error"}
...> |> Result.error_or_else(fn
...>   :ok, {1, 2} -> {:ok, "matched ok"}
...>   :none, {} -> {:ok, "matched none"}
...> end)
{:error, "just error"}

iex> {:ok, 1, 2}
...> |> Result.error_or_else(fn
...>   {1, 2} -> {:ok, "matched two terms"}
...>   1 -> {:ok, "matched one term"}
...>   {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched two terms"}

iex> {:ok, 1}
...> |> Result.error_or_else(fn
...>   {1, 2} -> {:ok, "matched two terms"}
...>   1 -> {:ok, "matched one term"}
...>   {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched one term"}

iex> :ok
...> |> Result.error_or_else(fn
...>   {1, 2} -> {:ok, "matched two terms"}
...>   1 -> {:ok, "matched one term"}
...>   {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched no terms"}

iex> :none
...> |> Result.error_or_else(fn
...>   {1, 2} -> {:ok, "matched two terms"}
...>   1 -> {:ok, "matched one term"}
...>   {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched no terms"}

iex> :ok |> Result.error_or_else({:ok, "hello"})
{:ok, "hello"}

iex> {:ok, 1} |> Result.error_or_else({:ok, "hello"})
{:ok, "hello"}

iex> {:ok, 1} |> Result.error_or_else("bare value")
"bare value"

iex> "bare value" |> Result.error_or_else(fn _ -> :none end)
:none

iex> "bare value" |> Result.error_or_else(fn
...>  :untagged, "bare value" -> :none
...> end)
:none