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:
#unsafe_fetch
and#size
do not mutate the collection#each(&)
and#each_index(&)
are not overridden
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
- Enumerable(T)
- Iterable(T)
Direct including types
Defined in:
core_ext/indexable.crviews/indexable_view.cr
Instance Method Summary
-
#[](index : Int, other : Int, *indexes : Int) : self
Returns the elements at the given indexes.
-
#[](indexes : Tuple) : Tuple
Returns the elements at the given indexes.
-
#[](indexes : Indexable(Int)) : self
Returns the elements at the given indexes.
-
#[](indexes : Enumerable(Int)) : self
Returns the elements at the given indexes.
-
#view : View(self, T)
Returns a read-only indexable view to the collection.
Instance Method Detail
Returns the elements at the given indexes. Raises IndexError
if
any index is out of bounds.
arr = "0123456789".chars
arr[8, 3, 6] # => ['8', '3', '6']
arr[[8, 3, 6]] # => ['8', '3', '6']
arr[Set{8, 3, 6}] # => ['8', '3', '6']
arr[{8, 3, 6}] # => {'8', '3', '6'}
arr[*{8, 3, 6}] # => ['8', '3', '6']
arr[7, 25, 1] # raises IndexError
Returns the elements at the given indexes. Raises IndexError
if
any index is out of bounds.
arr = "0123456789".chars
arr[8, 3, 6] # => ['8', '3', '6']
arr[[8, 3, 6]] # => ['8', '3', '6']
arr[Set{8, 3, 6}] # => ['8', '3', '6']
arr[{8, 3, 6}] # => {'8', '3', '6'}
arr[*{8, 3, 6}] # => ['8', '3', '6']
arr[7, 25, 1] # raises IndexError
Returns the elements at the given indexes. Raises IndexError
if
any index is out of bounds.
arr = "0123456789".chars
arr[8, 3, 6] # => ['8', '3', '6']
arr[[8, 3, 6]] # => ['8', '3', '6']
arr[Set{8, 3, 6}] # => ['8', '3', '6']
arr[{8, 3, 6}] # => {'8', '3', '6'}
arr[*{8, 3, 6}] # => ['8', '3', '6']
arr[7, 25, 1] # raises IndexError
Returns the elements at the given indexes. Raises IndexError
if
any index is out of bounds.
arr = "0123456789".chars
arr[8, 3, 6] # => ['8', '3', '6']
arr[[8, 3, 6]] # => ['8', '3', '6']
arr[Set{8, 3, 6}] # => ['8', '3', '6']
arr[{8, 3, 6}] # => {'8', '3', '6'}
arr[*{8, 3, 6}] # => ['8', '3', '6']
arr[7, 25, 1] # raises IndexError