Thanks a lot for your array functions. I'm setting these up in a class file, and I was thinking that I'd add the functions to the intrinsics. But I looked at the intrinsics file for Array.as, and I was a bit surprised to learn that Array is dynamic. So is it worth the effort to modify the intrinsics?

Thanks
Chris

Steven Sacks | BLITZ wrote:

Here is the next iteration - new and improved.   Again, all comments and
optimizations, including conversations about optimizations, are most
welcome!

// Array Empowerment
// Removes all elements from Array
Array.prototype.clear = function() {
        this.length = 0;
};
// Invoking block once for every element, passing each element as a
parameter to block
// The result of block is used as the given element in the array
Array.prototype.collect = function(block) {
        var n = this.length;
        for (var a = 0; a < n; a++) {
                this[a] = block(this[a]);
        }
};
// Removes all null and undefined elements from Array
Array.prototype.compact = function() {
        var i = -1;
        var n = this.length;
        var r = [];
        for (var a = 0; a < n; a++) {
                if (this[a] != null && this[a] != undefined) {
                        r.push(this[a]);
                }
        }
};
// Deletes items from the Array that are equal to obj
Array.prototype.delete_all = function(obj) {
        var n = this.length;
        var r = [];
        if (!(obj instanceof Array)) {
                for (a = 0; a < n; a++) {
                        if (this[a] == obj) r.push(this[a]);
                }
        } else {
                for (a = 0; a < n; a++) {
                        if (!this[a].eql(obj)) r.push(this[a]);
                }               
        }
        this.replicate(r);
};
// Deletes the element at the specified index, returning that element,
or undefined if the index is out of range
Array.prototype.delete_at = function(i) {
        if (i < this.length && i > -1) return this.splice(i, 1)[0];
        return undefined;
};
// Deletes every element of Array for which block evaluates to true
Array.prototype.delete_if = function(block) {
        var r = [];
        var n = this.length;
        for (var a = 0; a < n; a++) {
                if (!block(this[a])) {
                        r.push(this[a]);
                }
        }
        this.replicate(r);
};
Array.prototype.duplicate = function() {
        var r = [];
        var a = this.length;
        while (--a -(-1)) {
                r[a] = this[a];
        }
        return r;
};
Array.prototype.replicate = function(r) {
        this.length = 0;
        var a = r.length;
        while (--a -(-1)) {
                this[a] = r[a];
        }
};
// Calls block once for each element in Array, passing that element as a
parameter
Array.prototype.each = function(block) {
        var n = this.length;
        for (var a = 0; a < n; a++) {
                block(this[a]);
        }
};
// Same as Array.each, but passes the index of the element instead of
the element itself
Array.prototype.each_index = function(block) {
        var n = this.length;
        for (var a = 0; a < n; a++) {
                block(a);
        }
};
// An array is equal to another array if the lengths are equal and each
corresponding element is equal
Array.prototype.eql = function(arr) {
        if (arr.length != this.length || !(arr instanceof Array)) {
                return false;
        } else {
                var a = this.length;
                while (--a -(-1)) {
                        if (this[a] instanceof Array) {
                                if (!this[a].eql(arr[a])) return false;
                        } else if (this[a] != arr[a]) {
                                return false;
                        }
                }
        }
        return true;
};
// Runs block on every item in the array // Returns true if the function returns true for every item
Array.prototype.every = function(block) {
        var a = this.length;
        while (--a -(-1)) {
                if (!block(this[a])) return false;
        }
        return true;
};
// Sets the selected elements of Array (which may be the entire array)
to obj
// A start of undefined is equivalent to zero. // A length of undefined is equivalent to Array.length.
Array.prototype.fill = function(obj, start, len) {
        if (!start) start = 0;
        if (len == undefined) len = this.length;
        len = start + len;
        if (len > this.length) len = this.length;    
        while (--len - (-1) > start) {
                this[len] = obj;
        }
};
// Runs a function on every item in the array
// Returns an array of all items for which the function returns true.
Array.prototype.filter = function(block) {
        var r = [];
        var n = this.length;
        for (var a = 0; a < n; a++) {
                if (block(this[a])) r.push(this[a]);
        }
        return r;
};
// Returns a new array that is a one-dimensional flattening of this
Array (recursively)
// That is, for every element that is an array, extract its elements
into the new array
Array.prototype.flatten = function(r) {
        if (!r) r = [];
        var l = this.length;
        for (var a = 0; a < l; a++) {
                if (!(this[a] instanceof Array)) {
                        r.push(this[a]);
                } else {
                        this[a].flatten(r);
                }
        }
        return r;
};
// Returns true if the given obj is present in Array (if any element ==
obj)
Array.prototype.include = function(obj) {
        var a = this.length;
        if (!(obj instanceof Array)) {
                while (--a -(-1)) {
                        if (this[a] == obj) return true;
                }               
        } else {
                while (--a -(-1)) {
                        if (this[a].eql(obj)) return true;
                }
        }
        return false;
};
// Returns the index of the first object in Array such that the object
== obj.
// Returns undefined if no match is found
Array.prototype.index = function(obj) {
        var a = this.length;
        while (--a -(-1)) {
                if (this[a] == obj) return a;
                if (this[a] instanceof Array) {
                        if (this[a].eql(obj)) return a;
                }
        }
        return undefined;
};
// Returns a new array consisting of elements at the given indices.
// May insert undefined for indices out of range.
Array.prototype.indexes = function() {
        var r = [];
        var l = arguments.length;
        for (var a = 0; a < l; a++) {
                if (arguments[a] > -1 && arguments[a] < this.length) {
                        r.push(this[arguments[a]]);
                } else {
                        r.push(undefined);
                }
        }
        return r;
};
// Synonym for Array.indexes Array.prototype.indices = Array.prototype.indexes;
//
// Returns the last element of Array. If the array is empty, returns
undefined
Array.prototype.last = function() {
        if (this.length > 0) return this[this.length - 1];
        return undefined;
};
// Runs a function on every item in the array and returns the results in
an array
Array.prototype.map = function(block) {
        var r = [];
        var n = this.length;
        for (var a = 0; a < n; a++) {
                r.push(block(this[a]));
        }
        return r;
};
// Returns the number of non-null/undefined elements in Array. May be
zero
Array.prototype.nitems = function() {
        var c = 0;
        var a = this.length;
        while (--a -(-1)) {
                if (this[a] != null && this[a] != undefined) c++;
        }
        return c;
};
// Same as Array.each , but traverses Array in reverse order
Array.prototype.reverse_each = function(block) {
        var n = this.length;
        while(--n -(-1)) {
                block(this[n]);
        }
};
// Returns the index of the last object in Array such that the object ==
obj
// Returns undefined if no match is found. Array.prototype.rindex = function(obj) {
        var a = this.length;
        while (--a -(-1)) {
                if (!(this[a] instanceof Array)) {
                        if (this[a] == obj) return a;                   
                } else {
                        if (this[a].eql(obj)) return a;
                }
        }
        return undefined;
};
// Runs a function on every item in the array
// Returns true if the function returns true for any one item
Array.prototype.some = function(block) {
        var a = this.length;
        while (--a -(-1)) {
                if (block(this[a])) return true;
        }
        return false;
};

// Returns a new array by removing duplicate values in Array. Array.prototype.uniq = function() {
        var r = [];
        var l = this.length;
        for (var a = 0; a < l; a++) {
                if (!r.include(this[a])) r.push(this[a]);
        }
        return r;
};
// MATH METHODS
Array.prototype.sum = function() {
        var x = 0;
        var a = this.length;
        while (--a -(-1)) {
                if (this[a] instanceof Number) {
                        x += this[a];
                } else {
                        return undefined;
                }
        }
};
Array.prototype.mean = function() {
        var s = this.sum();
        if (s != undefined) return this.sum() / this.length;
        return undefined;
};
Array.prototype.min = function() {
        var a = this.length;
        if (!a) return undefined;
        var x = this.last();
        while (--a -(-1)) {
                x = Math.min(this[a], x);
        }
        return x;
};
Array.prototype.max = function() {
        var a = this.length;
        if (!a) return undefined;
        var x = this.last();
        while (--a -(-1)) {
                x = Math.max(this[a], x);
        }
        return x;
};
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to