plymio_funcio v0.1.0 Plymio.Funcio.Enum.Collate View Source

Collate Patterns for Enumerables.

These functions collate the elements of an enum according to one of the defined patterns.

See Plymio.Funcio for overview and documentation terms.

Link to this section Summary

Functions

collate0_enum/2 takes an enum and collates its elements according to pattern 0

collate1_enum/2 takes an enum and collates its elements according to pattern 1

collate2_enum/1 takes an enum and collates its elements according to pattern 2

Link to this section Types

Link to this section Functions

Link to this function collate0_enum(enum) View Source
collate0_enum(any()) :: {:ok, list()} | {:error, error()}

collate0_enum/2 takes an enum and collates its elements according to pattern 0.

If an element is {:ok, value}, the value is added to the accumulated list of values and {:ok, values} is returned.

If any element is {:error, error} or value, the collation is halted, returning {:error, error}.

Examples

iex> enum = [{:ok, 1}, {:ok, 2}, {:ok, 3}]
...> enum |> collate0_enum
{:ok, [1, 2, 3]}

iex> enum = [{:ok, 1}, {:error, %ArgumentError{message: "value is 2"}}, {:ok, 3}]
...> {:error, error} = enum |> collate0_enum
...> error |> Exception.message
"value is 2"

iex> {:error, error} = :not_an_enum |> collate0_enum
...> error |> Exception.message
...> |> String.starts_with?("protocol Enumerable not implemented for :not_an_enum")
true
Link to this function collate1_enum(enum) View Source
collate1_enum(any()) :: {:ok, list()} | {:error, error()}

collate1_enum/2 takes an enum and collates its elements according to pattern 1.

If an element is {:ok, value} or value, the value is added to the accumulated list of values and {:ok, values} is returned.

If any element is {:error, error} the collation is halted, returning the {:error, error}.

Examples

iex> [:a, 2, {:ok, :tre}] |> collate1_enum
{:ok, [:a, 2, :tre]}

iex> enum = [{:ok, 1}, {:error, %ArgumentError{message: "value is 2"}}, {:ok, 3}]
...> {:error, error} = enum |> collate1_enum
...> error |> Exception.message
"value is 2"

iex> {:error, error} = :not_an_enum |> collate1_enum
...> error |> Exception.message
...> |> String.starts_with?("protocol Enumerable not implemented for :not_an_enum")
true
Link to this function collate2_enum(enum) View Source
collate2_enum(any()) :: {:ok, list()} | {:error, error()}

collate2_enum/1 takes an enum and collates its elements according to pattern 2.

If an element is {:ok, value} or value, the value is added to the accumulated list of values and {:ok, values} is returned.

If an element is nil or the unset value (see Plymio.Fontais), the element is dropped and not added to the accumulated values.

If any element is {:error, error} the collation is halted, returning the {:error, error}.

Examples

iex> [{:ok, :a}, nil, {:ok, :tre}] |> collate2_enum
{:ok, [:a, :tre]}

iex> unset_value = Plymio.Fontais.Guard.the_unset_value
...> [unset_value, nil, {:ok, :a}, nil, {:ok, :tre}, unset_value] |> collate2_enum
{:ok, [:a, :tre]}

iex> unset_value = Plymio.Fontais.Guard.the_unset_value
...> [unset_value, nil, {:ok, :a}, nil, :b, {:ok, :c}, unset_value, :d] |> collate2_enum
{:ok, [:a, :b, :c, :d]}

iex> unset_value = Plymio.Fontais.Guard.the_unset_value
...> enum = [unset_value, {:ok, 1}, nil, {:error, %ArgumentError{message: "value is 2"}}, {:ok, 3}]
...> {:error, error} = enum |> collate2_enum
...> error |> Exception.message
"value is 2"

iex> {:error, error} = :not_an_enum |> collate2_enum
...> error |> Exception.message
...> |> String.starts_with?("protocol Enumerable not implemented for :not_an_enum")
true