UNPKG

1.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.LazyList = void 0;
4const lazy_1 = require("./lazy");
5/**
6 * @description lazy linked list
7 * */
8class LazyList extends lazy_1.Lazy {
9 constructor(value, tail, mapper) {
10 super(value || (() => undefined));
11 if (arguments.length === 0) {
12 this.isHead = true;
13 }
14 this.tail = tail;
15 this.mapper = mapper;
16 }
17 append(a) {
18 return new LazyList(a, this, this.mapper);
19 }
20 appendRaw(a) {
21 return this.append(() => a);
22 }
23 appendAll(xs) {
24 return xs.reduce((acc, c) => acc.appendRaw(c), this);
25 }
26 /** @description non-lazy */
27 toArray(thisArg = this) {
28 const xs = [];
29 for (let c = thisArg; c && !c.isHead; c = c.tail) {
30 if (c.mapper) {
31 xs.push(c.mapper(c.value()));
32 }
33 else {
34 xs.push(c.value());
35 }
36 }
37 return xs;
38 }
39 /** @override */
40 map(f) {
41 if (this.mapper) {
42 const mapper = this.mapper;
43 return new LazyList(() => this.value(), this.tail, a => f(mapper(a)));
44 }
45 else {
46 return new LazyList(() => this.value(), this.tail, f);
47 }
48 }
49}
50exports.LazyList = LazyList;
51(function (LazyList) {
52 LazyList.headSymbol = Symbol.for('head');
53 LazyList.empty = () => new LazyList();
54 LazyList.fromArray = (xs) => LazyList.empty().appendAll(xs);
55})(LazyList = exports.LazyList || (exports.LazyList = {}));
56//# sourceMappingURL=lazy-list.js.map
\No newline at end of file