OkThen.Result.tagged_then

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

tagged_then(result, tag, func_or_value)

Specs

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

If result is tagged with the specified tag atom, passes the wrapped value into the provided function and returns the result. If func_or_value is not a function, then it is returned as-is.

If result is not tagged with the specified tag atom, result is returned as-is.

Use this function to pipe results into functions that return tagged tuples.

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.

A note about unwrapped nils

If result is nil, it is intprereted as :none. This may be slightly unintuitive, so if you're curious, this is the reason:

Untagged results are internally wrapped as an {:untagged, any()} using Result.from_as(value, :untagged), and if value is nil, the return value of Result.from_as/2 will always be :none.

Examples

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

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

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

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

iex> :ok |> Result.tagged_then(:ok, fn -> "bare value" end)
"bare value"

iex> :ok |> Result.tagged_then(:ok, fn {} -> "bare value" end)
"bare value"

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

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

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

iex> {:ok, 1, 2} |> Result.tagged_then(:ok, fn 1, 2 -> {:ok, "hello"} end)
** (ArgumentError) Value-mapping function must have arity between 0 and 1.

iex> {:ok, 1, 2} |> Result.tagged_then(:ok, fn {1, 2} -> {:ok, {}} end)
{:ok, {}}

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

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

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

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

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

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

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

iex> "bare value" |> Result.tagged_then(:ok, :none)
"bare value"

iex> "bare value" |> Result.tagged_then(:untagged, :none)
:none

iex> nil |> Result.tagged_then(:none, :ok)
:ok