module Indexable(T)

Overview

A container that allows accessing elements via a numeric index.

Indexing starts at 0. A negative index is assumed to be relative to the end of the container: -1 indicates the last element, -2 is the next to last element, and so on.

Types including this module are typically Array-like types.

Stability guarantees

Several methods in Indexable, such as #bsearch and #cartesian_product, require the collection to be stable; that is, calling #each(&) over and over again should always yield the same elements, provided the collection is not mutated between the calls. In particular, #each(&) itself should not mutate the collection throughout the loop. Stability of an Indexable is guaranteed if the following criteria are met:

The standard library assumes that all including types of Indexable are always stable. It is undefined behavior to implement an Indexable that is not stable or only conditionally stable.

Included Modules

Defined in:

collection/core_ext.cr

Instance Method Summary

Instance methods inherited from module Collection(Int32, T)

[](key_or_index : X) : Y [], []?(key_or_index : X) : Y | Nil []?, dig(key : X, *subkeys) dig, dig?(key : X, *subkeys) dig?, each_pair(&block : Tuple(X, Y) -> ) : Nil
each_pair : Iterator(Tuple(X, Y))
each_pair
, nested? : Bool nested?

Instance Method Detail

def each_pair(&) : Nil #
Description copied from module Collection(Int32, T)

Must yield this structure's key, value or index, element pairs.


[View source]
def each_pair : Iterator(Tuple(Int32, T)) #
Description copied from module Collection(Int32, T)

Provides an Iterator for each pair in self.

NOTE when the base collection provides it own iterator implementation, this method should be overridden to use this directly, this is a generalised implementation and likely sub-optimal in many cases.


[View source]
def traverse(*prefix : *T, &) : Nil forall T #

Traverse the structure depth first yielding a tuple of the path through the structure and the leaf value for every element to the passed block.

The paths contain a tuple of the keys or indicies used across intermediate structures, similar to what would be passed to #dig.


[View source]
def traverse(*prefix : *T) : Iterator forall T #

Provide an iterator that may be used to traverse a nested structure.


[View source]