Ruby Iterators for Actionscript Part III

Posted by Ben Jackson Tue, 10 Jan 2006 19:30:00 GMT

Just saw that a code snippet I posted in a comment seems to have expired, so I'm posting this up where I know it'll stay put. It's an AS2 class built on top of Robin Hillard's Ruby Iterators for ActionScript in AS1. Take a look at the original article for more details.

    class au.com.rocketboots.utils.RubyScript {
      private static var _iteratorsAdded:Boolean;

      public static function addIterators()
      {
        if (_iteratorsAdded) {
          return;
        } else {
          _iteratorsAdded = true;
        }

        /************************
            Array methods
         ************************/

        Array.prototype.each = function(block : Function) : Array {
            var l = this.length;
            for(var i = 0; i < l; i++)
                block(this[i], i);
            return this;
        }

        Array.prototype.filter = function(block : Function) : Array {
            var l = this.length;
            var result = [];
            for(var i = 0; i < l; i++) {
                if (block(this[i], i))
                    result.push(this[i], i);
            }
            return result;
        }

        Array.prototype.collect = function(block : Function) : Array {
            var l = this.length;
            var result = [];
            for(var i = 0; i < l; i++) 
                result.push(block(this[i], i));
            return result;
        }

        Array.prototype.find = function(block : Function) : Array {
            var l = this.length;
            for(var i = 0; i < l; i++) {
                if (block(this[i], i))
                    return this[i];
            }
        }

        Array.prototype.inject = function(block : Function) : Array {
            var l = this.length;
            var a = this[0];
            for(var i = 1; i < l; i++)
                a = block(a, this[i])
            return a;
        }

        /************************
            Object methods
         ************************/

         Object.prototype.each = function(block : Function) : Array {
            var l = this.length;
            for(var i in this)
                if ((typeof(this[i])) != "function") block(this[i], i);
            return this;
        }

        Object.prototype.filter = function(block : Function) : Array {
            var l = this.length;
            var result = [];
            for(var i in this) {
                if ((typeof(this[i])) != "function")
                    if (block(this[i], i))
                        result.push(this[i], i);
            }
            return result;
        }

        Object.prototype.collect = function(block : Function) : Array {
            var l = this.length;
            var result = [];
            for(var i in this) 
                if ((typeof(this[i])) != "function")
                    result.push(block(this[i], i));
            return result;
        }

        Object.prototype.find = function(block : Function) : Array {
            var l = this.length;
            for(var i in this) {
                if ((typeof(this[i])) != "function")
                    if (block(this[i], i))
                        return this[i];
            }
        }

        Object.prototype.inject = function(block : Function) : Array {
            var l = this.length;
            var a;
            for(var i in this)
                if ((typeof(this[i])) != "function")
                    if (a == undefined)
                        a = this[i];
                    else
                        a = block(a, this[i])
            return a;
        }

      }
    }

no comments | no trackbacks

Comments

Trackbacks

Use the following link to trackback from your own site:
http://www.unfitforprint.com/trackbacks?article_id=ruby-iterators-for-actionscript-part-iii&day=10&month=01&year=2006

Comments are disabled