Iterator AS2 Class

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

Learn more.

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;
                }
        }
}

5 Replies to “Iterator AS2 Class”

  1. 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.

  2. 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.

Comments are closed.