/** * Array.reduce * * reduce is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. * You can work around this by inserting the following code at the beginning of your scripts, allowing use of reduce in ECMA-262 implementations which do not natively support it. * This algorithm is exactly the one used in Firefox and SpiderMonkey. * * http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:reduce */ if (!Array.prototype.reduce) { Array.prototype.reduce = function(fun /*, initial*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); // no value to return if no initial value and an empty array if (len == 0 && arguments.length == 1) throw new TypeError(); var i = 0; if (arguments.length >= 2) { var rv = arguments[1]; } else { do { if (i in this) { rv = this[i++]; break; } // if array contains no values, no initial value to return if (++i >= len) throw new TypeError(); } while (true); } for (; i < len; i++) { if (i in this) rv = fun.call(null, rv, this[i], i, this); } return rv; }; } /** * Array.reduceRight * * reduceRight is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. * You can work around this by inserting the following code at the beginning of your scripts, allowing use of reduceRight in ECMA-262 implementations which do not natively support it. * This algorithm is exactly the one used in Firefox and SpiderMonkey. * * http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:reduceRight */ if (!Array.prototype.reduceRight) { Array.prototype.reduceRight = function(fun /*, initial*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); // no value to return if no initial value, empty array if (len == 0 && arguments.length == 1) throw new TypeError(); var i = len - 1; if (arguments.length >= 2) { var rv = arguments[1]; } else { do { if (i in this) { rv = this[i--]; break; } // if array contains no values, no initial value to return if (--i < 0) throw new TypeError(); } while (true); } for (; i >= 0; i--) { if (i in this) rv = fun.call(null, rv, this[i], i, this); } return rv; }; }