Skip to content

Iterator

An iterator is a special kind of data that 'points' to the elements of a complex object such as an object or an array. It is especially useful if we want to do something to all the elements of an object/array within a loop. For example:

1
2
3
4
5
6
var myArray = [2, 3, 7, 2, 5];

for (var num in myArray) {
    num *= 2;
}
print(myArray);

In this example, we begin by creating an array called myArray and assigning a bunch of numbers to it. Then, inside a for loop, we create an iterator called num that will point to the elements of myArray. The iterator will cause our for loop to repeat for a total of cycles equivalent to the amount of elements within myArray. And on each cycle, num will point to a different element of myArray.

So, the outcome of this behavior is that the for loop above, will cause each number in myArray to be multiplied by 2. When we finally print myArray, we will see:

1
[4, 6, 14, 4, 10]

Syntax

Usually, iterators are only used within loops, but it is possible to use them outside of loops, like this:

1
2
3
var myArray = [2, 3, 7, 2, 5];
var myIterator in myArray;
print(myIterator);

In this example, we begin by creating an array called myArray. Then, we create an iterator called myIterator. Initially this iterator is pointing to the first element in myArray, which contains the value 2. So, finally, when we print myIterator, we will see:

1
2

If we want our iterator to point at the next element, we can write the following line:

1
@myIterator.next();

Info

If we want to take control of our iterator, instead of controlling the value that the iterator is pointing to, we need to put an @ symbol before the name of our iterator.

If we want our iterator to point at the previous element:

1
@myIterator.back();

To overwrite the element that an iterator is pointing to:

1
myIterator = 17;

To delete the element that an iterator is pointing to:

1
delete myIterator;

To delete an iterator:

1
delete @myIterator;

Iterators are not limited to pointing to arrays and objects. It is possible to create an iterator that just counts from one number to another number:

1
var myIterator in between 1 to 5;

This will create an iterator that starts from the number 1 and finishes after the number 5. The count increases by 1 each time.

It is possible to count backwards:

1
var myIterator in between 10 to 0;

The count here, decreases by 1 each time.

It is possible to have the count increase or decrease by a different amount:

1
var myIterator in between 0 to 100 by 10;

In this example, the count will increase by 10. We can also make it decread by a custom amount:

1
var myIterator in between 1000 to 300 by 150;

Properties

A property is a special feature of a variable that can be used to get and/or set some aspect of that variable.

length

The length property can be used to find out how many elements can be pointed to by this iterator

1
var result = truckHeight.length;

index

The index property can be used to find out the numerical identifier for the element which the iterator is currently pointing to. For example, if the iterator is pointing to the first element of an array or object, then the index property will return 0.

Like most programming languages, in Modulo1 we start counting from zero

1
var result = @myIterator.index;

We can also use the index property to tell our iterator to point to a different element. Regardless of whether our iterator is pointing to an object or an array, however, we must use a number to identify our chosen element. For example, to tell our iterator to point to the fourth element, we would set the index to 3.

1
@myIterator.index = 3;

type

The type property can be used to find out the data-type of a particular number variable. The result will always be "iterator"

1
var result = truckHeight.type;

Functions

back()

The back function causes the iterator to shift its target so that it points to the previous element

1
@myIterator.back();

next()

The next function causes the iterator to shift its target so that it points to the next element. If you shift the iterator so that it points beyond the last element in the object/array, the iterator will then point to null.

1
@myIterator.next();

isAtEnd()

The isAtEnd function returns a boolean (true or false), that indicates whether or not the iterator has exceeded the elements of the object/array that it was pointing to. For example, if the array being pointed to has 6 elements, it means that the last element of that array has an index of 5. If the iterator, though, is pointing to an index of 6, then the iterator has reached the end of the array and is not actually pointing to any elements.

1
var result = @myIterator.isAtEnd();