View Source Arrays.Protocol protocol (Arrays v2.1.1)

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.

Summary

Types

Any datatype implementing the Arrays.Protocol.

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.)

A list of options passed to empty/1.

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.

Should create a new instance of your custom array type.

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.

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.

Types

@type array() :: t()

Any datatype implementing the Arrays.Protocol.

@type 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.)

@type options() :: Keyword.t()

A list of options passed to empty/1.

What options are recognized by a particular implementation varies.

@type t() :: term()
@type 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.

Functions

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

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

Called by Arrays.append/2

@spec empty(options()) :: array()

Should create a new instance of your custom array type.

This is called internally by functions such as Arrays.new/0 and Arrays.empty/1.

NOTE: This function will not be dispatched by normal protocol handling. It will be called directly: The first (and only) parameter will be a list of options.

c.f. t:options.

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

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

Called by Arrays.extract/1

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

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

Called by Arrays.get/2

@spec map(array(), (current_value :: value() -> updated_value :: value())) :: array()

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

Called by Arrays.map/2

@spec 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
@spec 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
@spec replace(array(), index(), item :: any()) :: array()

Replaces the element in array at index with value.

Called by Arrays.replace/3

Link to this function

resize(array, size, default)

View Source
@spec resize(array(), size :: non_neg_integer(), default :: any()) :: array()

Changes the size of the array.

When made smaller, truncates elements beyond the first size elements will be removed. When made larger, new elements will receive default as value.

Called by Arrays.resize/2

@spec 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
@spec 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).

@spec to_list(array()) :: list()

Transforms the array into a list.

Called by Arrays.to_list/1