1 /**
  2  * @class The built-in Array class.
  3  * @name Array
  4  */
  5 
  6 /**
  7  * Creates a new array with the results of calling a provided function on every
  8  * element in this array. Implemented in Javascript 1.6.
  9  *
 10  * @function
 11  * @name Array.prototype.map
 12  * @see <a
 13  * href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/Map">map</a>
 14  * documentation.
 15  * @param {function} f function that produces an element of the new Array from
 16  * an element of the current one.
 17  * @param [o] object to use as <tt>this</tt> when executing <tt>f</tt>.
 18  */
 19 if (!Array.prototype.map) Array.prototype.map = function(f, o) {
 20   var n = this.length;
 21   var result = new Array(n);
 22   for (var i = 0; i < n; i++) {
 23     if (i in this) {
 24       result[i] = f.call(o, this[i], i, this);
 25     }
 26   }
 27   return result;
 28 };
 29 
 30 /**
 31  * Creates a new array with all elements that pass the test implemented by the
 32  * provided function. Implemented in Javascript 1.6.
 33  *
 34  * @function
 35  * @name Array.prototype.filter
 36  * @see <a
 37  * href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/filter">filter</a>
 38  * documentation.
 39  * @param {function} f function to test each element of the array.
 40  * @param [o] object to use as <tt>this</tt> when executing <tt>f</tt>.
 41  */
 42 if (!Array.prototype.filter) Array.prototype.filter = function(f, o) {
 43   var n = this.length;
 44   var result = new Array();
 45   for (var i = 0; i < n; i++) {
 46     if (i in this) {
 47       var v = this[i];
 48       if (f.call(o, v, i, this)) result.push(v);
 49     }
 50   }
 51   return result;
 52 };
 53 
 54 /**
 55  * Executes a provided function once per array element. Implemented in
 56  * Javascript 1.6.
 57  *
 58  * @function
 59  * @name Array.prototype.forEach
 60  * @see <a
 61  * href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/ForEach">forEach</a>
 62  * documentation.
 63  * @param {function} f function to execute for each element.
 64  * @param [o] object to use as <tt>this</tt> when executing <tt>f</tt>.
 65  */
 66 if (!Array.prototype.forEach) Array.prototype.forEach = function(f, o) {
 67   var n = this.length >>> 0;
 68   for (var i = 0; i < n; i++) {
 69     if (i in this) f.call(o, this[i], i, this);
 70   }
 71 };
 72 
 73 /**
 74  * Apply a function against an accumulator and each value of the array (from
 75  * left-to-right) as to reduce it to a single value. Implemented in Javascript
 76  * 1.8.
 77  *
 78  * @function
 79  * @name Array.prototype.reduce
 80  * @see <a
 81  * href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/Reduce">reduce</a>
 82  * documentation.
 83  * @param {function} f function to execute on each value in the array.
 84  * @param [v] object to use as the first argument to the first call of
 85  * <tt>t</tt>.
 86  */
 87 if (!Array.prototype.reduce) Array.prototype.reduce = function(f, v) {
 88   var len = this.length;
 89   if (!len && (arguments.length == 1)) {
 90     throw new Error("reduce: empty array, no initial value");
 91   }
 92 
 93   var i = 0;
 94   if (arguments.length < 2) {
 95     while (true) {
 96       if (i in this) {
 97         v = this[i++];
 98         break;
 99       }
100       if (++i >= len) {
101         throw new Error("reduce: no values, no initial value");
102       }
103     }
104   }
105 
106   for (; i < len; i++) {
107     if (i in this) {
108       v = f(v, this[i], i, this);
109     }
110   }
111   return v;
112 };
113