@beenotung/tslib
Version:
utils library in Typescript
59 lines (58 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazyList = void 0;
const lazy_1 = require("./lazy");
/**
* @description lazy linked list
* */
class LazyList extends lazy_1.Lazy {
isHead;
tail;
mapper;
constructor(value, tail, mapper) {
super(value || (() => undefined));
if (arguments.length === 0) {
this.isHead = true;
}
this.tail = tail;
this.mapper = mapper;
}
append(a) {
return new LazyList(a, this, this.mapper);
}
appendRaw(a) {
return this.append(() => a);
}
appendAll(xs) {
return xs.reduce((acc, c) => acc.appendRaw(c), this);
}
/** @description non-lazy */
toArray(thisArg = this) {
const xs = [];
for (let c = thisArg; c && !c.isHead; c = c.tail) {
if (c.mapper) {
xs.push(c.mapper(c.value()));
}
else {
xs.push(c.value());
}
}
return xs;
}
/** @override */
map(f) {
if (this.mapper) {
const mapper = this.mapper;
return new LazyList(() => this.value(), this.tail, a => f(mapper(a)));
}
else {
return new LazyList(() => this.value(), this.tail, f);
}
}
}
exports.LazyList = LazyList;
(function (LazyList) {
LazyList.headSymbol = Symbol.for('head');
LazyList.empty = () => new LazyList();
LazyList.fromArray = (xs) => LazyList.empty().appendAll(xs);
})(LazyList || (exports.LazyList = LazyList = {}));