| rdfs:comment
| - An iterator or enumerator is an object that traverses through the elements of a collection. Iterators are used to customize the traversal method of a collection. Iterators commonly have two functions: pointing to the current item in the collection, and modifying the iterator itself so that it points to the next item in the collection. Here is a simple iterator for a linked list: Iterators can be created by a collection object itself. Some programming languages generate implicit iterators for some list results. In other programming languages, generators are used to create iterators.
- An iterator is a programming method by which one can loop (iterate) through things in a collection. (For example, an iterator might allow looping through all effects on a creature.) This is generally a means to process the collection one thing at a time. A high-level iterator, such as those provided by NWScript, typically handles all the bookkeeping behind the scenes, allowing the programmer to simply request the next thing, without having to keep track of what has already been processed.
|
| abstract
| - An iterator or enumerator is an object that traverses through the elements of a collection. Iterators are used to customize the traversal method of a collection. Iterators commonly have two functions: pointing to the current item in the collection, and modifying the iterator itself so that it points to the next item in the collection. Operations
* current item - the current item in the collection
* next - moves to the next item
* has next - returns true if the collection has more items Note that the next and has next procedures can be combined so that next is a function returning the result of has next.
* remove (optional) - removes the current item and moves to the next item
* reset (optional) - resets the iterator (moves to the first item in the collection) Here is a simple iterator for a linked list: class linkedlistiterator of t current as listnode of t head as listnode of t new(list as list of t) head = list.head reset() current = head current() as t return current.value next() as boolean current = current.next return not current is null Iterators can be created by a collection object itself. Some programming languages generate implicit iterators for some list results. In other programming languages, generators are used to create iterators. Iterators are commonly used to customize the usage of for each loops. For each loops can be broken down as follows: for each x in col // do something // -to- do x = iterator.current // do something while iterator.next
- An iterator is a programming method by which one can loop (iterate) through things in a collection. (For example, an iterator might allow looping through all effects on a creature.) This is generally a means to process the collection one thing at a time. A high-level iterator, such as those provided by NWScript, typically handles all the bookkeeping behind the scenes, allowing the programmer to simply request the next thing, without having to keep track of what has already been processed. In NWScript, an iterator is typically started with a command whose name starts with "GetFirst", then updated with the corresponding "GetNext" command. These commands return the things in the collection until all such things have been returned. Once the collection has been exhausted, the update (or initialization, if the collection is empty) returns an invalid value that can be tested for. // Loop through the effects on oCreature. effect eSearch = GetFirstEffect(oCreature); // Initialize the iterator while ( GetIsEffectValid(eSearch) ) // Tests to see if the iterator is finished { // Do something with eSearch. // Update the loop. eSearch = GetNextEffect(oCreature); // Increment the iterator } Trying to nest iterators of the same type can confuse NWScript and lead to unintended results. This is often not an issue, but when it is, it can arise subtly: it can easily go unnoticed that an iterator loop calls a function whose implementation contains another iterator loop.
|