File: iterator.md

package info (click to toggle)
surgescript 0.5.4.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,876 kB
  • sloc: ansic: 13,674; makefile: 16
file content (42 lines) | stat: -rw-r--r-- 1,535 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Iterator
========

In SurgeScript, Iterator is an abstract contract, or protocol, that define a set of functions that should be written in concrete implementations. If you're familiar with object oriented programming, Iterator is an interface. Iterators are used to traverse containers / collections / data structures and access its elements.

Concrete implementations of Iterator include: `ArrayIterator` and `DictionaryIterator`. If you wish to [write your own iterators](/tutorials/advanced_features#iterators), you need to specify the [functions described below](#functions).

The data structure that you wish to traverse should include an `iterator()` function that spawns the concrete implementation of the Iterator. Thus, the `parent` object of the Iterator will be the structure to be traversed.

The basic usage of an Iterator is as follows:

```
// container is an object such as an Array
iterator = container.iterator(); // spawn an iterator to traverse the container
while(iterator.hasNext()) {
    element = iterator.next();
    Console.print(element);
}
```

Functions
---------

#### hasNext

`hasNext()`

Checks if there are more elements to be accessed in the iteration.

*Returns*

Returns `true` if there are more elements to be visited, or `false` otherwise.

#### next

`next()`

Gets the next element of the container and advances the iteration.

*Returns*

The next element of the container, or `null` if there is no such element. The first call to `next()` returns the first element of the container, and so on.