-
-
Save izumskee/90af72804729045890af1afd1066dd52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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