OkThen.Result.Enum (ok_then v0.1.1)

Functions for processing tagged tuples inside Enums.

Link to this section Summary

Functions

Collects an Enum of results into a single result. If all results were tagged :ok, then a result will be returned tagged with :ok, whose value is a list of the wrapped values from each element in the list. Otherwise, the result whose tag didn't match tag is returned.

Collects an Enum of results into a single result. If all results were tagged with the specified tag, then a result will be returned tagged with tag, whose value is a list of the wrapped values from each element in the list. Otherwise, the result whose tag didn't match tag is returned.

Collects an Enum of results into a map, with result values grouped by their tag.

Link to this section Functions

Link to this function

collect(results)

Specs

collect([OkThen.Result.tagged()]) :: {atom(), [any()]}

Collects an Enum of results into a single result. If all results were tagged :ok, then a result will be returned tagged with :ok, whose value is a list of the wrapped values from each element in the list. Otherwise, the result whose tag didn't match tag is returned.

Equivalent to collect_tagged(results, :ok). See collect_tagged/2.

Examples

iex> [:ok, :ok]
...> |> Result.Enum.collect()
{:ok, [{}, {}]}

iex> [:ok, :ok, :ok, :error, {:error, 2}]
...> |> Result.Enum.collect()
{:error, {}}

iex> [{:ok, 1}, {:ok, 1, 2}, :ok]
...> |> Result.Enum.collect()
{:ok, [1, {1, 2}, {}]}

iex> [{:ok, 1}, {:ok, 1, 2}, {:something, 1}, :ok]
...> |> Result.Enum.collect()
{:something, 1}
Link to this function

collect_tagged(results, tag)

Specs

collect_tagged([OkThen.Result.tagged()], atom()) :: {atom(), [any()]}

Collects an Enum of results into a single result. If all results were tagged with the specified tag, then a result will be returned tagged with tag, whose value is a list of the wrapped values from each element in the list. Otherwise, the result whose tag didn't match tag is returned.

Examples

iex> [:ok, :ok]
...> |> Result.Enum.collect_tagged(:ok)
{:ok, [{}, {}]}

iex> [:ok, :ok, :ok, :error, {:error, 2}]
...> |> Result.Enum.collect_tagged(:ok)
{:error, {}}

iex> [{:ok, 1}, {:ok, 1, 2}, :ok]
...> |> Result.Enum.collect_tagged(:ok)
{:ok, [1, {1, 2}, {}]}

iex> [{:ok, 1}, {:ok, 1, 2}, {:something, 1}, :ok]
...> |> Result.Enum.collect_tagged(:ok)
{:something, 1}
Link to this function

group_by_tag(results)

Specs

group_by_tag([OkThen.Result.tagged()]) :: %{required(atom()) => [any()]}

Collects an Enum of results into a map, with result values grouped by their tag.

Examples

iex> [:ok, :ok, :ok, :error, :error]
...> |> Result.Enum.group_by_tag()
%{
  error: [{}, {}],
  ok: [{}, {}, {}]
}

iex> [{:ok, 1}, {:ok, 2}, {:ok, 3}, {:error, 4}, {:error, 5}]
...> |> Result.Enum.group_by_tag()
%{
  error: [4, 5],
  ok: [1, 2, 3]
}

iex> [{:ok, 1}, {:ok, 2, 3}, :none, {:error, 4}, {:another, 5}]
...> |> Result.Enum.group_by_tag()
%{
  another: [5],
  error: [4],
  none: [{}],
  ok: [1, {2, 3}]
}