HaXe Iterators

ActionScript

  1. var myHash:Hash<Int> = new Hash();
  2. myHash.set("one", 1);
  3. myHash.set("two", 1);
  4. myHash.set("three", 2);
  5. myHash.set("four", 3);
  6. myHash.set("five", 5);
  7.  
  8. var total:Int = 0;
  9.  
  10. for ( num in myHash.iterator() ) {
  11. total += num;
  12. trace(num + ": " + total);
  13. }

In computer science, an iterator is an object that allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. An iterator is sometimes called a cursor, especially within the context of a database.
From Wikipedia

In this particular example you don’t need to use .iterator() as the Hash class is iterable, see http://haxe.org/ref/iterators > ‘Iterable Objects’.

NOTE: Use myHash.iterator() (mind the parenthesis) and not myHash.iterator as I’m used to from coding AS3.

Leave a Reply

Your email address will not be published. Required fields are marked *