1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.LazyList = void 0;
|
4 | const lazy_1 = require("./lazy");
|
5 |
|
6 |
|
7 |
|
8 | class 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 |
|
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 |
|
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 | }
|
50 | exports.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 |
|
\ | No newline at end of file |