Slide 58
Slide 58 text
@MoOx
58
ACTUALLY IN JS
// http://es5.github.io/#x15.4.4.21
function foldLeft(arr, fn) {
var array = new Object(arr),
index = -1,
length = array.length >>> 0,
acc,
hasInitialValue;
if (Object.prototype.toString.call(fn) != "[object Function]") {
throw new TypeError();
}
if (arguments.length > 1) {
acc = arguments[1];
} else {
hasInitialValue = false;
while (++index < length) {
if (!(hasInitialValue = index in array)) {
continue;
}
acc = array[index];
break;
}
if (!hasInitialValue) {
throw new TypeError();
}
}
while (++index < length) {
if (!(index in array)) continue;
acc = fn.call(void 0, acc, array[index], index, array);
}
return acc;
};