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