Because they’re so ■■■■ useful:
Array.prototype.first = function():Number { return this[0]; }
Array.prototype.last = function():Number { return this[this.length-1]; }
Array.prototype.each = function(iterator:Function):Array {
for (var i:Number = 0; i < this.length; i++) {
iterator.call(this, this*, i);
}
return this
}
Array.prototype.all = function(iterator:Function):Boolean {
if (!iterator) return false;
var result:Boolean = true;
this.each(function(value, index) {
if (!iterator(value, index)) result = false;
});
return result;
}
Array.prototype.any = function(iterator:Function):Boolean {
if (!iterator) return false;
var result:Boolean = false;
this.each(function(value, index) {
if (iterator(value, index)) result = true;
});
return result;
}
Array.prototype.collect = function(iterator:Function):Array {
var results:Array = [];
this.each(function(value, index) {
results.push(iterator(value, index));
});
return results;
}
Array.prototype.map = Array.prototype.collect;
Array.prototype.include = function(item:Object):Array {
return this.any(function(value) { return value == item });
}
Array.prototype.member = Array.prototype.include;
Array.prototype.inject = function(memo:Object, iterator:Function):Object {
this.each(function(value, index) {
memo = iterator(memo, value, index);
});
return memo;
}
Array.prototype.sum = function():Number {
return this.inject(0, function(sum, value) { return sum + value; });
}
Array.prototype.findAll = function(iterator:Function):Array {
var results:Array = [];
this.each(function(value, index) {
if (iterator(value, index)) results.push(value);
});
return results;
}
Array.prototype.select = Array.prototype.findAll;
Array.prototype.even = function():Number {
return this.findAll(function(number) { return (number % 2) == 0; })
}
Array.prototype.odd = function():Number {
return this.findAll(function(number) { return (number % 2) == 1; })
}
Array.prototype.detect = function(iterator:Function):Object {
return this.findAll(iterator).first();
}
Array.prototype.find = Array.prototype.detect;
Array.prototype.invoke = function(method:String):Array {
return this.collect(function(value, index) {
return value[method].apply(value, arguments.slice(1));
});
}
Array.prototype.max = function():Number {
var result:Number;
this.each(function(value, index) {
if (result == undefined || value > result)
result = value;
});
return result;
}
Array.prototype.min = function():Number {
var result:Number;
this.each(function(value, index) {
if (result == undefined || value < result)
result = value;
});
return result;
}
Array.prototype.partition = function(iterator:Function):Array {
var trues:Array = [], falses:Array = [];
this.each(function(value, index) {
if (iterator(value, index)) {
trues.push(value);
} else {
falses.push(value);
}
});
return [trues, falses];
}
Array.prototype.pluck = function(prop:String):Array {
var results:Array = [];
this.each(function(value, index) {
results.push(value[prop]);
});
return results;
}
Array.prototype.reject = function(iterator:Function):Array {
var results:Array = [];
this.each(function(value, index) {
if (!iterator(value, index))
results.push(value);
});
return results;
}
Array.prototype.clear = function():Array {
this = [];
return this;
}
Array.prototype.compact = function():Array {
return this.select(function(value, index) {
return value != null;
});
}
Array.prototype.indexOf = function(value:Object):Number {
for(var i:Number = 0; i < this.length; i++) {
if(this* == value) {
return i;
}
}
}
Array.prototype.clone = function():Array { return this; }
Array.prototype.flatten = function():Array {
return this.inject([], function(arr, value) {
return arr.concat(value && value.constructor == Array ? value.flatten() : [value]);
});
}
Array.prototype.without = function():Array {
var results:Array = [];
this.select(function(value, index) {
return !arguments.include(value);
});
}
Array.prototype.unique = function():Array {
return this.inject([], function(arr, value) {
return arr.include(value) ? arr : arr.concat([value]);
});
}