OkThen.Result.tagged_map
You're seeing just the function
tagged_map
, go back to OkThen.Result module for more information.
Link to this function
tagged_map(result, tag, func_or_value)
Specs
tagged_map(t, tag, func_or_value(out)) :: t | tag | {tag, out} when t: result_input(), tag: atom(), out: any()
If result
is tagged with the specified tag
atom, transforms the wrapped value by passing it
into the provided mapping function, and replacing it with the returned value. If a function is
not provided, the argument at the same position is used as the new value.
If the new value would be nil
, then :none
is returned as the result instead. Consider piping
into |> none_then({tag, nil})
if you really want {tag, nil}
. See none_then/2
.
If result
is not tagged with the specified tag
atom, result
is returned as-is.
Examples
iex> :ok |> Result.tagged_map(:ok, "hello")
{:ok, "hello"}
iex> {:ok, 1} |> Result.tagged_map(:ok, "hello")
{:ok, "hello"}
iex> {:ok, 1} |> Result.tagged_map(:ok, nil)
:none
iex> :none |> Result.tagged_map(:ok, "hello")
:none
iex> :ok |> Result.tagged_map(:ok, fn -> "hello" end)
{:ok, "hello"}
iex> :ok |> Result.tagged_map(:ok, fn {} -> "hello" end)
{:ok, "hello"}
iex> {:bla, 1} |> Result.tagged_map(:bla, fn 1 -> "hello" end)
{:bla, "hello"}
iex> {:some, 1, 2} |> Result.tagged_map(:some, fn {1, 2} -> "hello" end)
{:some, "hello"}
iex> {:ok, 1, 2} |> Result.tagged_map(:ok, fn 1, 2 -> "hello" end)
** (ArgumentError) Value-mapping function must have arity between 0 and 1.
iex> {:ok, 1, 2} |> Result.tagged_map(:ok, fn {1, 2} -> {} end)
:ok
iex> :error |> Result.tagged_map(:ok, fn _ -> "hello" end)
:error
iex> {:error, 1} |> Result.tagged_map(:ok, fn _ -> "hello" end)
{:error, 1}
iex> {:error, 1, 2} |> Result.tagged_map(:ok, fn _ -> "hello" end)
{:error, 1, 2}
iex> :none |> Result.tagged_map(:ok, fn _ -> "hello" end)
:none
iex> :something_else |> Result.tagged_map(:ok, fn _ -> "hello" end)
:something_else
iex> "bare value" |> Result.tagged_map(:ok, fn _ -> "hello" end)
"bare value"
iex> "bare value" |> Result.tagged_map(:untagged, fn _ -> "hello" end)
{:untagged, "hello"}
iex> "bare value" |> Result.tagged_map(:ok, "hello")
"bare value"
iex> "bare value" |> Result.tagged_map(:untagged, "hello")
{:untagged, "hello"}