1 /**
  2  * @fileOverview This file contains Array compatibility prototypes for those introduced in JavaScript 1.8
  3  */
  4 
  5 /**#@+
  6  * @native 1.8
  7  */
  8 
  9 /**
 10  * Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
 11  * 
 12  * @see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
 13  * 
 14  * @name reduce
 15  * @methodOf Array.prototype
 16  * 
 17  * @param {Function} fun Function to execute on each value in the array
 18  * @param [initial] Object to use as the first argument to the first call of the callback
 19  * @return The final value created by the reduce
 20  */
 21 if (!Array.prototype.reduce) {
 22 	Array.prototype.reduce = function(fun /*, initial*/) {
 23 		var len = this.length >>> 0;
 24 		if (typeof fun != "function")
 25 			throw new TypeError();
 26 		
 27 		// no value to return if no initial value and an empty array
 28 		if (len == 0 && arguments.length == 1)
 29 			throw new TypeError();
 30 		
 31 		var i = 0;
 32 		if (arguments.length >= 2)
 33 		{
 34 			var rv = arguments[1];
 35 		}
 36 		else
 37 		{
 38 			do {
 39 				if (i in this) {
 40 					rv = this[i++];
 41 					break;
 42 				}
 43 				
 44 				// if array contains no values, no initial value to return
 45 				if (++i >= len)
 46 					throw new TypeError();
 47 			}
 48 			while (true);
 49 		}
 50 		
 51 		for (; i < len; i++) {
 52 			if (i in this)
 53 				rv = fun.call(null, rv, this[i], i, this);
 54 		}
 55 		
 56 		return rv;
 57 	};
 58 }
 59 
 60 /**
 61  * Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
 62  * 
 63  * @see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
 64  * 
 65  * @name reduceRight
 66  * @methodOf Array.prototype
 67  * 
 68  * @param {Function} fun Function to execute on each value in the array
 69  * @param [initial] Object to use as the first argument to the first call of the callback
 70  * @return The final value created by the reduce
 71  */
 72 if (!Array.prototype.reduceRight) {
 73 	Array.prototype.reduceRight = function(fun /*, initial*/) {
 74 		var len = this.length >>> 0;
 75 		if (typeof fun != "function")
 76 			throw new TypeError();
 77 		
 78 		// no value to return if no initial value, empty array
 79 		if (len == 0 && arguments.length == 1)
 80 			throw new TypeError();
 81 		
 82 		var i = len - 1;
 83 		if (arguments.length >= 2) {
 84 			var rv = arguments[1];
 85 		} else {
 86 			do {
 87 				if (i in this) {
 88 					rv = this[i--];
 89 					break;
 90 				}
 91 				
 92 				// if array contains no values, no initial value to return
 93 				if (--i < 0)
 94 					throw new TypeError();
 95 			}
 96 			while (true);
 97 		}
 98 		
 99 		for (; i >= 0; i--) {
100 			if (i in this)
101 				rv = fun.call(null, rv, this[i], i, this);
102 		}
103 		
104 		return rv;
105 	};
106 }
107 /**#@-*/
108 
109