plymio_funcio v0.1.0 Plymio.Funcio.Index View Source

Utility Functions for Indices

Documentation Terms

See also Plymio.Funcio for an overview and other documentation terms.

index

An index is an integer

indices

An indices is a list of index

index range

An index range is a specification for an indices.

It can include integers, integer ranges, lists of integers or an enumerable that realises to a list of integers.

Examples are:

0
-2
1 .. 5
[99, 1, 2, -1, -2, 4, -1, 0]
[0 .. 4, [5 , 6], 7 .. 9]
0 .. 9 |> Stream.map(&(&1))
[0 .. 4, [5 , 6], 7 .. 9, 0 .. 9, 0 .. 9 |> Stream.map(&(&1))]

Link to this section Summary

Functions

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

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

normalise_index_range/1 takes an index range and realises it to return {:ok, indices}

normalise_indices/1 calls Plymio.Fontais.Utility.list_wrap_flat_just/1 on ist argument and then calls validate_indices/1, returning {:ok, indices}, otherwise {:error, error}

validate_index/1 takes a value and validates it is an integer, returning {:ok, index}, otherwise {:error, error}

validate_indices/1 takes a list and validates each element is an integer, returning {:ok, indices}, otherwise {:error, error}

validate_positive_indices/1 takes a list and validates each element is a postive integer, returning {:ok, indices}, otherwise {:error, error}

Link to this section Types

Link to this section Functions

Link to this function create_predicate_index_range(index_range) View Source
create_predicate_index_range(any()) ::
  {:ok, fun1_predicate()} | {:error, error()}

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

If the index range is an arity 1 function, it is “wrapped” to ensure the result is always true or false.

If the index range is nil an always true predicate will be returned.

Note negative indices will cause an error.

Examples

iex> {:ok, fun} = 42 |> create_predicate_index_range
...> true = {:x, 42} |> fun.()
...> {"HelloWorld", 4} |> fun.()
false

iex> {:ok, fun} = [0,1,2] |> create_predicate_index_range
...> true = {:x, 0} |> fun.()
...> true = {%{a: 1}, 2} |> fun.()
...> true = {42, 2} |> fun.()
...> {"HelloWorld", 4} |> fun.()
false

iex> {:error, error} = [-2,0,-1] |> create_predicate_index_range
...> error |> Exception.message
"indices invalid, got: [-2, -1]"

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

iex> {:ok, fun} = fn _ -> false end |> create_predicate_index_range
...> false = {:x, 0} |> fun.()
...> false = {%{a: 1}, 2} |> fun.()
...> false = {42, 2} |> fun.()
...> {"HelloWorld", 4} |> fun.()
false

iex> {:ok, fun} = fn _ -> 42 end |> create_predicate_index_range
...> true = {:x, 0} |> fun.()
...> true = {%{a: 1}, 2} |> fun.()
...> true = {42, 2} |> fun.()
...> {"HelloWorld", 4} |> fun.()
true

iex> {:ok, fun} = nil |> create_predicate_index_range
...> true = {:x, 0} |> fun.()
...> true = {%{a: 1}, 2} |> fun.()
...> true = {42, 2} |> fun.()
...> {"HelloWorld", 4} |> fun.()
true

iex> {:error, error} = :not_valid |> create_predicate_index_range
...> error |> Exception.message
"index range invalid, got: :not_valid"
Link to this function create_predicate_indices(indices) View Source
create_predicate_indices(any()) :: {:ok, fun1_predicate()} | {:error, error()}

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

Examples

iex> {:ok, fun} = [0,1,2] |> create_predicate_indices
...> true = {:x, 0} |> fun.()
...> true = {%{a: 1}, 2} |> fun.()
...> true = {42, 2} |> fun.()
...> {"HelloWorld", 4} |> fun.()
false

iex> {:error, error} = :not_valid |> create_predicate_indices
...> error |> Exception.message
"indices invalid, got: :not_valid"
Link to this function normalise_index_range(indices) View Source
normalise_index_range(any()) :: {:ok, indices()} | {:error, error()}

normalise_index_range/1 takes an index range and realises it to return {:ok, indices}.

Examples

iex> 1 |> normalise_index_range
{:ok, [1]}

iex> -2 |> normalise_index_range
{:ok, [-2]}

iex> [1,2,3,3,1,2] |> normalise_index_range
{:ok, [1,2,3,3,1,2]}

iex> 0 .. 4 |> normalise_index_range
{:ok, [0,1,2,3,4]}

iex> 0 .. 9 |> Stream.map(&(&1))  |> normalise_index_range
{:ok, [0,1,2,3,4,5,6,7,8,9]}

iex> [0 .. 4, [5 , 6], 7 .. 9, 0 .. 9, 0 .. 9 |> Stream.map(&(&1))]
...> |> normalise_index_range
{:ok, [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]}

iex> {:error, error} = :not_valid |> normalise_index_range
...> error |> Exception.message
"index range invalid, got: :not_valid"
Link to this function normalise_indices(indices) View Source
normalise_indices(any()) :: {:ok, indices()} | {:error, error()}

normalise_indices/1 calls Plymio.Fontais.Utility.list_wrap_flat_just/1 on ist argument and then calls validate_indices/1, returning {:ok, indices}, otherwise {:error, error}

Examples

iex> [1,2,3] |> normalise_indices
{:ok, [1,2,3]}

iex> 42 |> normalise_indices
{:ok, [42]}

iex> {:error, error} = [1,:b,3] |> normalise_indices
...> error |> Exception.message
"index invalid, got: :b"

iex> {:error, error} = [1,:b,:c] |> normalise_indices
...> error |> Exception.message
"indices invalid, got: [:b, :c]"

iex> {:error, error} = :not_an_index |> normalise_indices
...> error |> Exception.message
"index invalid, got: :not_an_index"
Link to this function validate_index(index) View Source
validate_index(any()) :: {:ok, index()} | {:error, error()}

validate_index/1 takes a value and validates it is an integer, returning {:ok, index}, otherwise {:error, error}

Examples

iex> 1 |> validate_index
{:ok, 1}

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

iex> {:error, error} = :not_an_index |> validate_index
...> error |> Exception.message
"index invalid, got: :not_an_index"
Link to this function validate_indices(indices) View Source
validate_indices(any()) :: {:ok, indices()} | {:error, error()}

validate_indices/1 takes a list and validates each element is an integer, returning {:ok, indices}, otherwise {:error, error}

Examples

iex> [1,2,3] |> validate_indices
{:ok, [1,2,3]}

iex> {:error, error} = 42 |> validate_indices
...> error |> Exception.message
"indices invalid, got: 42"

iex> {:error, error} = [1,:b,3] |> validate_indices
...> error |> Exception.message
"index invalid, got: :b"

iex> {:error, error} = [1,:b,:c] |> validate_indices
...> error |> Exception.message
"indices invalid, got: [:b, :c]"

iex> {:error, error} = 42 |> validate_indices
...> error |> Exception.message
"indices invalid, got: 42"
Link to this function validate_positive_indices(indices) View Source
validate_positive_indices(any()) :: {:ok, indices()} | {:error, error()}

validate_positive_indices/1 takes a list and validates each element is a postive integer, returning {:ok, indices}, otherwise {:error, error}

Examples

iex> [1,2,3] |> validate_positive_indices
{:ok, [1,2,3]}

iex> {:error, error} = [1,-1,2] |> validate_positive_indices
...> error |> Exception.message
"index invalid, got: -1"

iex> {:error, error} = 42 |> validate_positive_indices
...> error |> Exception.message
"indices invalid, got: 42"

iex> {:error, error} = [1,:b,3] |> validate_positive_indices
...> error |> Exception.message
"index invalid, got: :b"

iex> {:error, error} = [1,:b,:c] |> validate_positive_indices
...> error |> Exception.message
"indices invalid, got: [:b, :c]"