Skip to content

Instantly share code, notes, and snippets.

@edulan
Created July 20, 2010 10:51
Show Gist options
  • Save edulan/482809 to your computer and use it in GitHub Desktop.
Save edulan/482809 to your computer and use it in GitHub Desktop.
/**
* Extensions to the AS2 Array class
*/
class ArrayExtensions
{
public function ArrayExtensions() {
}
public static function initialize() {
Array.prototype.map = function(mapFunction:Function):Array {
var result:Array = new Array();
var len:Number = this.length;
for (var i:Number = 0; i < len; i++) {
result.push(mapFunction(this[i]));
}
return result;
}
Array.prototype.reduce = function(initValue:Object, reduceFunction:Function):Object {
var result:Object = initValue;
var len:Number = this.length;
for (var i:Number = 0; i < len; i++) {
result = reduceFunction(result, this[i]);
}
return result;
}
}
}
Usage:
ArrayExtensions.initialize();
var sum = [1,2,3,4].map(function(n) { return n*2 } ).reduce(0, function(a,n) { return a+=n } );
trace(sum) // Output 20;
@chaky
Copy link

chaky commented Aug 13, 2010

nice code dude I will use it for sure...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment