OkThen.Result.or_else

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

or_else(result, func_or_value)

Specs

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

If result is not tagged with :ok, 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 :ok, result is returned as-is.

Use this function in a pipeline to branch the unhappy path into another function, 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.

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

Examples

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

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

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

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

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

iex> {:error, 1, 2}
...> |> Result.or_else(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.or_else(fn
...>   {1, 2} -> {:ok, "matched two terms"}
...>   1 -> {:ok, "matched one term"}
...>   {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched one term"}

iex> :error
...> |> Result.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.or_else(fn
...>   {1, 2} -> {:ok, "matched two terms"}
...>   1 -> {:ok, "matched one term"}
...>   {} -> {:ok, "matched no terms"}
...> end)
{:ok, "matched no terms"}

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

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

iex> {:error, 1} |> Result.or_else("bare value")
"bare value"

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

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