eliscript 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +135 -0
  3. package/bin/eliscript +16 -0
  4. package/bin/eliscript-bootstrap +15 -0
  5. package/bin/eliscript-build +15 -0
  6. package/bin/eliscript-check +15 -0
  7. package/bin/eliscript-eval +15 -0
  8. package/bin/eliscript-format +15 -0
  9. package/bin/eliscript-org +10 -0
  10. package/bin/eliscript-portable +17 -0
  11. package/bin/eliscript-seed +10 -0
  12. package/bin/eliscript-seed-build +10 -0
  13. package/bin/eliscript-watch +15 -0
  14. package/bootstrap/README.md +100 -0
  15. package/bootstrap/compiler/analyzer.eli +1426 -0
  16. package/bootstrap/compiler/compiler.eli +472 -0
  17. package/bootstrap/compiler/emitter.eli +1745 -0
  18. package/bootstrap/compiler/expander.eli +2522 -0
  19. package/bootstrap/compiler/formatter.eli +359 -0
  20. package/bootstrap/compiler/ir.eli +297 -0
  21. package/bootstrap/compiler/lower.eli +661 -0
  22. package/bootstrap/compiler/project.eli +1120 -0
  23. package/bootstrap/compiler/reader.eli +321 -0
  24. package/bootstrap/compiler/source-map.eli +165 -0
  25. package/bootstrap/compiler/symbol.eli +92 -0
  26. package/bootstrap/compiler/syntax.eli +54 -0
  27. package/bootstrap/compiler/transient-analysis.eli +825 -0
  28. package/bootstrap/host/build.mjs +28 -0
  29. package/bootstrap/host/bun.mjs +282 -0
  30. package/bootstrap/host/check-cli.mjs +163 -0
  31. package/bootstrap/host/eliscript-cli.mjs +36 -0
  32. package/bootstrap/host/evaluation-cli.mjs +401 -0
  33. package/bootstrap/host/evaluation.mjs +560 -0
  34. package/bootstrap/host/format-cli.mjs +139 -0
  35. package/bootstrap/host/project-cli.mjs +295 -0
  36. package/bootstrap/host/project.mjs +1080 -0
  37. package/bootstrap/host/watch-cli.mjs +129 -0
  38. package/bootstrap/host/watch.mjs +267 -0
  39. package/browser/host.mjs +357 -0
  40. package/browser/worker.mjs +60 -0
  41. package/compiler/README.md +118 -0
  42. package/compiler/eliscript-analyzer.el +716 -0
  43. package/compiler/eliscript-cli.el +108 -0
  44. package/compiler/eliscript-diagnostic.el +158 -0
  45. package/compiler/eliscript-emitter.el +1351 -0
  46. package/compiler/eliscript-evaluation.el +259 -0
  47. package/compiler/eliscript-expander.el +1630 -0
  48. package/compiler/eliscript-form.el +79 -0
  49. package/compiler/eliscript-ir-emitter.el +1460 -0
  50. package/compiler/eliscript-ir.el +356 -0
  51. package/compiler/eliscript-lower.el +463 -0
  52. package/compiler/eliscript-macro-eval.el +811 -0
  53. package/compiler/eliscript-parameters.el +324 -0
  54. package/compiler/eliscript-portable.el +492 -0
  55. package/compiler/eliscript-project-cli.el +156 -0
  56. package/compiler/eliscript-project.el +1676 -0
  57. package/compiler/eliscript-reader.el +421 -0
  58. package/compiler/eliscript-source-map.el +192 -0
  59. package/compiler/eliscript-symbol.el +101 -0
  60. package/compiler/eliscript-transient-analysis.el +817 -0
  61. package/compiler/eliscript.el +239 -0
  62. package/dist/bootstrap/analyzer.mjs +968 -0
  63. package/dist/bootstrap/analyzer.mjs.map +1 -0
  64. package/dist/bootstrap/compiler.mjs +228 -0
  65. package/dist/bootstrap/compiler.mjs.map +1 -0
  66. package/dist/bootstrap/emitter.mjs +1173 -0
  67. package/dist/bootstrap/emitter.mjs.map +1 -0
  68. package/dist/bootstrap/expander.mjs +1469 -0
  69. package/dist/bootstrap/expander.mjs.map +1 -0
  70. package/dist/bootstrap/formatter.mjs +304 -0
  71. package/dist/bootstrap/formatter.mjs.map +1 -0
  72. package/dist/bootstrap/ir.mjs +193 -0
  73. package/dist/bootstrap/ir.mjs.map +1 -0
  74. package/dist/bootstrap/lower.mjs +344 -0
  75. package/dist/bootstrap/lower.mjs.map +1 -0
  76. package/dist/bootstrap/project.mjs +585 -0
  77. package/dist/bootstrap/project.mjs.map +1 -0
  78. package/dist/bootstrap/reader.mjs +259 -0
  79. package/dist/bootstrap/reader.mjs.map +1 -0
  80. package/dist/bootstrap/source-map.mjs +154 -0
  81. package/dist/bootstrap/source-map.mjs.map +1 -0
  82. package/dist/bootstrap/symbol.mjs +74 -0
  83. package/dist/bootstrap/symbol.mjs.map +1 -0
  84. package/dist/bootstrap/syntax.mjs +38 -0
  85. package/dist/bootstrap/syntax.mjs.map +1 -0
  86. package/dist/bootstrap/transient-analysis.mjs +660 -0
  87. package/dist/bootstrap/transient-analysis.mjs.map +1 -0
  88. package/dist/browser/compiler.js +6491 -0
  89. package/editor/README.md +83 -0
  90. package/editor/eliscript-mode.el +878 -0
  91. package/editor/eliscript-repl.el +501 -0
  92. package/package.json +163 -0
  93. package/platform/README.md +48 -0
  94. package/platform/browser.mjs +204 -0
  95. package/platform/worker.mjs +166 -0
  96. package/runtime/README.md +296 -0
  97. package/runtime/core/collection-internals.mjs +512 -0
  98. package/runtime/core/collection.mjs +645 -0
  99. package/runtime/core/data-text.mjs +532 -0
  100. package/runtime/core/data.mjs +268 -0
  101. package/runtime/core/identifier.mjs +314 -0
  102. package/runtime/core/lazy-sequence.mjs +244 -0
  103. package/runtime/core/list-internals.mjs +18 -0
  104. package/runtime/core/list.mjs +273 -0
  105. package/runtime/core/map-internals.mjs +793 -0
  106. package/runtime/core/map.mjs +394 -0
  107. package/runtime/core/metadata-internals.mjs +14 -0
  108. package/runtime/core/metadata.mjs +44 -0
  109. package/runtime/core/object-impl.mjs +65 -0
  110. package/runtime/core/object-impl.mjs.map +1 -0
  111. package/runtime/core/object.mjs +13 -0
  112. package/runtime/core/order.mjs +234 -0
  113. package/runtime/core/persistent-kind.mjs +43 -0
  114. package/runtime/core/protocol-error.mjs +36 -0
  115. package/runtime/core/protocol-impl.mjs +326 -0
  116. package/runtime/core/protocol-impl.mjs.map +1 -0
  117. package/runtime/core/protocol.mjs +14 -0
  118. package/runtime/core/queue-internals.mjs +27 -0
  119. package/runtime/core/queue.mjs +238 -0
  120. package/runtime/core/record.mjs +346 -0
  121. package/runtime/core/regex.mjs +184 -0
  122. package/runtime/core/sequence.mjs +632 -0
  123. package/runtime/core/set-algebra.mjs +287 -0
  124. package/runtime/core/set-internals.mjs +33 -0
  125. package/runtime/core/set.mjs +378 -0
  126. package/runtime/core/sorted-internals.mjs +215 -0
  127. package/runtime/core/sorted.mjs +598 -0
  128. package/runtime/core/text-impl.mjs +95 -0
  129. package/runtime/core/text-impl.mjs.map +1 -0
  130. package/runtime/core/text.mjs +15 -0
  131. package/runtime/core/transducer.mjs +668 -0
  132. package/runtime/core/transient-internals.mjs +38 -0
  133. package/runtime/core/transient.mjs +53 -0
  134. package/runtime/core/truth.mjs +3 -0
  135. package/runtime/core/type.mjs +262 -0
  136. package/runtime/core/value-internals.mjs +188 -0
  137. package/runtime/core/value.mjs +93 -0
  138. package/runtime/core/vector-internals.mjs +102 -0
  139. package/runtime/core/vector.mjs +951 -0
  140. package/runtime/literals.mjs +34 -0
  141. package/runtime/source-mapping.mjs +220 -0
  142. package/runtime/testing/list.mjs +52 -0
  143. package/runtime/testing/map.mjs +145 -0
  144. package/runtime/testing/queue.mjs +49 -0
  145. package/runtime/testing/set.mjs +61 -0
  146. package/runtime/testing/sorted.mjs +105 -0
  147. package/runtime/testing/value.mjs +17 -0
  148. package/runtime/testing/vector.mjs +102 -0
  149. package/runtime/worker-value-codec.mjs +565 -0
  150. package/runtime/worker-value-stream.mjs +786 -0
  151. package/runtime/worker.mjs +975 -0
  152. package/stdlib/README.md +774 -0
  153. package/stdlib/bit.eli +36 -0
  154. package/stdlib/core/collection.eli +134 -0
  155. package/stdlib/core/data-text.eli +16 -0
  156. package/stdlib/core/data.eli +306 -0
  157. package/stdlib/core/identifier.eli +32 -0
  158. package/stdlib/core/lazy-sequence.eli +63 -0
  159. package/stdlib/core/metadata.eli +20 -0
  160. package/stdlib/core/object.eli +89 -0
  161. package/stdlib/core/order.eli +133 -0
  162. package/stdlib/core/protocol.eli +387 -0
  163. package/stdlib/core/record.eli +16 -0
  164. package/stdlib/core/regex.eli +17 -0
  165. package/stdlib/core/seq.eli +701 -0
  166. package/stdlib/core/set.eli +312 -0
  167. package/stdlib/core/sorted.eli +53 -0
  168. package/stdlib/core/text.eli +103 -0
  169. package/stdlib/core/transducer.eli +59 -0
  170. package/stdlib/core/transient.eli +24 -0
  171. package/stdlib/core/type.eli +19 -0
  172. package/stdlib/core/walk.eli +195 -0
  173. package/stdlib/core/zipper.eli +508 -0
  174. package/stdlib/data-text.eli +1092 -0
  175. package/stdlib/data.eli +42 -0
  176. package/stdlib/deferred.eli +70 -0
  177. package/stdlib/function.eli +120 -0
  178. package/stdlib/hierarchy.eli +245 -0
  179. package/stdlib/identifier.eli +147 -0
  180. package/stdlib/interop/js.eli +616 -0
  181. package/stdlib/json.eli +870 -0
  182. package/stdlib/metadata.eli +56 -0
  183. package/stdlib/multimethod.eli +464 -0
  184. package/stdlib/numeric.eli +193 -0
  185. package/stdlib/object.eli +96 -0
  186. package/stdlib/persistent-list.eli +145 -0
  187. package/stdlib/persistent-map.eli +724 -0
  188. package/stdlib/persistent-queue.eli +32 -0
  189. package/stdlib/persistent-set.eli +236 -0
  190. package/stdlib/persistent-vector.eli +342 -0
  191. package/stdlib/result.eli +141 -0
  192. package/stdlib/sequence.eli +106 -0
  193. package/stdlib/state/atom.eli +265 -0
  194. package/stdlib/text.eli +88 -0
  195. package/stdlib/value.eli +325 -0
@@ -0,0 +1,512 @@
1
+ import {
2
+ defineProtocol,
3
+ protocolMethod,
4
+ protocolSlot,
5
+ } from "./protocol.mjs";
6
+
7
+ export const I_COUNTED = defineProtocol("ICounted", ["count"]);
8
+ export const I_EMPTYABLE = defineProtocol("IEmptyable", ["empty"]);
9
+ export const I_CONJ = defineProtocol("IConj", ["conj"]);
10
+ export const I_LOOKUP = defineProtocol("ILookup", ["get"]);
11
+ export const I_ASSOCIATIVE = defineProtocol("IAssociative", [
12
+ "assoc",
13
+ "contains",
14
+ ]);
15
+ export const I_INDEXED = defineProtocol("IIndexed", ["nth"]);
16
+ export const I_SEQABLE = defineProtocol("ISeqable", ["seq"]);
17
+ export const I_REDUCE = defineProtocol("IReduce", ["reduce"]);
18
+ export const I_KV_REDUCE = defineProtocol("IKVReduce", ["reduceKV"]);
19
+ export const I_MAP = defineProtocol("IMap", ["dissoc"]);
20
+ export const I_SET = defineProtocol("ISet", ["disj"]);
21
+ export const I_STACK = defineProtocol("IStack", ["peek", "pop"]);
22
+ export const I_REVERSIBLE = defineProtocol("IReversible", ["rseq"]);
23
+
24
+ export const COLLECTION_COUNT = protocolSlot(I_COUNTED, "count");
25
+ export const COLLECTION_EMPTY = protocolSlot(I_EMPTYABLE, "empty");
26
+ export const COLLECTION_CONJ = protocolSlot(I_CONJ, "conj");
27
+ export const COLLECTION_GET = protocolSlot(I_LOOKUP, "get");
28
+ export const COLLECTION_ASSOC = protocolSlot(I_ASSOCIATIVE, "assoc");
29
+ export const COLLECTION_CONTAINS = protocolSlot(I_ASSOCIATIVE, "contains");
30
+ export const COLLECTION_NTH = protocolSlot(I_INDEXED, "nth");
31
+ export const COLLECTION_SEQ = protocolSlot(I_SEQABLE, "seq");
32
+ export const COLLECTION_REDUCE = protocolSlot(I_REDUCE, "reduce");
33
+ export const COLLECTION_REDUCE_KV = protocolSlot(I_KV_REDUCE, "reduceKV");
34
+ export const COLLECTION_DISSOC = protocolSlot(I_MAP, "dissoc");
35
+ export const COLLECTION_DISJ = protocolSlot(I_SET, "disj");
36
+ export const COLLECTION_PEEK = protocolSlot(I_STACK, "peek");
37
+ export const COLLECTION_POP = protocolSlot(I_STACK, "pop");
38
+ export const COLLECTION_RSEQ = protocolSlot(I_REVERSIBLE, "rseq");
39
+
40
+ export const dispatchCollectionCount = protocolMethod(I_COUNTED, "count");
41
+ export const dispatchCollectionEmpty = protocolMethod(I_EMPTYABLE, "empty");
42
+ export const dispatchCollectionConj = protocolMethod(I_CONJ, "conj");
43
+ export const dispatchCollectionGet = protocolMethod(I_LOOKUP, "get");
44
+ export const dispatchCollectionAssoc = protocolMethod(I_ASSOCIATIVE, "assoc");
45
+ export const dispatchCollectionContains = protocolMethod(
46
+ I_ASSOCIATIVE,
47
+ "contains",
48
+ );
49
+ export const dispatchCollectionNth = protocolMethod(I_INDEXED, "nth");
50
+ export const dispatchCollectionSeq = protocolMethod(I_SEQABLE, "seq");
51
+ export const dispatchCollectionReduce = protocolMethod(I_REDUCE, "reduce");
52
+ export const dispatchCollectionReduceKV = protocolMethod(I_KV_REDUCE, "reduceKV");
53
+ export const dispatchCollectionDissoc = protocolMethod(I_MAP, "dissoc");
54
+ export const dispatchCollectionDisj = protocolMethod(I_SET, "disj");
55
+ export const dispatchCollectionPeek = protocolMethod(I_STACK, "peek");
56
+ export const dispatchCollectionPop = protocolMethod(I_STACK, "pop");
57
+ export const dispatchCollectionRseq = protocolMethod(I_REVERSIBLE, "rseq");
58
+
59
+ const NO_INITIAL = Symbol("eliscript.collection.no-initial");
60
+ const REDUCED_STATE = Symbol("eliscript.collection.reduced-state");
61
+ const REDUCTION_STATE = Symbol("eliscript.collection.reduction-state");
62
+ const REDUCTION_TOKEN = Symbol("eliscript.collection.reduction-token");
63
+ const MEMOIZED_SEQUENCE_STATE = Symbol(
64
+ "eliscript.collection.memoized-sequence-state",
65
+ );
66
+ const MEMOIZED_SEQUENCE_TOKEN = Symbol(
67
+ "eliscript.collection.memoized-sequence-token",
68
+ );
69
+ const NO_MEMOIZED_SEQUENCE_FAILURE = Symbol(
70
+ "eliscript.collection.no-memoized-sequence-failure",
71
+ );
72
+ const SEQUENCE_STATE = Symbol("eliscript.collection.sequence-state");
73
+ const SEQUENCE_TOKEN = Symbol("eliscript.collection.sequence-token");
74
+ const UNBOUNDED_SEQUENCE = Symbol("eliscript.collection.unbounded-sequence");
75
+
76
+ function checkedCount(value) {
77
+ if (!Number.isSafeInteger(value) || value < 0) {
78
+ throw new TypeError("collection count must be a non-negative safe integer");
79
+ }
80
+ return value;
81
+ }
82
+
83
+ function iteratorFromFactory(factory) {
84
+ const iterator = factory();
85
+ if (iterator === null || typeof iterator !== "object" ||
86
+ typeof iterator.next !== "function") {
87
+ throw new TypeError("sequence view factory must return an iterator");
88
+ }
89
+ return iterator;
90
+ }
91
+
92
+ class MemoizedSequenceSource {
93
+ constructor(factory) {
94
+ this.factory = factory;
95
+ this.source = null;
96
+ this.values = [];
97
+ this.started = false;
98
+ this.complete = false;
99
+ this.failure = NO_MEMOIZED_SEQUENCE_FAILURE;
100
+ }
101
+
102
+ read(index) {
103
+ if (index < this.values.length) {
104
+ return { value: this.values[index], done: false };
105
+ }
106
+ if (this.failure !== NO_MEMOIZED_SEQUENCE_FAILURE) throw this.failure;
107
+ if (this.complete) return { value: undefined, done: true };
108
+ if (!this.started) {
109
+ this.started = true;
110
+ try {
111
+ this.source = iteratorFromFactory(this.factory);
112
+ this.factory = null;
113
+ } catch (error) {
114
+ this.failure = error;
115
+ throw error;
116
+ }
117
+ }
118
+ try {
119
+ const result = this.source.next();
120
+ if (result === null || typeof result !== "object" ||
121
+ typeof result.done !== "boolean") {
122
+ throw new TypeError("sequence iterator must return an iterator result");
123
+ }
124
+ if (result.done) {
125
+ this.complete = true;
126
+ this.source = null;
127
+ return { value: undefined, done: true };
128
+ }
129
+ this.values.push(result.value);
130
+ return { value: result.value, done: false };
131
+ } catch (error) {
132
+ const source = this.source;
133
+ this.failure = error;
134
+ this.source = null;
135
+ if (source !== null && typeof source.return === "function") {
136
+ try {
137
+ source.return();
138
+ } catch {
139
+ // Preserve the original realization failure as the stable result.
140
+ }
141
+ }
142
+ throw error;
143
+ }
144
+ }
145
+
146
+ iterator() {
147
+ const source = this;
148
+ let index = 0;
149
+ let closed = false;
150
+ return {
151
+ next() {
152
+ if (closed) return { value: undefined, done: true };
153
+ const result = source.read(index);
154
+ if (!result.done) index += 1;
155
+ return result;
156
+ },
157
+ return(value) {
158
+ closed = true;
159
+ return { value, done: true };
160
+ },
161
+ [Symbol.iterator]() {
162
+ return this;
163
+ },
164
+ };
165
+ }
166
+ }
167
+
168
+ export function readCollectionEntry(entry) {
169
+ if (entry === null || entry === undefined ||
170
+ typeof entry[Symbol.iterator] !== "function") {
171
+ throw new TypeError(
172
+ "persistent hash map entries must be iterable key/value pairs",
173
+ );
174
+ }
175
+ const iterator = iteratorFromFactory(() => entry[Symbol.iterator]());
176
+ let complete = false;
177
+ try {
178
+ const first = iterator.next();
179
+ if (first.done) {
180
+ throw new TypeError(
181
+ "persistent hash map entries must contain exactly two values",
182
+ );
183
+ }
184
+ const second = iterator.next();
185
+ if (second.done) {
186
+ throw new TypeError(
187
+ "persistent hash map entries must contain exactly two values",
188
+ );
189
+ }
190
+ const third = iterator.next();
191
+ if (!third.done) {
192
+ throw new TypeError(
193
+ "persistent hash map entries must contain exactly two values",
194
+ );
195
+ }
196
+ complete = true;
197
+ return [first.value, second.value];
198
+ } finally {
199
+ if (!complete && typeof iterator.return === "function") {
200
+ iterator.return();
201
+ }
202
+ }
203
+ }
204
+
205
+ function sequenceCount(state) {
206
+ if (state.count === UNBOUNDED_SEQUENCE) {
207
+ throw new RangeError("unbounded sequence does not have a finite count");
208
+ }
209
+ if (typeof state.count === "function") {
210
+ return checkedCount(state.count());
211
+ }
212
+ if (state.count !== null) {
213
+ return state.count;
214
+ }
215
+ const iterator = iteratorFromFactory(state.factory);
216
+ let count = 0;
217
+ while (!iterator.next().done) {
218
+ count += 1;
219
+ checkedCount(count);
220
+ }
221
+ return count;
222
+ }
223
+
224
+ function slicedSequenceCount(state, offset) {
225
+ if (state.count === UNBOUNDED_SEQUENCE) {
226
+ return UNBOUNDED_SEQUENCE;
227
+ }
228
+ if (typeof state.count === "function") {
229
+ return () => Math.max(0, sequenceCount(state) - offset);
230
+ }
231
+ if (state.count === null) {
232
+ return null;
233
+ }
234
+ return Math.max(0, state.count - offset);
235
+ }
236
+
237
+ function prependedSequenceCount(state) {
238
+ if (state.count === UNBOUNDED_SEQUENCE) {
239
+ return UNBOUNDED_SEQUENCE;
240
+ }
241
+ if (typeof state.count === "function") {
242
+ return () => checkedCount(sequenceCount(state) + 1);
243
+ }
244
+ if (state.count === null) {
245
+ return null;
246
+ }
247
+ return checkedCount(state.count + 1);
248
+ }
249
+
250
+ export class SequenceView {
251
+ constructor(token, factory, count) {
252
+ if (token !== SEQUENCE_TOKEN || typeof factory !== "function") {
253
+ throw new TypeError("SequenceView values must be created by seq");
254
+ }
255
+ this[SEQUENCE_STATE] = Object.freeze({ factory, count });
256
+ Object.freeze(this);
257
+ }
258
+
259
+ [COLLECTION_COUNT]() {
260
+ return sequenceCount(this[SEQUENCE_STATE]);
261
+ }
262
+
263
+ [COLLECTION_SEQ]() {
264
+ const state = this[SEQUENCE_STATE];
265
+ if (state.count === UNBOUNDED_SEQUENCE) {
266
+ return this;
267
+ }
268
+ if (state.count === null) {
269
+ const iterator = iteratorFromFactory(state.factory);
270
+ try {
271
+ return iterator.next().done ? null : this;
272
+ } finally {
273
+ if (typeof iterator.return === "function") {
274
+ iterator.return();
275
+ }
276
+ }
277
+ }
278
+ return this[COLLECTION_COUNT]() === 0 ? null : this;
279
+ }
280
+
281
+ [COLLECTION_REDUCE](reducer, ...initial) {
282
+ return reduceIterable(this, reducer, ...initial);
283
+ }
284
+
285
+ [Symbol.iterator]() {
286
+ return iteratorFromFactory(this[SEQUENCE_STATE].factory);
287
+ }
288
+
289
+ get [Symbol.toStringTag]() {
290
+ return "EliscriptSequenceView";
291
+ }
292
+ }
293
+
294
+ export class MemoizedSequenceView {
295
+ constructor(token, factory, count) {
296
+ if (token !== MEMOIZED_SEQUENCE_TOKEN || typeof factory !== "function") {
297
+ throw new TypeError(
298
+ "MemoizedSequenceView values must be created by memoizedSequenceView",
299
+ );
300
+ }
301
+ this[MEMOIZED_SEQUENCE_STATE] = Object.freeze({
302
+ source: new MemoizedSequenceSource(factory),
303
+ count,
304
+ });
305
+ Object.freeze(this);
306
+ }
307
+
308
+ [COLLECTION_COUNT]() {
309
+ return sequenceCount({
310
+ factory: () => this[Symbol.iterator](),
311
+ count: this[MEMOIZED_SEQUENCE_STATE].count,
312
+ });
313
+ }
314
+
315
+ [COLLECTION_SEQ]() {
316
+ const iterator = this[Symbol.iterator]();
317
+ try {
318
+ return iterator.next().done ? null : this;
319
+ } finally {
320
+ iterator.return();
321
+ }
322
+ }
323
+
324
+ [COLLECTION_REDUCE](reducer, ...initial) {
325
+ return reduceIterable(this, reducer, ...initial);
326
+ }
327
+
328
+ [Symbol.iterator]() {
329
+ return this[MEMOIZED_SEQUENCE_STATE].source.iterator();
330
+ }
331
+
332
+ get [Symbol.toStringTag]() {
333
+ return "EliscriptMemoizedSequenceView";
334
+ }
335
+ }
336
+
337
+ export class ReductionView {
338
+ constructor(token, reduceFunction) {
339
+ if (token !== REDUCTION_TOKEN || typeof reduceFunction !== "function") {
340
+ throw new TypeError("ReductionView values must be created by reductionView");
341
+ }
342
+ this[REDUCTION_STATE] = reduceFunction;
343
+ Object.freeze(this);
344
+ }
345
+
346
+ [COLLECTION_REDUCE](reducer, ...initial) {
347
+ return this[REDUCTION_STATE](reducer, ...initial);
348
+ }
349
+
350
+ get [Symbol.toStringTag]() {
351
+ return "EliscriptReductionView";
352
+ }
353
+ }
354
+
355
+ export function sequenceView(factory, count = null) {
356
+ if (typeof factory !== "function") {
357
+ throw new TypeError("sequence view factory must be a function");
358
+ }
359
+ if (count !== null && typeof count !== "function") {
360
+ checkedCount(count);
361
+ if (count === 0) {
362
+ return null;
363
+ }
364
+ }
365
+ return new SequenceView(SEQUENCE_TOKEN, factory, count);
366
+ }
367
+
368
+ export function unboundedSequenceView(factory) {
369
+ if (typeof factory !== "function") {
370
+ throw new TypeError("unbounded sequence view factory must be a function");
371
+ }
372
+ return new SequenceView(SEQUENCE_TOKEN, factory, UNBOUNDED_SEQUENCE);
373
+ }
374
+
375
+ export function memoizedSequenceView(factory, count = null) {
376
+ if (typeof factory !== "function") {
377
+ throw new TypeError("memoized sequence view factory must be a function");
378
+ }
379
+ if (count !== null) {
380
+ checkedCount(count);
381
+ if (count === 0) return null;
382
+ }
383
+ return new MemoizedSequenceView(MEMOIZED_SEQUENCE_TOKEN, factory, count);
384
+ }
385
+
386
+ export function memoizedSequenceStatus(value) {
387
+ if (!(value instanceof MemoizedSequenceView)) {
388
+ throw new TypeError("expected a memoized sequence view");
389
+ }
390
+ const source = value[MEMOIZED_SEQUENCE_STATE].source;
391
+ return Object.freeze({
392
+ started: source.started,
393
+ complete: source.complete,
394
+ failed: source.failure !== NO_MEMOIZED_SEQUENCE_FAILURE,
395
+ realizedCount: source.values.length,
396
+ });
397
+ }
398
+
399
+ export function sliceSequenceView(sequence, offset) {
400
+ if (sequence === null) {
401
+ return null;
402
+ }
403
+ if (!(sequence instanceof SequenceView) &&
404
+ !(sequence instanceof MemoizedSequenceView)) {
405
+ throw new TypeError("sequence slice expects a SequenceView or nil");
406
+ }
407
+ checkedCount(offset);
408
+ if (offset === 0) {
409
+ return sequence;
410
+ }
411
+
412
+ const state = sequence instanceof SequenceView
413
+ ? sequence[SEQUENCE_STATE]
414
+ : {
415
+ factory: () => sequence[Symbol.iterator](),
416
+ count: sequence[MEMOIZED_SEQUENCE_STATE].count,
417
+ };
418
+ const factory = () => {
419
+ const iterator = sequence[Symbol.iterator]();
420
+ let remaining = offset;
421
+ while (remaining > 0) {
422
+ if (iterator.next().done) {
423
+ return iterator;
424
+ }
425
+ remaining -= 1;
426
+ }
427
+ return iterator;
428
+ };
429
+ const count = slicedSequenceCount(state, offset);
430
+ return count === UNBOUNDED_SEQUENCE
431
+ ? unboundedSequenceView(factory)
432
+ : sequenceView(factory, count);
433
+ }
434
+
435
+ export function prependSequenceView(value, sequence) {
436
+ if (sequence !== null && !(sequence instanceof SequenceView) &&
437
+ !(sequence instanceof MemoizedSequenceView)) {
438
+ throw new TypeError("sequence prepend expects a SequenceView or nil");
439
+ }
440
+
441
+ const factory = () => (function* prependedValues() {
442
+ yield value;
443
+ if (sequence !== null) {
444
+ yield* sequence;
445
+ }
446
+ })();
447
+ if (sequence === null) {
448
+ return sequenceView(factory, 1);
449
+ }
450
+
451
+ const state = sequence instanceof SequenceView
452
+ ? sequence[SEQUENCE_STATE]
453
+ : {
454
+ factory: () => sequence[Symbol.iterator](),
455
+ count: sequence[MEMOIZED_SEQUENCE_STATE].count,
456
+ };
457
+ const count = prependedSequenceCount(state);
458
+ return count === UNBOUNDED_SEQUENCE
459
+ ? unboundedSequenceView(factory)
460
+ : sequenceView(factory, count);
461
+ }
462
+
463
+ export function reductionView(reduceFunction) {
464
+ if (typeof reduceFunction !== "function") {
465
+ throw new TypeError("reduction view function must be a function");
466
+ }
467
+ return new ReductionView(REDUCTION_TOKEN, reduceFunction);
468
+ }
469
+
470
+ export function reducedValue(value) {
471
+ if (isReducedValue(value)) {
472
+ return value;
473
+ }
474
+ const result = Object.create(null);
475
+ Object.defineProperty(result, REDUCED_STATE, { value });
476
+ return Object.freeze(result);
477
+ }
478
+
479
+ export function isReducedValue(value) {
480
+ return value !== null &&
481
+ (typeof value === "object" || typeof value === "function") &&
482
+ Object.prototype.hasOwnProperty.call(value, REDUCED_STATE);
483
+ }
484
+
485
+ export function unreducedValue(value) {
486
+ return isReducedValue(value) ? value[REDUCED_STATE] : value;
487
+ }
488
+
489
+ export function reduceIterable(iterable, reducer, ...initial) {
490
+ if (typeof reducer !== "function") {
491
+ throw new TypeError("collection reducer must be a function");
492
+ }
493
+ let accumulator = initial.length === 0 ? NO_INITIAL : initial[0];
494
+ for (const value of iterable) {
495
+ if (accumulator === NO_INITIAL) {
496
+ accumulator = value;
497
+ continue;
498
+ }
499
+ accumulator = reducer(accumulator, value);
500
+ if (isReducedValue(accumulator)) {
501
+ return unreducedValue(accumulator);
502
+ }
503
+ }
504
+ if (accumulator === NO_INITIAL) {
505
+ throw new TypeError("cannot reduce an empty collection without an initial value");
506
+ }
507
+ return accumulator;
508
+ }
509
+
510
+ export function validateCollectionCount(value) {
511
+ return checkedCount(value);
512
+ }