The interface to add the capability of iterating through built-in syntax.
For objects which support vector semantics, it's useful to expose an interface to allow iteration over all the held values. In particular, if the class implements this interface, the iteration can be performed directly with a foreach
statement. class Vector : IIterable {
IIterator getIterator() {
return ...;
}
}
Vector vector = new vector();
for (var ele : vector) { // engine calls getIterator(), which returns a new IIterable.
ele.fun(); // access to each element
}
It might look confusing at the first, but it's also critical to know the difference between IIterable and IIterator. IIterable allows the object to return a view on the contained data, and thus its core method can be called unrestrictedly. IIterator, on the other hand, represents the API provided by this view, exposing the minimally necessary methods for traversing through the data set. Most importantly, IIterator is also a stateful object. The foreach statement is essentially interacting with an IIterator, not an IIterable. IIterable is useful in that it decouples the ability of iteration from the state-tracking during such iteration.
Known Extensions
Type | Name | Signature |
---|---|---|
method | getIterator | public IIterator getIterator() |
method E | all | public bool all(Function) |
method E | any | public bool any(Function) |
method E | append | public IIterable append(var) |
method E | concat | public IIterable concat(var) |
method E | count | public int count() |
method E | distinct | public IIterable distinct() |
method E | except | public IIterable except(System.Util.IIterable) |
method E | filter | public IIterable filter(Function) |
method E | first | public var first() |
method E | first | public var first(bool) |
method E | flatten | public IIterable flatten(Function) |
method E | intersect | public IIterable intersect(System.Util.IIterable) |
method E | last | public var last() |
method E | last | public var last(bool) |
method E | map | public IIterable map(Function) |
method E | reduce | public var reduce(var, Function) |
method E | skip | public IIterable skip(int) |
method E | take | public IIterable take(int) |
method E | toArray | public var toArray() |
method E | toList | public System.Collection.List toList() |
method E | toMap | public System.Collection.Map toMap(Function) |
method E | toMap | public System.Collection.Map toMap(Function, Function) |
method E | union | public IIterable union(System.Util.IIterable) |
method E | zip | public IIterable zip(System.Util.IIterable, Function) |
public IIterator getIterator()
Get an iterator from this object. The iterator provides methods to traverse through the entire data set contained within this object.
Returns