Abstract supertype of containers whose elements may be iterated. An iterable container need not be finite, but its elements must at least be countable. There may not be a well-defined iteration order, and so the order of iterated elements may not be stable.
An instance of Iterable may be iterated using a for
loop:
for (c in "hello world") { ... }
Iterable and its subtypes define various operations
that return other iterable objects. Such operations
come in two flavors:
Lazy operations return a “view” of the receiving iterable object. If the underlying iterable object is mutable, then changes to the underlying object will be reflected in the resulting view. Lazy operations are usually efficient, avoiding memory allocation or iteration of the receiving iterable object.
Eager operations return an immutable object. If the receiving iterable object is mutable, changes to this object will not be reflected in the resulting immutable object. Eager operations are often expensive, involving memory allocation and iteration of the receiving iterable object.
Lazy operations are preferred, because they can be efficiently chained. For example:
string.filter((Character c) c.letter).map((Character c) c.uppercased)
is much less expensive than:
string.select((Character c) c.letter).collect((Character c) c.uppercased)
Furthermore, it is always easy to produce a new immutable iterable object given the view produced by a lazy operation. For example:
{ string.filter((Character c) c.letter).map((Character c) c.uppercased)... }
Lazy operations normally return an instance of
Iterable or Map.
However, there are certain scenarios where an eager operation is more useful, more convenient, or no more expensive than a lazy operation, including:
Eager operations normally return a sequence.
| Attributes | |
| coalesced | Source Code The non-null elements of this |
| empty | Source Code shared actual default Boolean empty Determines if the iterable object is empty, that is to say, if the iterator returns no elements. |
| first | Source Code shared actual default Element? first The first element returned by the iterator, if any.
This should produce the same value as
|
| indexed | Source Code All entries of form { "hello", null, "world" }.indexed
results in an iterable object with the entries
|
| iterator | Source Code An iterator for the elements belonging to this container. |
| last | Source Code shared actual default Element? last The last element returned by the iterator, if any. Iterables are potentially infinite, so calling this might never return; also, this implementation will iterate through all the elements, which might be very time-consuming. |
| rest | Source Code Returns an iterable object containing all but the first element of this container. |
| sequence | Source Code shared default Element[] sequence A sequence containing the elements returned by the iterator. |
| Inherited Attributes |
| Attributes inherited from: Object |
| Attributes inherited from: Container |
| Attributes inherited from: ContainerWithFirstElement<Element,Null> |
| Methods | |
| any | Source Code Return Parameters:
|
| by | Source Code Produce an (0..10).by(3) results in an iterable object with the elements
Throws:
|
| chain | Source Code The elements of this iterable object, in their original order, followed by the elements of the given iterable object also in their original order. |
| collect | Source Code shared default Result[] collect<Result>(Result collecting(Element element)) A sequence containing the results of applying the
given mapping to the elements of this container. An
eager counterpart to Parameters:
See also: map |
| count | Source Code Return the number of elements in this Parameters:
|
| every | Source Code Return Parameters:
|
| filter | Source Code An Parameters:
See also: select |
| find | Source Code The first element which satisfies the given
predicate, if any, or Parameters:
|
| findLast | Source Code The last element which satisfies the given
predicate, if any, or Parameters:
|
| fold | Source Code shared default Result fold<Result>(Result initial, Result accumulating(Result partial, Element elem)) The result of applying the accumulating function to each element of this container in turn. Parameters:
|
| group | Source Code Creates a Map that contains this Parameters:
|
| map | Source Code An Parameters:
See also: collect |
| select | Source Code A sequence containing the elements of this
container that satisfy the given predicate. An
eager counterpart to Parameters:
See also: filter |
| skipping | Source Code Produce an |
| sort | Source Code A sequence containing the elements of this container, sorted according to a function imposing a partial order upon the elements. For convenience, the functions "Hello World!".sort(byIncreasing((Character c) c.lowercased)) This operation is eager by nature. Parameters:
See also: byIncreasing, byDecreasing |
| taking | Source Code Produce an |
| Inherited Methods |
| Methods inherited from: Object |