Skip to content

Instantly share code, notes, and snippets.

@xzure
Created July 1, 2016 17:13
Show Gist options
  • Save xzure/eb98c7e89a98019720a69ad875045afa to your computer and use it in GitHub Desktop.
Save xzure/eb98c7e89a98019720a69ad875045afa to your computer and use it in GitHub Desktop.
const reduce = function(callback) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.reduce called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var t = Object(this),
len = t.length >>> 0,
k = 0,
value;
if (arguments.length == 2) {
value = arguments[1];
} else {
while (k < len && !(k in t)) {
k++;
}
if (k >= len) {
throw new TypeError('Reduce of empty array with no initial value');
}
value = t[k++];
}
for (; k < len; k++) {
if (k in t) {
value = callback(value, t[k], k, t);
}
}
return value;
};
if (!Array.prototype.reduce) Array.prototype.reduce = reduce;
if (!Float32Array.prototype.reduce) Float32Array.prototype.reduce = reduce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment