Iterator

In computer programming, an iterator is an object that progressively provides access to each item of a collection, in order.

By contrast with a subroutine, a generator coroutine can yield values to its caller multiple times, instead of returning just once.

There are subtle differences and distinctions in the use of the terms "generator" and "iterator", which vary between authors and languages.

[7] Alternative implementations of stream include data-driven languages, such as AWK and sed.

Instead of using an iterator, many languages allow the use of a subscript operator and a loop counter to access each element.

But this approach will negatively impact the iterator performance, since it must effectuate a double pointer following to access the actual data element.

Using an alternative collection (for example a singly linked list instead of a vector) would be a better choice (globally more efficient) if the stability of the iterators is needed.

[15]: 344 [16]: 56 [17]: 89  and an optional Reset() method,[15]: 344  to rewind the enumerator back to its initial position.

The enumerator initially points to a special value before the first element, so a call to MoveNext() is required to begin iterating.

Enumerators are typically obtained by calling the GetEnumerator() method of an object implementing the IEnumerable interface.

However, the foreach statement in C# can operate on any object providing such a method, even if it does not implement IEnumerable (duck typing).

Most containers also provide a separate const_iterator type, for which operations that would allow changing the values pointed to are intentionally not defined.

This approach does not properly separate the advance operation from the actual data access.

Adding or removing elements by calling the methods of the container (also from the same thread) makes the iterator unusable.

An exception is also thrown if there are no more elements remaining (hasNext() has previously returned false).

[18]: 266  Using the enhanced for loop, the preceding example can be rewritten as Some containers also use the older (since 1.0) Enumeration class.

In Scala, iterators have a rich set of methods similar to collections, and can be used directly in for loops.

Implicit conversions are a feature of Scala: methods that, when visible in the current scope, automatically insert calls to themselves into relevant expressions at the appropriate place to make them typecheck when they otherwise would not.

In the case of internal iteration where the user can supply an operation to the iterator to perform over every element of a collection, many built-in operators and MATLAB functions are overloaded to execute over every element of an array and return a corresponding output array implicitly.

Alternatively, it may be desirable to abstract the mechanisms of the array storage container from the user by defining a custom object-oriented MATLAB implementation of the Iterator Pattern.

This is written in the new class-definition syntax introduced with MATLAB software version 7.6 (R2008a) and features a one-dimensional cell array realization of the List Abstract Data Type (ADT) as the mechanism for storing a heterogeneous (in data type) set of elements.

It provides the functionality for explicit forward List traversal with the hasNext(), next() and reset() methods for use in a while-loop.

PHP's foreach loop was introduced in version 4.0 and made compatible with objects as values in 4.0 Beta 4.

[20] However, support for iterators was added in PHP 5 through the introduction of the internal[21] Traversable interface.

The Standard PHP Library provides several classes to work with special iterators.

[24] The simplest implementation is by wrapping an array, this can be useful for type hinting and information hiding.

Iterators in Python are a fundamental part of the language and in many cases go unseen as they are implicitly used in the for (foreach) statement, in list comprehensions, and in generator expressions.

Their usage is hidden behind iteration APIs such as the for statement, map, grep, list indexing with .

All collections provided by the standard library implement the IntoIterator trait (meaning they define the into_iter() method).

Custom collections can implement the IntoIterator trait and return an associated iterator type for their elements, enabling their use directly in for loops.

UML class diagram of the Iterator interface in PHP
UML class diagram of the Iterator interface in PHP