Here’s an ActionScript 2.0 Iterator class. You typically extend this since the one I provide is a base class. Here’s an example:
dialogue_array = new Iterator(); dialogue_array[0] = "Sup!?"; dialogue_array[1] = "Not much, mangxt."; dialogue_array[2] = "Cool... btw, your inside an Iterator, sucka!"; trace(dialogue_array.first()); trace(dialogue_array.isDone); trace(dialogue_array.next()); trace(dialogue_array.isDone); trace(dialogue_array.next()); trace(dialogue_array.isDone);
Download – AS
Code follows in extended entry.
class Iterator extends Array { private var _current:Number; // --------------------------------------------------------------- public function get current():Number { return _current; } // --------------------------------------------------------------- public function get isDone():Boolean { return ( (current + 1) >= this.length) ? true : false; } // --------------------------------------------------------------- function Iterator() { super(); // via convention, does technically nothing of value _current = -1; } // --------------------------------------------------------------- public function first(Void):Object { _current = 0; return this[_current]; } // --------------------------------------------------------------- public function next(Void):Object { if(_current + 1 < this.length) { _current++; return this[_current]; } else { return null; } } }
Handy, I’ll put this to good use. I have a couple utility classes I need to clean up to follow good oop and post. Thanks again.
BTW, the Remoting classes for AS 2.0 come with a Collection and an Iterator class: http://www.richinternet.de/blog/index.cfm?entry=9E87C1C4-0CB7-9774-90A22EE4B3CC326D
Iterator is just a pattern Jessie – it’s better when it is an interface, not implementation. And you have it here: mx.utils.Iterator
If you want to have implementation, I think you should make it as mix-in, similar to EventDispatcher.
That’s a pretty hot example Dirk, thank you! I can see how it’s an interface, much like DataProvider is.
This was the result of just getting bored, going here:
http://www.dofactory.com/patterns/Patterns.aspx
And ending up writing a class I figured someone might want in AS2.
The as2lib (www.as2lib.org) offers a whole set of different iterators for arrays, maps, stacks, queues that all implement the org.as2lib.data.holder.Iterator interface.