plymio_funcio v0.1.0 Plymio.Funcio.Enum.Map.Gather View Source

Map and Gather Patterns for Enumerables.

These functions map the elements of an enum and gather the results according to one of the defined patterns.

Gathering means collecting all the :ok and :error results into a opts with keys :ok and :error. The value of each key will be a list of 2tuples where the first element is the enum element and the second is either the value from {:ok, value} or error from {:error, error}. If there are no :ok or :error results, the key will not be present in the final opts.

See Plymio.Funcio for overview and documentation terms.

Link to this section Summary

Functions

map_gather0_enum/2 take an enum and map/1 and applies the map/1 to each element of the enum and then gathers the results according to pattern 0

Link to this section Types

Link to this section Functions

Link to this function map_gather0_enum(enum, fun) View Source
map_gather0_enum(any(), any()) :: {:ok, opts()} | {:error, error()}

map_gather0_enum/2 take an enum and map/1 and applies the map/1 to each element of the enum and then gathers the results according to pattern 0.

Examples

iex> fun = fn v -> {:ok, v * v} end
...> [1,2,3] |> map_gather0_enum(fun)
{:ok, [ok: [{1,1},{2,4},{3,9}]]}

iex> fun = fn
...>  3 -> {:error, %ArgumentError{message: "argument is 3"}}
...>  v -> {:ok, v + 42}
...> end
...> [1,2,3] |> map_gather0_enum(fun)
{:ok, [ok: [{1,43},{2,44}], error: [{3, %ArgumentError{message: "argument is 3"}}]]}

iex> fun = :not_a_fun
...> {:error, error} = [1,2,3] |> map_gather0_enum(fun)
...> error |> Exception.message
"map/1 function invalid, got: :not_a_fun"

iex> fun = fn v -> {:ok, v} end
...> {:error, error} = 42 |> map_gather0_enum(fun)
...> error |> Exception.message
...> |> String.starts_with?("protocol Enumerable not implemented for 42")
true