Loops and iterators in Haxe

posted on Sep 06, 2014 in Haxe, Syntax
loop

Loops are widely used in all programming languages to repeat an action until a condition is met.

Besides the usual while loop, Haxe employs iterators, which can then be used in for loops. This means that for loops are slightly different from the usual C++ syntax.

Let's take a closer look at the supported loops.

The while loop follows the usual pattern and lets you keep executing an action until a condition is met. There are two ways to write it down. This first way to define a while loop is to enter the condition in the brackets after the "while" keyword:

var i:Int = 0;
while (i < 10) {
	// action
	i++;
}

The second way is to use an additional "do" keyword:

var i:Int = 0;
do {
	// action
	i++;
}while(i < 10);

For loops in Haxe use iterators to loop over. An iterator is an object of the Iterator or Iterable type, which provide an interface to return elements from a set.

The simplest iterator can be created using the ... operator:

var i:Int;
for (i in 0...10) {
	trace(i);
}

The 0...10 expression creates a numeric iterator which runs the cycle 10 times, each time incrementing the i value by 1.

The trace function in this case prints the i value to the console when testing the application.

Using this method, it is possible to iterate over an array:

var i:Int;
var arr:Array = ["Hello", "world", "how", "are", "you"];
for (i in 0...arr.length) {
	trace(arr[i]);
}

Creating a custom iterator class is easy. As I mentioned above, two types can be used in for loops - Iterator and Iterable. An Iterator is a class that has two methods: hasNext() and next(), which return the availability of the next element and its value. An Iterable should have a method iterator(), which returns an Iterator object.

Let's create a custom iterator which lets us loop over a string to print out each character individually.

Here is the code:

class StringIterator
{
	private var str:String;
	private var i:Int;

	public function new(str:String) 
	{
		this.str = str;
		i = 0;
	}
	
	public function hasNext():Bool {
		return i < str.length;
	}
	
	public function next():String {
		return str.charAt(i++);
	}
	
}

We declare 2 variables, one for holding the string and one for the position in the set.

The new() constructor will be used to pass the string value to the iterator.

The hasNext() method returns a true or false value based on whether we reached the end of the string.

The next() method will print the character at current position and then increment the position by 1.

This iterator can later be used like this:

var iterator:StringIterator = new StringIterator("Hello");
var char:String;
for (char in iterator) {
	trace(char);
}

The debugger console will print out each character of the "Hello" string individually.

And that's how you use loops and iterators in Haxe. As you can see, this kind of concept is easy to use and customize and will surely speed up your development.

In the next tutorial we will learn how to use Enums in Haxe.

15257