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:
lib/views/src/views/indexable_view.crchem/core_ext/indexable.cr
Instance Method Summary
-
#sentence(io : IO, separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ", & : T, IO -> ) : Nil
Prints the elements in the collection as a sentence to io.
-
#sentence(io : IO, separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ") : Nil
Prints the elements in the collection as a sentence to io.
-
#sentence(separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ", & : T -> ) : String
Returns a
String
by concatenating the elements in the collection as a sentence. -
#sentence(separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ") : String
Returns a
String
by concatenating the elements in the collection as a sentence.
Instance methods inherited from module Enumerable(T)
average(weights : Indexable(Number))average(weights : Indexable(Number), & : T -> _) average, mean
mean(& : T -> _) mean
Instance Method Detail
Prints the elements in the collection as a sentence to io. How each element is printed is controlled by the given block.
The output depends on the number of elements in the collection:
- If there are three or more elements, all but the tail element in the collection are joined by separator, and the tail element is joined by tail_separator.
- If there are two elements, these are joined by pair_separator.
- If there is one element, it is printed to io without any separator.
- If the collection is empty, nothing is printed.
[1, 2, 3].sentence(STDOUT, "-", tail_separator: "-or-") { |e, io| io << "(#{e})" }
Prints:
(1)-(2)-or-(3)
Prints the elements in the collection as a sentence to io.
The output depends on the number of elements in the collection:
- If there are three or more elements, all but the tail element in the collection are joined by separator, and the tail element is joined by tail_separator.
- If there are two elements, these are joined by pair_separator.
- If there is one element, it is printed to io without any separator.
- If the collection is empty, nothing is printed.
[1, 2, 3].sentence(STDOUT, "-", tail_separator: "-or-")
Prints:
1-2-or-3
Returns a String
by concatenating the elements in the collection
as a sentence. How each element is printed is controlled by the
given block.
The string representation depends on the number of elements in the collection:
- If there are three or more elements, all but the tail element in the collection are joined by separator, and the tail element is joined by tail_separator.
- If there are two elements, these are joined by pair_separator.
- If there is one element, it is returned as is.
- If the collection is empty, an empty string is returned.
[1, 2, 3].sentence { |e| "(#{e})" } # => "(1), (2), and (3)"
[1, 2, 3].sentence("-", tail_separator: "-or-") { |e| "(#{e})" } # => "(1)-(2)-or-(3)"
Returns a String
by concatenating the elements in the collection
as a sentence.
The string representation depends on the number of elements in the collection:
- If there are three or more elements, all but the tail element in the collection are joined by separator, and the tail element is joined by tail_separator.
- If there are two elements, these are joined by pair_separator.
- If there is one element, it is returned as is.
- If the collection is empty, an empty string is returned.
([] of Int32).sentence # => ""
[1].sentence # => "1"
[1, 2].sentence # => "1 and 2"
[1, 2].sentence(pair_separator: "-or-") # => "1-or-2"
[1, 2, 3].sentence # => "1, 2, and 3"
[1, 2, 3].sentence("-", tail_separator: "-or-") # => "1-2-or-3"