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,645 @@
1
+ import {
2
+ extendProtocolCategory,
3
+ extendProtocolType,
4
+ implementsProtocol,
5
+ } from "./protocol.mjs";
6
+ import {
7
+ I_COUNTED,
8
+ I_EMPTYABLE,
9
+ I_CONJ,
10
+ I_INDEXED,
11
+ I_LOOKUP,
12
+ I_ASSOCIATIVE,
13
+ I_REDUCE,
14
+ I_KV_REDUCE,
15
+ I_MAP,
16
+ I_SET,
17
+ I_STACK,
18
+ I_REVERSIBLE,
19
+ I_SEQABLE,
20
+ ReductionView as InternalReductionView,
21
+ MemoizedSequenceView as InternalMemoizedSequenceView,
22
+ SequenceView as InternalSequenceView,
23
+ dispatchCollectionCount,
24
+ dispatchCollectionEmpty,
25
+ dispatchCollectionConj,
26
+ dispatchCollectionGet,
27
+ dispatchCollectionAssoc,
28
+ dispatchCollectionContains,
29
+ dispatchCollectionNth,
30
+ dispatchCollectionReduce,
31
+ dispatchCollectionReduceKV,
32
+ dispatchCollectionDissoc,
33
+ dispatchCollectionDisj,
34
+ dispatchCollectionPeek,
35
+ dispatchCollectionPop,
36
+ dispatchCollectionRseq,
37
+ dispatchCollectionSeq,
38
+ isReducedValue,
39
+ readCollectionEntry,
40
+ reduceIterable,
41
+ reducedValue,
42
+ reductionView as createReductionView,
43
+ memoizedSequenceView as createMemoizedSequenceView,
44
+ sequenceView as createSequenceView,
45
+ unboundedSequenceView as createUnboundedSequenceView,
46
+ unreducedValue,
47
+ validateCollectionCount,
48
+ } from "./collection-internals.mjs";
49
+ import { isPersistentList } from "./list.mjs";
50
+ import { isPersistentQueue } from "./queue.mjs";
51
+ import { isRecord } from "./record.mjs";
52
+ import { isPersistentMapValue, isPersistentSetValue } from "./persistent-kind.mjs";
53
+ import { EMPTY_SET } from "./set.mjs";
54
+ import { isPersistentVector } from "./vector.mjs";
55
+
56
+ const MISSING = Symbol("eliscript.collection.missing");
57
+
58
+ function indexedValue(values, index, notFound = MISSING) {
59
+ if (Number.isInteger(index) && index >= 0 && index < values.length) {
60
+ return values[index];
61
+ }
62
+ if (notFound !== MISSING) {
63
+ return notFound;
64
+ }
65
+ throw new RangeError(`nth index ${String(index)} is outside collection bounds`);
66
+ }
67
+
68
+ function indexedAssoc(values, index, value) {
69
+ if (!Number.isInteger(index) || index < 0 || index > values.length) {
70
+ throw new RangeError(
71
+ `assoc index ${String(index)} is outside collection bounds`,
72
+ );
73
+ }
74
+ const result = values.slice();
75
+ result[index] = value;
76
+ return result;
77
+ }
78
+
79
+ function indexedContains(values, index) {
80
+ return Number.isInteger(index) && index >= 0 && index < values.length;
81
+ }
82
+
83
+ function mapConj(values, entry) {
84
+ const [key, value] = readCollectionEntry(entry);
85
+ const result = new Map(values);
86
+ result.set(key, value);
87
+ return result;
88
+ }
89
+
90
+ function mapAssoc(values, key, value) {
91
+ const result = new Map(values);
92
+ result.set(key, value);
93
+ return result;
94
+ }
95
+
96
+ function mapDissoc(values, key) {
97
+ const result = new Map(values);
98
+ result.delete(key);
99
+ return result;
100
+ }
101
+
102
+ function setConj(values, value) {
103
+ const result = new Set(values);
104
+ result.add(value);
105
+ return result;
106
+ }
107
+
108
+ function setDisj(values, value) {
109
+ const result = new Set(values);
110
+ result.delete(value);
111
+ return result;
112
+ }
113
+
114
+ function objectAssoc(values, key, value) {
115
+ if (typeof key !== "string") {
116
+ throw new TypeError("plain object association keys must be strings");
117
+ }
118
+ return { ...values, [key]: value };
119
+ }
120
+
121
+ function objectConj(values, entry) {
122
+ const [key, value] = readCollectionEntry(entry);
123
+ return objectAssoc(values, key, value);
124
+ }
125
+
126
+ function objectDissoc(values, key) {
127
+ if (typeof key !== "string") {
128
+ throw new TypeError("plain object dissociation keys must be strings");
129
+ }
130
+ const result = { ...values };
131
+ delete result[key];
132
+ return result;
133
+ }
134
+
135
+ function arrayPeek(values) {
136
+ return values.length === 0 ? null : values[values.length - 1];
137
+ }
138
+
139
+ function arrayPop(values) {
140
+ if (values.length === 0) {
141
+ throw new RangeError("cannot pop an empty array");
142
+ }
143
+ return values.slice(0, -1);
144
+ }
145
+
146
+ function indexedReverseSequence(values) {
147
+ return values.length === 0
148
+ ? null
149
+ : createSequenceView(
150
+ () => {
151
+ let index = values.length;
152
+ return {
153
+ next() {
154
+ if (index === 0) return { value: undefined, done: true };
155
+ index -= 1;
156
+ return { value: values[index], done: false };
157
+ },
158
+ };
159
+ },
160
+ () => values.length,
161
+ );
162
+ }
163
+
164
+ function arraySequence(values) {
165
+ return values.length === 0
166
+ ? null
167
+ : createSequenceView(() => values[Symbol.iterator](), () => values.length);
168
+ }
169
+
170
+ function mapSequence(values) {
171
+ if (values.size === 0) {
172
+ return null;
173
+ }
174
+ return createSequenceView(
175
+ () => (function* entries() {
176
+ for (const entry of values) {
177
+ yield Object.freeze([entry[0], entry[1]]);
178
+ }
179
+ })(),
180
+ () => values.size,
181
+ );
182
+ }
183
+
184
+ function setSequence(values) {
185
+ return values.size === 0
186
+ ? null
187
+ : createSequenceView(() => values[Symbol.iterator](), () => values.size);
188
+ }
189
+
190
+ function stringSequence(value) {
191
+ return value.length === 0
192
+ ? null
193
+ : createSequenceView(
194
+ () => (function* codeUnits() {
195
+ for (let index = 0; index < value.length; index += 1) {
196
+ yield value[index];
197
+ }
198
+ })(),
199
+ () => value.length,
200
+ );
201
+ }
202
+
203
+ function objectSequence(value) {
204
+ const keys = Object.keys(value);
205
+ return keys.length === 0
206
+ ? null
207
+ : createSequenceView(
208
+ () => (function* entries() {
209
+ for (const key of Object.keys(value)) {
210
+ yield Object.freeze([key, value[key]]);
211
+ }
212
+ })(),
213
+ () => Object.keys(value).length,
214
+ );
215
+ }
216
+
217
+ function reduceIndexedValues(values, reducer, initial) {
218
+ let result = initial;
219
+ for (let index = 0; index < values.length; index += 1) {
220
+ result = reducer(result, index, values[index]);
221
+ if (isReducedValue(result)) return unreducedValue(result);
222
+ }
223
+ return result;
224
+ }
225
+
226
+ function reduceMapValues(values, reducer, initial) {
227
+ let result = initial;
228
+ for (const [key, value] of values) {
229
+ result = reducer(result, key, value);
230
+ if (isReducedValue(result)) return unreducedValue(result);
231
+ }
232
+ return result;
233
+ }
234
+
235
+ function reduceObjectValues(value, reducer, initial) {
236
+ let result = initial;
237
+ for (const key of Object.keys(value)) {
238
+ result = reducer(result, key, value[key]);
239
+ if (isReducedValue(result)) return unreducedValue(result);
240
+ }
241
+ return result;
242
+ }
243
+
244
+ extendProtocolCategory(I_COUNTED, "string", {
245
+ count: (value) => value.length,
246
+ });
247
+ extendProtocolCategory(I_EMPTYABLE, "string", { empty: () => "" });
248
+ extendProtocolCategory(I_LOOKUP, "string", {
249
+ get: (value, index, notFound = null) => indexedValue(value, index, notFound),
250
+ });
251
+ extendProtocolCategory(I_INDEXED, "string", { nth: indexedValue });
252
+ extendProtocolCategory(I_SEQABLE, "string", { seq: stringSequence });
253
+ extendProtocolCategory(I_REDUCE, "string", {
254
+ reduce: (value, reducer, ...initial) =>
255
+ reduceIterable(stringSequence(value) ?? [], reducer, ...initial),
256
+ });
257
+ extendProtocolCategory(I_REVERSIBLE, "string", {
258
+ rseq: indexedReverseSequence,
259
+ });
260
+
261
+ extendProtocolType(I_COUNTED, Object, {
262
+ count: (value) => Object.keys(value).length,
263
+ });
264
+ extendProtocolType(I_EMPTYABLE, Object, { empty: () => ({}) });
265
+ extendProtocolType(I_CONJ, Object, { conj: objectConj });
266
+ extendProtocolType(I_LOOKUP, Object, {
267
+ get: (value, key, notFound = null) =>
268
+ typeof key === "string" && Object.hasOwn(value, key)
269
+ ? value[key]
270
+ : notFound,
271
+ });
272
+ extendProtocolType(I_ASSOCIATIVE, Object, {
273
+ assoc: objectAssoc,
274
+ contains: (value, key) =>
275
+ typeof key === "string" && Object.hasOwn(value, key),
276
+ });
277
+ extendProtocolType(I_SEQABLE, Object, { seq: objectSequence });
278
+ extendProtocolType(I_REDUCE, Object, {
279
+ reduce: (value, reducer, ...initial) =>
280
+ reduceIterable(objectSequence(value) ?? [], reducer, ...initial),
281
+ });
282
+ extendProtocolType(I_KV_REDUCE, Object, { reduceKV: reduceObjectValues });
283
+ extendProtocolType(I_MAP, Object, { dissoc: objectDissoc });
284
+
285
+ extendProtocolType(I_COUNTED, Array, { count: (values) => values.length });
286
+ extendProtocolType(I_EMPTYABLE, Array, { empty: () => [] });
287
+ extendProtocolType(I_CONJ, Array, {
288
+ conj: (values, value) => [...values, value],
289
+ });
290
+ extendProtocolType(I_LOOKUP, Array, {
291
+ get: (values, index, notFound = null) => indexedValue(values, index, notFound),
292
+ });
293
+ extendProtocolType(I_ASSOCIATIVE, Array, {
294
+ assoc: indexedAssoc,
295
+ contains: indexedContains,
296
+ });
297
+ extendProtocolType(I_INDEXED, Array, { nth: indexedValue });
298
+ extendProtocolType(I_SEQABLE, Array, { seq: arraySequence });
299
+ extendProtocolType(I_REDUCE, Array, { reduce: reduceIterable });
300
+ extendProtocolType(I_KV_REDUCE, Array, { reduceKV: reduceIndexedValues });
301
+ extendProtocolType(I_STACK, Array, { peek: arrayPeek, pop: arrayPop });
302
+ extendProtocolType(I_REVERSIBLE, Array, { rseq: indexedReverseSequence });
303
+
304
+ extendProtocolType(I_COUNTED, Map, { count: (values) => values.size });
305
+ extendProtocolType(I_EMPTYABLE, Map, { empty: () => new Map() });
306
+ extendProtocolType(I_CONJ, Map, { conj: mapConj });
307
+ extendProtocolType(I_LOOKUP, Map, {
308
+ get: (values, key, notFound = null) =>
309
+ values.has(key) ? values.get(key) : notFound,
310
+ });
311
+ extendProtocolType(I_ASSOCIATIVE, Map, {
312
+ assoc: mapAssoc,
313
+ contains: (values, key) => values.has(key),
314
+ });
315
+ extendProtocolType(I_SEQABLE, Map, { seq: mapSequence });
316
+ extendProtocolType(I_REDUCE, Map, {
317
+ reduce: (values, reducer, ...initial) =>
318
+ reduceIterable(mapSequence(values) ?? [], reducer, ...initial),
319
+ });
320
+ extendProtocolType(I_KV_REDUCE, Map, { reduceKV: reduceMapValues });
321
+ extendProtocolType(I_MAP, Map, { dissoc: mapDissoc });
322
+
323
+ extendProtocolType(I_COUNTED, Set, { count: (values) => values.size });
324
+ extendProtocolType(I_EMPTYABLE, Set, { empty: () => new Set() });
325
+ extendProtocolType(I_CONJ, Set, { conj: setConj });
326
+ extendProtocolType(I_LOOKUP, Set, {
327
+ get: (values, key, notFound = null) => values.has(key) ? key : notFound,
328
+ });
329
+ extendProtocolType(I_ASSOCIATIVE, Set, {
330
+ contains: (values, key) => values.has(key),
331
+ });
332
+ extendProtocolType(I_SEQABLE, Set, { seq: setSequence });
333
+ extendProtocolType(I_REDUCE, Set, { reduce: reduceIterable });
334
+ extendProtocolType(I_SET, Set, { disj: setDisj });
335
+
336
+ extendProtocolCategory(I_COUNTED, "null", { count: () => 0 });
337
+ extendProtocolCategory(I_EMPTYABLE, "null", { empty: () => null });
338
+ extendProtocolCategory(I_SEQABLE, "null", { seq: () => null });
339
+ extendProtocolCategory(I_REDUCE, "null", {
340
+ reduce: (_value, reducer, ...initial) => reduceIterable([], reducer, ...initial),
341
+ });
342
+ extendProtocolCategory(I_KV_REDUCE, "null", {
343
+ reduceKV: (_value, _reducer, initial) => initial,
344
+ });
345
+ extendProtocolCategory(I_MAP, "null", { dissoc: () => null });
346
+ extendProtocolCategory(I_SET, "null", { disj: () => null });
347
+ extendProtocolCategory(I_STACK, "null", { peek: () => null, pop: () => null });
348
+ extendProtocolCategory(I_REVERSIBLE, "null", { rseq: () => null });
349
+
350
+ export const ICounted = I_COUNTED;
351
+ export const IEmptyable = I_EMPTYABLE;
352
+ export const IConj = I_CONJ;
353
+ export const ILookup = I_LOOKUP;
354
+ export const IAssociative = I_ASSOCIATIVE;
355
+ export const IIndexed = I_INDEXED;
356
+ export const ISeqable = I_SEQABLE;
357
+ export const IReduce = I_REDUCE;
358
+ export const IKVReduce = I_KV_REDUCE;
359
+ export const IMap = I_MAP;
360
+ export const ISet = I_SET;
361
+ export const IStack = I_STACK;
362
+ export const IReversible = I_REVERSIBLE;
363
+
364
+ export const ReductionView = InternalReductionView;
365
+ export const MemoizedSequenceView = InternalMemoizedSequenceView;
366
+ export const SequenceView = InternalSequenceView;
367
+
368
+ export function reductionView(reduceFunction) {
369
+ return createReductionView(reduceFunction);
370
+ }
371
+
372
+ export function memoizedSequenceView(factory, count = null) {
373
+ return createMemoizedSequenceView(factory, count);
374
+ }
375
+
376
+ export function sequenceView(factory, count = null) {
377
+ return createSequenceView(factory, count);
378
+ }
379
+
380
+ export function unboundedSequenceView(factory) {
381
+ return createUnboundedSequenceView(factory);
382
+ }
383
+
384
+ export function count(collection) {
385
+ return validateCollectionCount(dispatchCollectionCount(collection));
386
+ }
387
+
388
+ export function isCounted(collection) {
389
+ return implementsProtocol(ICounted, collection);
390
+ }
391
+
392
+ export function isIndexed(collection) {
393
+ return implementsProtocol(IIndexed, collection);
394
+ }
395
+
396
+ export function isSeqable(collection) {
397
+ return implementsProtocol(ISeqable, collection);
398
+ }
399
+
400
+ export function isReducible(collection) {
401
+ return implementsProtocol(IReduce, collection);
402
+ }
403
+
404
+ export function isReversible(collection) {
405
+ return implementsProtocol(IReversible, collection);
406
+ }
407
+
408
+ export function isAssociative(collection) {
409
+ return implementsProtocol(IAssociative, collection);
410
+ }
411
+
412
+ export function isCollection(value) {
413
+ return value instanceof InternalSequenceView ||
414
+ value instanceof InternalMemoizedSequenceView ||
415
+ isPersistentList(value) ||
416
+ isPersistentQueue(value) ||
417
+ isPersistentVector(value) ||
418
+ isPersistentMapValue(value) ||
419
+ isRecord(value) ||
420
+ isPersistentSetValue(value);
421
+ }
422
+
423
+ export function isList(value) {
424
+ return isPersistentList(value);
425
+ }
426
+
427
+ export function isVector(value) {
428
+ return isPersistentVector(value);
429
+ }
430
+
431
+ export function isQueue(value) {
432
+ return isPersistentQueue(value);
433
+ }
434
+
435
+ export function isMap(value) {
436
+ return isPersistentMapValue(value) || isRecord(value);
437
+ }
438
+
439
+ export function isSet(value) {
440
+ return isPersistentSetValue(value);
441
+ }
442
+
443
+ export function isSequential(value) {
444
+ return value instanceof InternalSequenceView ||
445
+ value instanceof InternalMemoizedSequenceView ||
446
+ isPersistentList(value) ||
447
+ isPersistentQueue(value) ||
448
+ isPersistentVector(value);
449
+ }
450
+
451
+ export function isSequence(value) {
452
+ return value instanceof InternalSequenceView ||
453
+ value instanceof InternalMemoizedSequenceView ||
454
+ isPersistentList(value);
455
+ }
456
+
457
+ export function boundedCount(limit, collection) {
458
+ if (!Number.isSafeInteger(limit) || limit < 0) {
459
+ throw new TypeError("boundedCount limit must be a non-negative safe integer");
460
+ }
461
+ if (limit === 0) return 0;
462
+
463
+ const sequence = seq(collection);
464
+ if (sequence === null) return 0;
465
+ const iterator = sequence[Symbol.iterator]();
466
+ let result = 0;
467
+ let complete = false;
468
+ try {
469
+ while (result < limit && !iterator.next().done) {
470
+ result += 1;
471
+ }
472
+ complete = true;
473
+ } finally {
474
+ if (
475
+ (!complete || result === limit) &&
476
+ typeof iterator.return === "function"
477
+ ) {
478
+ iterator.return();
479
+ }
480
+ }
481
+ return result;
482
+ }
483
+
484
+ export function areDistinct(...values) {
485
+ let seen = EMPTY_SET;
486
+ for (const value of values) {
487
+ if (seen.has(value)) return false;
488
+ seen = seen.conj(value);
489
+ }
490
+ return true;
491
+ }
492
+
493
+ export function empty(collection) {
494
+ return dispatchCollectionEmpty(collection);
495
+ }
496
+
497
+ export function conj(collection, ...values) {
498
+ let result = collection;
499
+ for (const value of values) {
500
+ result = dispatchCollectionConj(result, value);
501
+ }
502
+ return result;
503
+ }
504
+
505
+ export function get(collection, key, notFound = null) {
506
+ return dispatchCollectionGet(collection, key, notFound);
507
+ }
508
+
509
+ export function assoc(collection, key, value, ...keyValues) {
510
+ if (arguments.length < 3 || keyValues.length % 2 !== 0) {
511
+ throw new TypeError(
512
+ "assoc requires a collection followed by one or more key/value pairs",
513
+ );
514
+ }
515
+ let result = dispatchCollectionAssoc(collection, key, value);
516
+ for (let index = 0; index < keyValues.length; index += 2) {
517
+ result = dispatchCollectionAssoc(
518
+ result,
519
+ keyValues[index],
520
+ keyValues[index + 1],
521
+ );
522
+ }
523
+ return result;
524
+ }
525
+
526
+ export function contains(collection, key) {
527
+ const result = dispatchCollectionContains(collection, key);
528
+ if (typeof result !== "boolean") {
529
+ throw new TypeError("IAssociative/contains must return a boolean");
530
+ }
531
+ return result;
532
+ }
533
+
534
+ export function nth(collection, index, ...notFound) {
535
+ return notFound.length === 0
536
+ ? dispatchCollectionNth(collection, index)
537
+ : dispatchCollectionNth(collection, index, notFound[0]);
538
+ }
539
+
540
+ export function seq(collection) {
541
+ const result = dispatchCollectionSeq(collection);
542
+ if (result === null) {
543
+ return null;
544
+ }
545
+ if (result === undefined || typeof result[Symbol.iterator] !== "function") {
546
+ throw new TypeError("ISeqable/seq must return null or an iterable sequence view");
547
+ }
548
+ return result;
549
+ }
550
+
551
+ export function isEmpty(collection) {
552
+ return seq(collection) === null;
553
+ }
554
+
555
+ export function notEmpty(collection) {
556
+ return isEmpty(collection) ? null : collection;
557
+ }
558
+
559
+ export function reduce(collection, reducer, ...initial) {
560
+ if (typeof reducer !== "function") {
561
+ throw new TypeError("collection reducer must be a function");
562
+ }
563
+ return unreducedValue(
564
+ initial.length === 0
565
+ ? dispatchCollectionReduce(collection, reducer)
566
+ : dispatchCollectionReduce(collection, reducer, initial[0]),
567
+ );
568
+ }
569
+
570
+ export function reduceKV(collection, reducer, initial) {
571
+ if (arguments.length !== 3) {
572
+ throw new TypeError("reduceKV requires a collection, reducer, and initial value");
573
+ }
574
+ if (typeof reducer !== "function") {
575
+ throw new TypeError("key/value reducer must be a function");
576
+ }
577
+ return unreducedValue(dispatchCollectionReduceKV(collection, reducer, initial));
578
+ }
579
+
580
+ export function dissoc(collection, ...keys) {
581
+ let result = collection;
582
+ for (const key of keys) {
583
+ result = dispatchCollectionDissoc(result, key);
584
+ }
585
+ return result;
586
+ }
587
+
588
+ export function disj(collection, ...values) {
589
+ let result = collection;
590
+ for (const value of values) {
591
+ result = dispatchCollectionDisj(result, value);
592
+ }
593
+ return result;
594
+ }
595
+
596
+ export function peek(collection) {
597
+ if (arguments.length !== 1) {
598
+ throw new TypeError("peek requires exactly one collection");
599
+ }
600
+ return dispatchCollectionPeek(collection);
601
+ }
602
+
603
+ export function pop(collection) {
604
+ if (arguments.length !== 1) {
605
+ throw new TypeError("pop requires exactly one collection");
606
+ }
607
+ return dispatchCollectionPop(collection);
608
+ }
609
+
610
+ export function rseq(collection) {
611
+ if (arguments.length !== 1) {
612
+ throw new TypeError("rseq requires exactly one collection");
613
+ }
614
+ const result = dispatchCollectionRseq(collection);
615
+ if (result === null) {
616
+ return null;
617
+ }
618
+ if (result === undefined || typeof result[Symbol.iterator] !== "function") {
619
+ throw new TypeError(
620
+ "IReversible/rseq must return null or an iterable sequence view",
621
+ );
622
+ }
623
+ return result;
624
+ }
625
+
626
+ export function reduced(value) {
627
+ return reducedValue(value);
628
+ }
629
+
630
+ export function isReduced(value) {
631
+ return isReducedValue(value);
632
+ }
633
+
634
+ export function unreduced(value) {
635
+ return unreducedValue(value);
636
+ }
637
+
638
+ export function isSequenceView(value) {
639
+ return value instanceof InternalSequenceView ||
640
+ value instanceof InternalMemoizedSequenceView;
641
+ }
642
+
643
+ export function isReductionView(value) {
644
+ return value instanceof InternalReductionView;
645
+ }