UNPKG

@rimbu/stream

Version:

Efficient structure representing a sequence of elements, with powerful operations for TypeScript

1,297 lines 74.7 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.StreamConstructorsImpl = exports.fromStreamSource = exports.StreamBase = void 0; exports.isEmptyStreamSourceInstance = isEmptyStreamSourceInstance; var tslib_1 = require("tslib"); var base_1 = require("@rimbu/base"); var common_1 = require("@rimbu/common"); var stream_1 = require("@rimbu/stream"); var custom_1 = require("@rimbu/stream/custom"); function yieldObjKeys(obj) { var _b, _c, _d, _i, key; return tslib_1.__generator(this, function (_e) { switch (_e.label) { case 0: _b = obj; _c = []; for (_d in _b) _c.push(_d); _i = 0; _e.label = 1; case 1: if (!(_i < _c.length)) return [3 /*break*/, 4]; _d = _c[_i]; if (!(_d in _b)) return [3 /*break*/, 3]; key = _d; return [4 /*yield*/, key]; case 2: _e.sent(); _e.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); } function yieldObjValues(obj) { var _b, _c, _d, _i, key; return tslib_1.__generator(this, function (_e) { switch (_e.label) { case 0: _b = obj; _c = []; for (_d in _b) _c.push(_d); _i = 0; _e.label = 1; case 1: if (!(_i < _c.length)) return [3 /*break*/, 4]; _d = _c[_i]; if (!(_d in _b)) return [3 /*break*/, 3]; key = _d; return [4 /*yield*/, obj[key]]; case 2: _e.sent(); _e.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); } function yieldObjEntries(obj) { var _b, _c, _d, _i, key; return tslib_1.__generator(this, function (_e) { switch (_e.label) { case 0: _b = obj; _c = []; for (_d in _b) _c.push(_d); _i = 0; _e.label = 1; case 1: if (!(_i < _c.length)) return [3 /*break*/, 4]; _d = _c[_i]; if (!(_d in _b)) return [3 /*break*/, 3]; key = _d; return [4 /*yield*/, [key, obj[key]]]; case 2: _e.sent(); _e.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); } /** * A reusable base implementation for `Stream` that provides all high-level operations * in terms of a `FastIterator` returned from `[Symbol.iterator]`. * * Custom stream implementations in `@rimbu/stream/custom` extend this class. * @typeparam T - the element type */ var StreamBase = /** @class */ (function () { function StreamBase() { } StreamBase.prototype.stream = function () { return this; }; StreamBase.prototype.equals = function (other, _b) { var _c = _b === void 0 ? {} : _b, _d = _c.eq, eq = _d === void 0 ? common_1.Eq.objectIs : _d, _e = _c.negate, negate = _e === void 0 ? false : _e; var it1 = this[Symbol.iterator](); var it2 = (0, exports.fromStreamSource)(other)[Symbol.iterator](); var done = Symbol('Done'); while (true) { var v1 = it1.fastNext(done); var v2 = it2.fastNext(done); if (done === v1 || done === v2) { return Object.is(v1, v2); } if (eq(v1, v2) === negate) { return false; } } }; StreamBase.prototype.assumeNonEmpty = function () { return this; }; StreamBase.prototype.asNormal = function () { return this; }; StreamBase.prototype.prepend = function (value) { return new PrependStream(this, value).assumeNonEmpty(); }; StreamBase.prototype.append = function (value) { return new AppendStream(this, value).assumeNonEmpty(); }; StreamBase.prototype.forEach = function (f, options) { if (options === void 0) { options = {}; } var _b = options.state, state = _b === void 0 ? (0, common_1.TraverseState)() : _b; if (state.halted) return; var done = Symbol('Done'); var value; var iterator = this[Symbol.iterator](); var halt = state.halt; while (!state.halted && done !== (value = iterator.fastNext(done))) { f(value, state.nextIndex(), halt); } }; StreamBase.prototype.forEachPure = function (f) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var done = Symbol('Done'); var value; var iterator = this[Symbol.iterator](); while (done !== (value = iterator.fastNext(done))) { f.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(args), false)); } }; StreamBase.prototype.indexed = function (options) { if (options === void 0) { options = {}; } var _b = options.startIndex, startIndex = _b === void 0 ? 0 : _b; return new IndexedStream(this, startIndex); }; StreamBase.prototype.filter = function (pred, options) { if (options === void 0) { options = {}; } var _b = options.negate, negate = _b === void 0 ? false : _b; return new FilterStream(this, pred, negate); }; StreamBase.prototype.filterPure = function (options) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var pred = options.pred, _b = options.negate, negate = _b === void 0 ? false : _b; return new FilterPureStream(this, pred, args, negate); }; StreamBase.prototype.withOnly = function (values) { if (values.length <= 0) { return this; } var set = new Set(values); return this.filterPure({ pred: function (v) { return set.has(v); } }); }; StreamBase.prototype.without = function (values) { if (values.length <= 0) { return this; } var set = new Set(values); return this.filterPure({ pred: function (v) { return set.has(v); }, negate: true }); }; StreamBase.prototype.map = function (mapFun) { return new MapStream(this, mapFun); }; StreamBase.prototype.mapPure = function (mapFun) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return new MapPureStream(this, mapFun, args); }; StreamBase.prototype.collect = function (collectFun) { return new CollectStream(this, collectFun); }; StreamBase.prototype.flatMap = function (flatMapFun) { return this.transform(stream_1.Transformer.flatMap(flatMapFun)); }; StreamBase.prototype.flatZip = function (flatMapFun) { return this.transform(stream_1.Transformer.flatZip(flatMapFun)); }; StreamBase.prototype.transform = function (transformer) { return new TransformerStream(this, transformer); }; StreamBase.prototype.first = function (otherwise) { return this[Symbol.iterator]().fastNext(otherwise); }; StreamBase.prototype.last = function (otherwise) { var done = Symbol('Done'); var value; var lastValue = done; var iterator = this[Symbol.iterator](); while (done !== (value = iterator.fastNext(done))) { lastValue = value; } if (done === lastValue) { return (0, common_1.OptLazy)(otherwise); } return lastValue; }; StreamBase.prototype.single = function (otherwise) { var iterator = this[Symbol.iterator](); var done = Symbol('Done'); var value = iterator.fastNext(done); if (done !== value) { if (done === iterator.fastNext(done)) { return value; } } return (0, common_1.OptLazy)(otherwise); }; StreamBase.prototype.count = function () { var result = 0; var done = Symbol('Done'); var iterator = this[Symbol.iterator](); while (done !== iterator.fastNext(done)) { result++; } return result; }; StreamBase.prototype.countElement = function (value, options) { if (options === void 0) { options = {}; } var _b = options.eq, eq = _b === void 0 ? common_1.Eq.objectIs : _b, _c = options.negate, negate = _c === void 0 ? false : _c; var result = 0; var done = Symbol('Done'); var iterator = this[Symbol.iterator](); var current; while (done !== (current = iterator.fastNext(done))) { if (eq(value, current) !== negate) { result++; } } return result; }; StreamBase.prototype.find = function (pred, options) { if (options === void 0) { options = {}; } var _b = options.occurrance, occurrance = _b === void 0 ? 1 : _b, _c = options.negate, negate = _c === void 0 ? false : _c, otherwise = options.otherwise; if (occurrance <= 0) { return (0, common_1.OptLazy)(otherwise); } var done = Symbol('Done'); var iterator = this[Symbol.iterator](); var value; var remain = occurrance; var index = 0; while (done !== (value = iterator.fastNext(done))) { if (pred(value, index++) !== negate && --remain <= 0) { return value; } } return (0, common_1.OptLazy)(otherwise); }; StreamBase.prototype.elementAt = function (index, otherwise) { if (index < 0) { return (0, common_1.OptLazy)(otherwise); } var done = Symbol('Done'); var iterator = this[Symbol.iterator](); var value; var i = 0; while (i <= index && done !== (value = iterator.fastNext(done))) { if (i === index) { return value; } i++; } return (0, common_1.OptLazy)(otherwise); }; StreamBase.prototype.indicesWhere = function (pred, options) { if (options === void 0) { options = {}; } return this.transform(stream_1.Transformer.indicesWhere(pred, options)); }; StreamBase.prototype.indicesOf = function (searchValue, options) { if (options === void 0) { options = {}; } return this.transform(stream_1.Transformer.indicesOf(searchValue, options)); }; StreamBase.prototype.indexWhere = function (pred, options) { if (options === void 0) { options = {}; } var _b = options.occurrance, occurrance = _b === void 0 ? 1 : _b, _c = options.negate, negate = _c === void 0 ? false : _c; if (occurrance <= 0) { return undefined; } var done = Symbol('Done'); var value; var iterator = this[Symbol.iterator](); var index = 0; var occ = 0; while (done !== (value = iterator.fastNext(done))) { var i = index++; if (pred(value, i) !== negate) { occ++; if (occ >= occurrance) { return i; } } } return undefined; }; StreamBase.prototype.indexOf = function (searchValue, options) { if (options === void 0) { options = {}; } var _b = options.occurrance, occurrance = _b === void 0 ? 1 : _b; if (occurrance <= 0) { return undefined; } var _c = options.eq, eq = _c === void 0 ? common_1.Eq.objectIs : _c, _d = options.negate, negate = _d === void 0 ? false : _d; var done = Symbol('Done'); var value; var iterator = this[Symbol.iterator](); var index = 0; var occ = 0; while (done !== (value = iterator.fastNext(done))) { var i = index++; if (eq(value, searchValue) !== negate) { occ++; if (occ >= occurrance) { return i; } } } return undefined; }; StreamBase.prototype.some = function (pred, options) { if (options === void 0) { options = {}; } return undefined !== this.indexWhere(pred, options); }; StreamBase.prototype.every = function (pred, options) { if (options === void 0) { options = {}; } var _b = options.negate, negate = _b === void 0 ? false : _b; return undefined === this.indexWhere(pred, { negate: !negate }); }; StreamBase.prototype.contains = function (searchValue, options) { if (options === void 0) { options = {}; } var _b = options.amount, amount = _b === void 0 ? 1 : _b; if (amount <= 0) { return true; } var eq = options.eq, negate = options.negate; return (undefined !== this.indexOf(searchValue, { occurrance: amount, eq: eq, negate: negate })); }; StreamBase.prototype.containsSlice = function (source, options) { if (options === void 0) { options = {}; } return this.reduce(stream_1.Reducer.containsSlice(source, options)); }; StreamBase.prototype.takeWhile = function (pred, options) { if (options === void 0) { options = {}; } var _b = options.negate, negate = _b === void 0 ? false : _b; return this.filter(function (value, index, halt) { var result = pred(value, index) !== negate; if (!result) { halt(); } return result; }); }; StreamBase.prototype.dropWhile = function (pred, options) { if (options === void 0) { options = {}; } var _b = options.negate, negate = _b === void 0 ? false : _b; return new DropWhileStream(this, pred, negate); }; StreamBase.prototype.take = function (amount) { if (amount <= 0) { return emptyStream; } return new TakeStream(this, amount); }; StreamBase.prototype.drop = function (amount) { if (amount <= 0) { return this; } return new DropStream(this, amount); }; StreamBase.prototype.repeat = function (amount) { var _this = this; if (undefined !== amount && amount <= 1) { return this; } return new FromStream(function () { return new custom_1.RepeatIterator(_this, amount); }); }; StreamBase.prototype.concat = function () { var others = []; for (var _i = 0; _i < arguments.length; _i++) { others[_i] = arguments[_i]; } if (others.every(isEmptyStreamSourceInstance)) { return this.assumeNonEmpty(); } return new ConcatStream(this, others).assumeNonEmpty(); }; StreamBase.prototype.min = function (otherwise) { return this.minBy(common_1.Comp.defaultComp().compare, otherwise); }; StreamBase.prototype.minBy = function (compare, otherwise) { var done = Symbol('Done'); var iterator = this[Symbol.iterator](); var result = iterator.fastNext(done); if (done === result) return (0, common_1.OptLazy)(otherwise); var value; while (done !== (value = iterator.fastNext(done))) { if (compare(value, result) < 0) result = value; } return result; }; StreamBase.prototype.max = function (otherwise) { return this.maxBy(common_1.Comp.defaultComp().compare, otherwise); }; StreamBase.prototype.maxBy = function (compare, otherwise) { var done = Symbol('Done'); var iterator = this[Symbol.iterator](); var result = iterator.fastNext(done); if (done === result) return (0, common_1.OptLazy)(otherwise); var value; while (done !== (value = iterator.fastNext(done))) { if (compare(value, result) > 0) result = value; } return result; }; StreamBase.prototype.intersperse = function (sep) { if (isEmptyStreamSourceInstance(sep)) { return this; } return this.transform(stream_1.Transformer.intersperse(sep)); }; StreamBase.prototype.join = function (_b) { var _c = _b === void 0 ? {} : _b, _d = _c.sep, sep = _d === void 0 ? '' : _d, _e = _c.start, start = _e === void 0 ? '' : _e, _f = _c.end, end = _f === void 0 ? '' : _f, _g = _c.valueToString, valueToString = _g === void 0 ? String : _g, _h = _c.ifEmpty, ifEmpty = _h === void 0 ? undefined : _h; var done = Symbol('Done'); var iterator = this[Symbol.iterator](); var value = iterator.fastNext(done); if (done === value) { if (undefined !== ifEmpty) { return ifEmpty; } return start.concat(end); } var result = start.concat(valueToString(value)); while (done !== (value = iterator.fastNext(done))) { result = result.concat(sep, valueToString(value)); } return result.concat(end); }; StreamBase.prototype.mkGroup = function (_b) { var _c = _b === void 0 ? {} : _b, _d = _c.sep, sep = _d === void 0 ? emptyStream : _d, _e = _c.start, start = _e === void 0 ? emptyStream : _e, _f = _c.end, end = _f === void 0 ? emptyStream : _f; return (0, exports.fromStreamSource)(start).concat(this.intersperse(sep), end); }; StreamBase.prototype.splitWhere = function (pred, options) { if (options === void 0) { options = {}; } return this.transform(stream_1.Transformer.splitWhere(pred, options)); }; StreamBase.prototype.splitOn = function (sepElem, options) { if (options === void 0) { options = {}; } return this.transform(stream_1.Transformer.splitOn(sepElem, options)); }; StreamBase.prototype.splitOnSlice = function (sepSeq, options) { if (options === void 0) { options = {}; } return this.transform(stream_1.Transformer.splitOnSlice(sepSeq, options)); }; StreamBase.prototype.distinctPrevious = function (options) { if (options === void 0) { options = {}; } return this.transform(stream_1.Transformer.distinctPrevious(options)); }; StreamBase.prototype.window = function (windowSize, options) { if (options === void 0) { options = {}; } return this.transform(stream_1.Transformer.window(windowSize, options)); }; StreamBase.prototype.partition = function (pred, options) { if (options === void 0) { options = {}; } return this.reduce(stream_1.Reducer.partition(pred, options)); }; StreamBase.prototype.groupBy = function (valueToKey, options) { if (options === void 0) { options = {}; } return this.reduce(stream_1.Reducer.groupBy(valueToKey, options)); }; StreamBase.prototype.fold = function (init, next) { return this.reduce(stream_1.Reducer.fold(init, next)); }; StreamBase.prototype.foldStream = function (init, next) { return this.reduceStream(stream_1.Reducer.fold(init, next)); }; StreamBase.prototype.reduce = function (shape) { var reducerInstance = stream_1.Reducer.combine(shape).compile(); var done = Symbol('Done'); var value; var iter = this[Symbol.iterator](); while (!reducerInstance.halted && done !== (value = iter.fastNext(done))) { reducerInstance.next(value); } return reducerInstance.getOutput(); }; StreamBase.prototype.reduceStream = function (shape) { var reducer = stream_1.Reducer.combine(shape); return new ReducerStream(this, reducer); }; StreamBase.prototype.toArray = function () { var iterator = this[Symbol.iterator](); var result = []; var done = Symbol('Done'); var value; while (done !== (value = iterator.fastNext(done))) { result.push(value); } return result; }; StreamBase.prototype.toString = function () { return "Stream(...<potentially empty>)"; }; StreamBase.prototype.toJSON = function () { return { dataType: 'Stream', value: this.toArray(), }; }; return StreamBase; }()); exports.StreamBase = StreamBase; var FromStream = /** @class */ (function (_super) { tslib_1.__extends(FromStream, _super); function FromStream(createIterator) { var _this = _super.call(this) || this; _this[_a] = undefined; _this[Symbol.iterator] = createIterator; return _this; } return FromStream; }(StreamBase)); _a = Symbol.iterator; var PrependStream = /** @class */ (function (_super) { tslib_1.__extends(PrependStream, _super); function PrependStream(source, item) { var _this = _super.call(this) || this; _this.source = source; _this.item = item; return _this; } PrependStream.prototype[Symbol.iterator] = function () { return new custom_1.PrependIterator(this.source[Symbol.iterator](), this.item); }; PrependStream.prototype.first = function () { return (0, common_1.OptLazy)(this.item); }; PrependStream.prototype.last = function () { return this.source.last(this.item); }; PrependStream.prototype.count = function () { return this.source.count() + 1; }; PrependStream.prototype.forEach = function (f, options) { if (options === void 0) { options = {}; } var _b = options.state, state = _b === void 0 ? (0, common_1.TraverseState)() : _b; if (state.halted) return; f((0, common_1.OptLazy)(this.item), state.nextIndex(), state.halt); if (state.halted) return; this.source.forEach(f, { state: state }); }; PrependStream.prototype.mapPure = function (mapFun) { var _b; var _this = this; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return new PrependStream((_b = this.source).mapPure.apply(_b, tslib_1.__spreadArray([mapFun], tslib_1.__read(args), false)), function () { return mapFun.apply(void 0, tslib_1.__spreadArray([(0, common_1.OptLazy)(_this.item)], tslib_1.__read(args), false)); }); }; PrependStream.prototype.take = function (amount) { if (amount <= 0) { return emptyStream; } if (amount === 1) { return exports.StreamConstructorsImpl.of((0, common_1.OptLazy)(this.item)); } return new PrependStream(this.source.take(amount - 1), this.item); }; PrependStream.prototype.drop = function (amount) { if (amount <= 0) { return this; } if (amount === 1) { return this.source; } return this.source.drop(amount - 1); }; PrependStream.prototype.minBy = function (compare) { var token = Symbol(); var result = this.source.minBy(compare, token); var itemValue = (0, common_1.OptLazy)(this.item); if (token === result) { return itemValue; } return compare(result, itemValue) <= 0 ? result : itemValue; }; PrependStream.prototype.maxBy = function (compare) { var token = Symbol(); var result = this.source.maxBy(compare, token); var itemValue = (0, common_1.OptLazy)(this.item); if (token === result) { return itemValue; } return compare(result, itemValue) > 0 ? result : itemValue; }; PrependStream.prototype.toArray = function () { var result = this.source.toArray(); result.unshift((0, common_1.OptLazy)(this.item)); return result; }; return PrependStream; }(StreamBase)); var AppendStream = /** @class */ (function (_super) { tslib_1.__extends(AppendStream, _super); function AppendStream(source, item) { var _this = _super.call(this) || this; _this.source = source; _this.item = item; return _this; } AppendStream.prototype[Symbol.iterator] = function () { return new custom_1.AppendIterator(this.source[Symbol.iterator](), this.item); }; AppendStream.prototype.first = function () { return this.source.first(this.item); }; AppendStream.prototype.last = function () { return (0, common_1.OptLazy)(this.item); }; AppendStream.prototype.count = function () { return this.source.count() + 1; }; AppendStream.prototype.forEach = function (f, options) { if (options === void 0) { options = {}; } var _b = options.state, state = _b === void 0 ? (0, common_1.TraverseState)() : _b; if (state.halted) return; this.source.forEach(f, { state: state }); if (state.halted) return; f((0, common_1.OptLazy)(this.item), state.nextIndex(), state.halt); }; AppendStream.prototype.mapPure = function (mapFun) { var _b; var _this = this; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return new AppendStream((_b = this.source).mapPure.apply(_b, tslib_1.__spreadArray([mapFun], tslib_1.__read(args), false)), function () { return mapFun.apply(void 0, tslib_1.__spreadArray([(0, common_1.OptLazy)(_this.item)], tslib_1.__read(args), false)); }); }; AppendStream.prototype.minBy = function (compare) { var token = Symbol(); var result = this.source.minBy(compare, token); var itemValue = (0, common_1.OptLazy)(this.item); if (token === result) { return itemValue; } return compare(result, itemValue) <= 0 ? result : itemValue; }; AppendStream.prototype.maxBy = function (compare) { var token = Symbol(); var result = this.source.maxBy(compare, token); var itemValue = (0, common_1.OptLazy)(this.item); if (token === result) { return itemValue; } return compare(result, itemValue) > 0 ? result : itemValue; }; AppendStream.prototype.toArray = function () { var result = this.source.toArray(); result.push((0, common_1.OptLazy)(this.item)); return result; }; return AppendStream; }(StreamBase)); var MapStream = /** @class */ (function (_super) { tslib_1.__extends(MapStream, _super); function MapStream(source, mapFun) { var _this = _super.call(this) || this; _this.source = source; _this.mapFun = mapFun; return _this; } MapStream.prototype[Symbol.iterator] = function () { return new custom_1.MapIterator(this.source[Symbol.iterator](), this.mapFun); }; MapStream.prototype.first = function (otherwise) { var done = Symbol('Done'); var value = this.source.first(done); if (done === value) return (0, common_1.OptLazy)(otherwise); return this.mapFun(value, 0); }; MapStream.prototype.last = function (otherwise) { var done = Symbol('Done'); var value = this.source.last(done); if (done === value) return (0, common_1.OptLazy)(otherwise); return this.mapFun(value, 0); }; MapStream.prototype.count = function () { return this.source.count(); }; MapStream.prototype.elementAt = function (index, otherwise) { var done = Symbol('Done'); var value = this.source.elementAt(index, done); if (done === value) return (0, common_1.OptLazy)(otherwise); return this.mapFun(value, index); }; MapStream.prototype.map = function (mapFun) { var _this = this; return new MapStream(this.source, function (value, index) { return mapFun(_this.mapFun(value, index), index); }); }; MapStream.prototype.take = function (amount) { if (amount <= 0) { return emptyStream; } return new MapStream(this.source.take(amount), this.mapFun); }; MapStream.prototype.drop = function (amount) { if (amount <= 0) { return this; } return new MapStream(this.source.drop(amount), this.mapFun); }; return MapStream; }(StreamBase)); var MapPureStream = /** @class */ (function (_super) { tslib_1.__extends(MapPureStream, _super); function MapPureStream(source, mapFun, args) { var _this = _super.call(this) || this; _this.source = source; _this.mapFun = mapFun; _this.args = args; return _this; } MapPureStream.prototype[Symbol.iterator] = function () { return new custom_1.MapPureIterator(this.source[Symbol.iterator](), this.mapFun, this.args); }; MapPureStream.prototype.first = function (otherwise) { var done = Symbol('Done'); var value = this.source.first(done); if (done === value) return (0, common_1.OptLazy)(otherwise); return this.mapFun.apply(this, tslib_1.__spreadArray([value], tslib_1.__read(this.args), false)); }; MapPureStream.prototype.last = function (otherwise) { var done = Symbol('Done'); var value = this.source.last(done); if (done === value) return (0, common_1.OptLazy)(otherwise); return this.mapFun.apply(this, tslib_1.__spreadArray([value], tslib_1.__read(this.args), false)); }; MapPureStream.prototype.count = function () { return this.source.count(); }; MapPureStream.prototype.elementAt = function (index, otherwise) { var done = Symbol('Done'); var value = this.source.elementAt(index, done); if (done === value) return (0, common_1.OptLazy)(otherwise); return this.mapFun.apply(this, tslib_1.__spreadArray([value], tslib_1.__read(this.args), false)); }; MapPureStream.prototype.mapPure = function (mapFun) { var _this = this; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return new MapPureStream(this.source, function (value) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return mapFun.apply(void 0, tslib_1.__spreadArray([_this.mapFun.apply(_this, tslib_1.__spreadArray([value], tslib_1.__read(_this.args), false))], tslib_1.__read(args), false)); }, args); }; MapPureStream.prototype.take = function (amount) { if (amount <= 0) { return emptyStream; } return new MapPureStream(this.source.take(amount), this.mapFun, this.args); }; MapPureStream.prototype.drop = function (amount) { if (amount <= 0) { return this; } return new MapPureStream(this.source.drop(amount), this.mapFun, this.args); }; return MapPureStream; }(StreamBase)); var ConcatStream = /** @class */ (function (_super) { tslib_1.__extends(ConcatStream, _super); function ConcatStream(source, otherSources) { var _this = _super.call(this) || this; _this.source = source; _this.otherSources = otherSources; return _this; } ConcatStream.prototype[Symbol.iterator] = function () { return new custom_1.ConcatIterator(this.source, this.otherSources, streamSourceHelpers); }; ConcatStream.prototype.forEach = function (f, options) { if (options === void 0) { options = {}; } var _b = options.state, state = _b === void 0 ? (0, common_1.TraverseState)() : _b; if (state.halted) return; this.source.forEach(f, { state: state }); var sourceIndex = -1; var sources = this.otherSources; var length = sources.length; while (!state.halted && ++sourceIndex < length) { var source = sources[sourceIndex]; if (!isEmptyStreamSourceInstance(source)) { (0, exports.fromStreamSource)(source).forEach(f, { state: state }); } } }; ConcatStream.prototype.forEachPure = function (f) { var _b, _c; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } (_b = this.source).forEachPure.apply(_b, tslib_1.__spreadArray([f], tslib_1.__read(args), false)); var sourceIndex = -1; var sources = this.otherSources; var length = sources.length; while (++sourceIndex < length) { var source = sources[sourceIndex]; if (!isEmptyStreamSourceInstance(source)) { (_c = (0, exports.fromStreamSource)(source)).forEachPure.apply(_c, tslib_1.__spreadArray([f], tslib_1.__read(args), false)); } } }; ConcatStream.prototype.last = function (otherwise) { var sources = this.otherSources; var sourceIndex = sources.length; while (--sourceIndex >= 0) { var source = sources[sourceIndex]; if (!isEmptyStreamSourceInstance(source)) { var done = Symbol('Done'); var value = (0, exports.fromStreamSource)(source).last(done); if (done !== value) return value; } } return this.source.last(otherwise); }; ConcatStream.prototype.count = function () { var result = this.source.count(); var sources = this.otherSources; var length = sources.length; var sourceIndex = -1; while (++sourceIndex < length) { var source = sources[sourceIndex]; if (!isEmptyStreamSourceInstance(source)) { result += (0, exports.fromStreamSource)(source).count(); } } return result; }; ConcatStream.prototype.filterPure = function (options) { var _b; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return new ConcatStream((_b = this.source).filterPure.apply(_b, tslib_1.__spreadArray([options], tslib_1.__read(args), false)), this.otherSources.map(function (source) { var _b; return (_b = (0, exports.fromStreamSource)(source)).filterPure.apply(_b, tslib_1.__spreadArray([options], tslib_1.__read(args), false)); })); }; ConcatStream.prototype.mapPure = function (mapFun) { var _b; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return new ConcatStream((_b = this.source).mapPure.apply(_b, tslib_1.__spreadArray([mapFun], tslib_1.__read(args), false)), this.otherSources.map(function (source) { var _b; return (_b = (0, exports.fromStreamSource)(source)).mapPure.apply(_b, tslib_1.__spreadArray([mapFun], tslib_1.__read(args), false)); })); }; ConcatStream.prototype.concat = function () { var others2 = []; for (var _i = 0; _i < arguments.length; _i++) { others2[_i] = arguments[_i]; } return new ConcatStream(this.source, this.otherSources.concat(others2)); }; ConcatStream.prototype.toArray = function () { var result = this.source.toArray(); var sourceIndex = -1; var sources = this.otherSources; var length = sources.length; while (++sourceIndex < length) { var source = sources[sourceIndex]; if (!isEmptyStreamSourceInstance(source)) { result = result.concat((0, exports.fromStreamSource)(source).toArray()); } } return result; }; return ConcatStream; }(StreamBase)); var IndexedStream = /** @class */ (function (_super) { tslib_1.__extends(IndexedStream, _super); function IndexedStream(source, startIndex) { var _this = _super.call(this) || this; _this.source = source; _this.startIndex = startIndex; return _this; } IndexedStream.prototype[Symbol.iterator] = function () { return new custom_1.IndexedIterator(this.source[Symbol.iterator](), this.startIndex); }; IndexedStream.prototype.first = function (otherwise) { var token = Symbol(); var sourceFirst = this.source.first(token); if (token === sourceFirst) { return (0, common_1.OptLazy)(otherwise); } return [0, sourceFirst]; }; IndexedStream.prototype.count = function () { return this.source.count(); }; IndexedStream.prototype.elementAt = function (index, otherwise) { var token = Symbol(); var elementAtSource = this.source.elementAt(index, token); if (token === elementAtSource) { return (0, common_1.OptLazy)(otherwise); } return [index, elementAtSource]; }; IndexedStream.prototype.take = function (amount) { if (amount <= 0) { return emptyStream; } return new IndexedStream(this.source.take(amount), this.startIndex); }; IndexedStream.prototype.toArray = function () { var index = this.startIndex; var iterator = this.source[Symbol.iterator](); var result = []; var done = Symbol('Done'); var value; while (done !== (value = iterator.fastNext(done))) { result.push([index++, value]); } return result; }; return IndexedStream; }(StreamBase)); var FilterStream = /** @class */ (function (_super) { tslib_1.__extends(FilterStream, _super); function FilterStream(source, pred, negate) { if (negate === void 0) { negate = false; } var _this = _super.call(this) || this; _this.source = source; _this.pred = pred; _this.negate = negate; return _this; } FilterStream.prototype[Symbol.iterator] = function () { return new custom_1.FilterIterator(this.source[Symbol.iterator](), this.pred, this.negate); }; FilterStream.prototype.filterPure = function (options) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var pred = options.pred, _b = options.negate, negate = _b === void 0 ? false : _b; var _c = this, thisPred = _c.pred, thisNegate = _c.negate; return new FilterStream(this.source, function (value, index, halt) { return thisPred(value, index, halt) !== thisNegate && pred.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(args), false)) !== negate; }); }; FilterStream.prototype.mapPure = function (mapFun) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var _b = this, pred = _b.pred, negate = _b.negate; return new CollectStream(this.source, function (value, index, skip, halt) { return pred(value, index, halt) !== negate ? mapFun.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(args), false)) : skip; }); }; return FilterStream; }(StreamBase)); var FilterPureStream = /** @class */ (function (_super) { tslib_1.__extends(FilterPureStream, _super); function FilterPureStream(source, pred, args, negate) { if (negate === void 0) { negate = false; } var _this = _super.call(this) || this; _this.source = source; _this.pred = pred; _this.args = args; _this.negate = negate; return _this; } FilterPureStream.prototype[Symbol.iterator] = function () { return new custom_1.FilterPureIterator(this.source[Symbol.iterator](), this.pred, this.args, this.negate); }; FilterPureStream.prototype.filterPure = function (options) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var pred = options.pred, _b = options.negate, negate = _b === void 0 ? false : _b; var thisPred = this.pred; var thisArgs = this.args; var thisNegate = this.negate; return new FilterPureStream(this.source, function (value) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return (thisPred.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(thisArgs), false)) !== thisNegate && pred.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(args), false)) !== negate); }, args); }; FilterPureStream.prototype.mapPure = function (mapFun) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var _b = this, pred = _b.pred, negate = _b.negate, thisArgs = _b.args; return new CollectStream(this.source, function (value, _, skip) { return pred.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(thisArgs), false)) !== negate ? mapFun.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(args), false)) : skip; }); }; return FilterPureStream; }(StreamBase)); var CollectStream = /** @class */ (function (_super) { tslib_1.__extends(CollectStream, _super); function CollectStream(source, collectFun) { var _this = _super.call(this) || this; _this.source = source; _this.collectFun = collectFun; return _this; } CollectStream.prototype[Symbol.iterator] = function () { return new custom_1.CollectIterator(this.source[Symbol.iterator](), this.collectFun); }; CollectStream.prototype.filterPure = function (options) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var pred = options.pred, _b = options.negate, negate = _b === void 0 ? false : _b; var collectFun = this.collectFun; return new CollectStream(this.source, function (value, index, skip, halt) { var result = collectFun(value, index, skip, halt); if (skip === result || pred.apply(void 0, tslib_1.__spreadArray([result], tslib_1.__read(args), false)) === negate) { return skip; } return result; }); }; CollectStream.prototype.mapPure = function (mapFun) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var collectFun = this.collectFun; return new CollectStream(this.source, function (value, index, skip, halt) { var result = collectFun(value, index, skip, halt); if (skip === result) { return skip; } return mapFun.apply(void 0, tslib_1.__spreadArray([result], tslib_1.__read(args), false)); }); }; return CollectStream; }(StreamBase)); var DropWhileStream = /** @class */ (function (_super) { tslib_1.__extends(DropWhileStream, _super); function DropWhileStream(source, pred, negate) { var _this = _super.call(this) || this; _this.source = source; _this.pred = pred; _this.negate = negate; return _this; } DropWhileStream.prototype[Symbol.iterator] = function () { return new custom_1.DropWhileIterator(this.source[Symbol.iterator](), this.pred, this.negate); }; return DropWhileStream; }(StreamBase)); var TakeStream = /** @class */ (function (_super) { tslib_1.__extends(TakeStream, _super); function TakeStream(source, amount) { var _this = _super.call(this) || this; _this.source = source; _this.amount = amount; return _this; } TakeStream.prototype[Symbol.iterator] = function () { return new custom_1.TakeIterator(this.source[Symbol.iterator](), this.amount); }; TakeStream.prototype.take = function (amount) { if (amount <= 0) { return emptyStream; } if (amount >= this.amount) { return this; } return this.source.take(amount); }; return TakeStream; }(StreamBase)); var DropStream = /** @class */ (function (_super) { tslib_1.__extends(DropStream, _super); function DropStream(source, amount) { var _this = _super.call(this) || this; _this.source = source; _this.amount = amount; return _this; } DropStream.prototype[Symbol.iterator] = function () { return new custom_1.DropIterator(this.source[Symbol.iterator](), this.amount); }; DropStream.prototype.drop = function (amount) { if (amount <= 0) { return this; } return this.source.drop(this.amount + amount); }; return DropStream; }(StreamBase)); var SlowIteratorAdapter = /** @class */ (function () { function SlowIteratorAdapter(source) { this.source = source; } SlowIteratorAdapter.prototype.next = function () { return this.source.next(); }; SlowIteratorAdapter.prototype.fastNext = function (otherwise) { var result = this.source.next(); if (result.done) { return (0, common_1.OptLazy)(otherwise); } return result.value; }; return SlowIteratorAdapter; }()); var FromIterable = /** @class */ (function (_super) { tslib_1.__extends(FromIterable, _super); function FromIterable(iterable) { var _this = _super.call(this) || this; _this.iterable = iterable; return _this; } FromIterable.prototype[Symbol.iterator] = function () { var iterator = this.iterable[Symbol.iterator](); if ((0, custom_1.isFastIterator)(iterator)) return iterator; return new SlowIteratorAdapter(iterator); }; return FromIterable; }(StreamBase)); var EmptyStream = /** @class */ (function (_super) { tslib_1.__extends(EmptyStream, _super); function EmptyStream() { return _super !== null && _super.apply(this, arguments) || this; } EmptyStream.prototype[Symbol.iterator] = function () { return custom_1.emptyFastIterator; }; EmptyStream.prototype.stream = function () { return this; }; EmptyStream.prototype.assumeNonEmpty = function () { base_1.RimbuError.throwEmptyCollectionAssumedNonEmptyError(); }; EmptyStream.prototype.equals = function (other) { var done = Symbol('Done'); return done === (0, exports.fromStreamSource)(other)[Symbol.iterator]().fastNext(done); }; EmptyStream.prototype.prepend = function (value) { return exports.StreamConstructorsImpl.of((0, common_1.OptLazy)(value)); }; EmptyStream.prototype.append = function (value) { return exports.StreamConstructorsImpl.of((0, common_1.OptLazy)(value)); }; EmptyStream.prototype.forEach = function () { // }; EmptyStream.prototype.forEachPure = function () { // }; EmptyStream.prototype.indexed = function () { return this; }; EmptyStream.prototype.map = function () { return this; }; EmptyStream.prototype.mapPure = function () { return this; }; EmptyStream.prototype.flatMap = function () { return this; }; EmptyStream.prototype.flatZip = function () { return this; }; EmptyStream.prototype.transform = function (transformer) { return (0, exports.fromStreamSource)(transformer.compile().getOutput()); }; EmptyStream.prototype.filter = function () { return this; }; EmptyStream.prototype.filterPure = function () { return this; }; EmptyStream.prototype.withOnly = function () { return this; }; EmptyStream.prototype.without = function () { return this; }; EmptyStream.prototype.collect = function () { return this; }; EmptyStream.prototype.first = function (otherwise) { return (0, common_1.OptLazy)(otherwise); }; EmptyStream.prototype.last = function (otherwise) { return (0, common_1.OptLazy)(otherwise); }; EmptyStream.prototype.single = function (otherwise) { return (0, common_1.OptLazy)(otherwise); }; EmptyStream.prototype.count = function () { return 0; }; EmptyStream.prototype.countElement = function () { return 0; }; EmptyStream.prototype.find = function (pred, options) { if (options === void 0) { options = {}; } var otherwise = options.otherwise; return (0, common_1.OptLazy)(otherwise); }; EmptyStream.prototype.elementAt = function (index, otherwise) { return (0, common_1.OptLazy)(otherwise); }; EmptyStream.prototype.indicesWhere = function () { return this; }; EmptyStream.prototype.indicesOf = function () { return this; }; EmptyStream.prototype.indexWhere = function () { return undefined; }; EmptyStream.prototype.indexOf = function () { return undefined; }; EmptyStream.prototype.some = function ()