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

Functions for an Enumerable’s Indices

Documentation Terms

index range

See Plymio.Funcio.Index for an explanation of index range

See Plymio.Funcio for an overview and explanation of other terms used in the documentation.

Link to this section Summary

Functions

create_predicate_index_range_enum/1 takes an enum and index range and creates an arity 1 function that expects to be passed a {value, index} 2tuple and returns true if index is in the index range, else false

normalise_index_range_enum/1 takes an enum and index range, normalises the index range and converts negative indices to their absolute (zero offset) values

validate_index_enum/2 takes an enum and index, converts the index into its absolute (zero offset) value and then confirms the index is valid for the enum, returning {:ok, positive_index}

validate_indices_enum/2 takes an enum and indices, converts negative indices into their absolute (zero offset) values and then confirms each index is valid for the enum, returning {:ok, positive_indices}

Link to this section Types

Link to this section Functions

Link to this function create_predicate_index_range_enum(enum, index_range \\ nil) View Source
create_predicate_index_range_enum(any(), any()) ::
  {:ok, fun1_predicate()} | {:error, error()}

create_predicate_index_range_enum/1 takes an enum and index range and creates an arity 1 function that expects to be passed a {value, index} 2tuple and returns true if index is in the index range, else false.

Examples

iex> {:ok, fun} = [1,2,3] |> create_predicate_index_range_enum(0)
...> true = {:x, 0} |> fun.()
...> {"HelloWorld", 2} |> fun.()
false

iex> {:ok, fun} = 0 .. 9 |> Enum.to_list |> create_predicate_index_range_enum(0 .. 2)
...> true = {:x, 0} |> fun.()
...> true = {%{a: 1}, 2} |> fun.()
...> true = {42, 2} |> fun.()
...> {"HelloWorld", 4} |> fun.()
false

iex> {:error, error} = [] |> create_predicate_index_range_enum(:not_valid)
...> error |> Exception.message
"index range invalid, got: :not_valid"
Link to this function normalise_index_range_enum(enum, index_range) View Source
normalise_index_range_enum(any(), any()) ::
  {:ok, indices()} | {:error, error()}

normalise_index_range_enum/1 takes an enum and index range, normalises the index range and converts negative indices to their absolute (zero offset) values.

Finally it confirms each index is valid for the enum, returning {:ok, positive_indices}.

Examples

iex> [1,2,3] |> normalise_index_range_enum(0)
{:ok, [0]}

iex> [1,2,3] |> normalise_index_range_enum(-1)
{:ok, [2]}

iex> [1,2,3] |> normalise_index_range_enum([0,-1])
{:ok, [0,2]}

iex> [1,2,3] |> normalise_index_range_enum(0 .. 2)
{:ok, [0,1,2]}

iex> [1,2,3] |> normalise_index_range_enum([0,-1,0,2])
{:ok, [0,2,0,2]}

iex> {:error, error} = [1,2,3] |> normalise_index_range_enum(4)
...> error |> Exception.message
"index invalid, got: 4"

iex> {:error, error} = [1,2,3] |> normalise_index_range_enum([0,-1,4,5,0,2])
...> error |> Exception.message
"indices invalid, got: [4, 5]"

iex> {:error, error} = [1,2,3] |> normalise_index_range_enum(:not_valid)
...> error |> Exception.message
"index range invalid, got: :not_valid"
Link to this function validate_index_enum(enum, index) View Source
validate_index_enum(any(), any()) :: {:ok, index()} | {:error, error()}

validate_index_enum/2 takes an enum and index, converts the index into its absolute (zero offset) value and then confirms the index is valid for the enum, returning {:ok, positive_index}.

Examples

iex> [1,2,3] |> validate_index_enum(0)
{:ok, 0}

iex> [1,2,3] |> validate_index_enum(-1)
{:ok, 2}

iex> {:error, error} = [1,2,3] |> validate_index_enum(4)
...> error |> Exception.message
"index too large, got: 4"

iex> {:error, error} = [1,2,3] |> validate_index_enum(-999)
...> error |> Exception.message
"index too small, got: -999"

iex> {:error, error} = [1,2,3] |> validate_index_enum(:not_valid)
...> error |> Exception.message
"index invalid, got: :not_valid"
Link to this function validate_indices_enum(enum, indices) View Source
validate_indices_enum(any(), any()) :: {:ok, indices()} | {:error, error()}

validate_indices_enum/2 takes an enum and indices, converts negative indices into their absolute (zero offset) values and then confirms each index is valid for the enum, returning {:ok, positive_indices}.

Examples

iex> [1,2,3] |> validate_indices_enum(0)
{:ok, [0]}

iex> [1,2,3] |> validate_indices_enum(-1)
{:ok, [2]}

iex> [1,2,3,4,5] |> validate_indices_enum([-1,0,2])
{:ok, [4,0,2]}

iex> {:error, error} = [1,2,3] |> validate_indices_enum(4)
...> error |> Exception.message
"index invalid, got: 4"

iex> {:error, error} = [1,2,3] |> validate_indices_enum([0,-1,4,5,0,2])
...> error |> Exception.message
"indices invalid, got: [4, 5]"

iex> {:error, error} = [1,2,3] |> validate_indices_enum(:not_valid)
...> error |> Exception.message
"index invalid, got: :not_valid"