sequency
Version:
Functional sequences for processing iterable data in JavaScript
31 lines • 1.26 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReduceIndexed = void 0;
var ReduceIndexed = /** @class */ (function () {
function ReduceIndexed() {
}
/**
* Reduces the whole sequence to a single value by invoking `operation` with each element
* from left to right. For every invocation of the operation `acc` is the result of the last
* invocation. For the first invocation of the operation `acc` is the first element of the
* sequence. In addition the `index` of the current element is also passed to the operation.
*
* @param {(index: number, acc: S, element: T) => S} operation
* @returns {S}
*/
ReduceIndexed.prototype.reduceIndexed = function (operation) {
var first = this.iterator.next();
if (first.done) {
throw new Error("Cannot reduce empty sequence");
}
var index = 1;
var result = first.value;
for (var item = this.iterator.next(); !item.done; item = this.iterator.next()) {
result = operation(index, result, item.value);
index++;
}
return result;
};
return ReduceIndexed;
}());
exports.ReduceIndexed = ReduceIndexed;
//# sourceMappingURL=reduceIndexed.js.map