sequency
Version:
Functional sequences for processing iterable data in JavaScript
29 lines • 1.05 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Reduce = void 0;
var Reduce = /** @class */ (function () {
function Reduce() {
}
/**
* 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.
*
* @param {(acc: S, value: T) => S} operation
* @returns {S}
*/
Reduce.prototype.reduce = function (operation) {
var first = this.iterator.next();
if (first.done) {
throw new Error("Cannot reduce empty sequence");
}
var result = first.value;
for (var item = this.iterator.next(); !item.done; item = this.iterator.next()) {
result = operation(result, item.value);
}
return result;
};
return Reduce;
}());
exports.Reduce = Reduce;
//# sourceMappingURL=reduce.js.map