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

Direct including types

Defined in:

core_ext/indexable.cr
views/indexable_view.cr

Instance Method Summary

Instance Method Detail

def [](index : Int, other : Int, *indexes : Int) : self #

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

[View source]
def [](indexes : Tuple) : Tuple #

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

[View source]
def [](indexes : Indexable(Int)) : self #

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

[View source]
def [](indexes : Enumerable(Int)) : self #

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

[View source]
def view : View(self, T) #

Returns a read-only indexable view to the collection.


[View source]