Arrays.Protocol protocol (Arrays v1.1.0) View Source

This protocol is implemented by all array types.

Do not call functions in this module directly if you want to use an array in your code. Instead, use the functions in the Arrays module, which will use the methods of this protocol (as well as the Arrays.Behaviour behaviour) internally.

Link to this section Summary

Types

Any datatype implementing the Arrays.Protocol as well as the Arrays.Behaviour.

An array index can be either a nonnegative index (up to the size of the array), or a negative index (then we count backwards from the size.)

t()

Type of the kind of value stored in the array. In practice, arrays can store anything so this is an alias for any.

Functions

Appends ('pushes') a single element to the end of the array.

Returns which value is currently used as 'default' for elements that have no value of their own.

Extracts ('pops') a single element from the end of the array.

Retrieves the value stored in array of the element at index.

Maps a function over an array, returning a new array.

Reduce an array to a single value, by calling the provided accumulation function for each element, left-to-right.

Reduce an array to a single value, by calling the provided accumulation function for each element, right-to-left.

Replaces the element in array at index with value.

Removes an element from the array array, resetting the element at index to the array's default value.

Changes the size of the array.

The number of elements in the array.

Return a contiguous slice of some elements in the array.

Transforms the array into a list.

Link to this section Types

Specs

array() :: t()

Any datatype implementing the Arrays.Protocol as well as the Arrays.Behaviour.

Specs

index() :: integer()

An array index can be either a nonnegative index (up to the size of the array), or a negative index (then we count backwards from the size.)

Specs

t() :: term()

Specs

value() :: any()

Type of the kind of value stored in the array. In practice, arrays can store anything so this is an alias for any.

Link to this section Functions

Specs

append(array(), item :: any()) :: array()

Appends ('pushes') a single element to the end of the array.

Called by Arrays.append/2

Specs

default(array()) :: any()

Returns which value is currently used as 'default' for elements that have no value of their own.

Called by Arrays.default/1

Specs

extract(array()) :: {:ok, {item :: any(), array()}} | {:error, :empty}

Extracts ('pops') a single element from the end of the array.

Called by Arrays.extract/1

Specs

get(array(), index()) :: any()

Retrieves the value stored in array of the element at index.

Called by Arrays.get/2

Specs

map(array(), (index(), current_value :: value() -> updated_value :: value())) ::
  array()

Maps a function over an array, returning a new array.

Called by Arrays.map/2

Specs

reduce(array(), acc :: any(), (item :: any(), acc :: any() -> any())) :: array()

Reduce an array to a single value, by calling the provided accumulation function for each element, left-to-right.

Note that fun takes the accumulator as second (right) parameter and the item as first (left) parameter.

Called by Arrays.reduce/3

Link to this function

reduce_right(array, acc, fun)

View Source

Specs

reduce_right(array(), acc :: any(), (acc :: any(), item :: any() -> any())) ::
  array()

Reduce an array to a single value, by calling the provided accumulation function for each element, right-to-left.

Note that fun takes the accumulator as first (left) parameter and the item as second (right) parameter.

Called by Arrays.reduce_right/3

Link to this function

replace(array, index, item)

View Source

Specs

replace(array(), index(), item :: any()) :: array()

Replaces the element in array at index with value.

Called by Arrays.replace/3

Specs

reset(array(), index()) :: any()

Removes an element from the array array, resetting the element at index to the array's default value.

Called by Arrays.reset/2

Specs

resize(array(), size :: non_neg_integer()) :: array()

Changes the size of the array.

Called by Arrays.resize/2

Specs

size(array()) :: non_neg_integer()

The number of elements in the array.

Called by Arrays.size/1

Link to this function

slice(array, start_index, amount)

View Source

Specs

slice(array(), index(), non_neg_integer()) :: array()

Return a contiguous slice of some elements in the array.

Handling of bounds is handled in the Arrays module, so we know for certain that 0 <= start_index < size(array) and start_index + length < size(array).

Specs

to_list(array()) :: list()

Transforms the array into a list.

Called by Arrays.to_list/1