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,632 @@
1
+ import {
2
+ count as collectionCount,
3
+ IReversible,
4
+ isReduced,
5
+ nth as collectionNth,
6
+ reduce,
7
+ reduced,
8
+ rseq,
9
+ seq,
10
+ sequenceView,
11
+ unboundedSequenceView,
12
+ unreduced,
13
+ } from "./collection.mjs";
14
+ import {
15
+ prependSequenceView,
16
+ sliceSequenceView,
17
+ } from "./collection-internals.mjs";
18
+ import { EMPTY_LIST, isPersistentList } from "./list.mjs";
19
+ import { implementsProtocolOperation } from "./protocol.mjs";
20
+ import {
21
+ deduping,
22
+ distincting,
23
+ dropping,
24
+ droppingWhile,
25
+ filtering,
26
+ into,
27
+ interposing,
28
+ keeping,
29
+ keepingIndexed,
30
+ mapcatting,
31
+ mapping,
32
+ mappingIndexed,
33
+ partitioningAll,
34
+ partitioningBy,
35
+ removing,
36
+ taking,
37
+ takingNth,
38
+ takingWhile,
39
+ } from "./transducer.mjs";
40
+ import {
41
+ conjBang,
42
+ persistentBang,
43
+ transient,
44
+ } from "./transient.mjs";
45
+ import { isTruthy } from "./truth.mjs";
46
+ import {
47
+ EMPTY_VECTOR,
48
+ isPersistentVector,
49
+ persistentVector,
50
+ } from "./vector.mjs";
51
+
52
+ const NOT_FOUND = Symbol("eliscript.sequence.not-found");
53
+ const NO_PARTITION_PAD = Symbol("eliscript.sequence.no-partition-pad");
54
+
55
+ function requireFunction(value, label) {
56
+ if (typeof value !== "function") {
57
+ throw new TypeError(`${label} must be a function`);
58
+ }
59
+ return value;
60
+ }
61
+
62
+ function requireNonNegativeSafeInteger(value, label) {
63
+ if (!Number.isSafeInteger(value) || value < 0) {
64
+ throw new TypeError(`${label} must be a non-negative safe integer`);
65
+ }
66
+ return value;
67
+ }
68
+
69
+ function requirePositiveSafeInteger(value, label) {
70
+ if (!Number.isSafeInteger(value) || value <= 0) {
71
+ throw new TypeError(`${label} must be a positive safe integer`);
72
+ }
73
+ return value;
74
+ }
75
+
76
+ function materializeSources(collections) {
77
+ const sources = [];
78
+ for (const collection of collections) {
79
+ sources.push(into(EMPTY_VECTOR, collection));
80
+ }
81
+ return sources;
82
+ }
83
+
84
+ function partitionWindows(
85
+ size,
86
+ step,
87
+ collection,
88
+ { includePartial = false, pad = NO_PARTITION_PAD } = {},
89
+ ) {
90
+ requirePositiveSafeInteger(size, "partition size");
91
+ requirePositiveSafeInteger(step, "partition step");
92
+ const values = into(EMPTY_VECTOR, collection);
93
+ const valueCount = collectionCount(values);
94
+ const result = transient(EMPTY_VECTOR);
95
+
96
+ for (let start = 0; start < valueCount; start += step) {
97
+ const group = transient(EMPTY_VECTOR);
98
+ let groupCount = Math.min(size, valueCount - start);
99
+ for (let offset = 0; offset < groupCount; offset += 1) {
100
+ conjBang(group, collectionNth(values, start + offset));
101
+ }
102
+
103
+ const partial = groupCount < size;
104
+ if (partial && pad !== NO_PARTITION_PAD) {
105
+ reduce(pad, (builder, value) => {
106
+ conjBang(builder, value);
107
+ groupCount += 1;
108
+ return groupCount >= size ? reduced(builder) : builder;
109
+ }, group);
110
+ }
111
+ const completed = persistentBang(group);
112
+ if (!partial || includePartial || pad !== NO_PARTITION_PAD) {
113
+ conjBang(result, completed);
114
+ }
115
+ if (partial && !includePartial) break;
116
+ }
117
+ return persistentBang(result);
118
+ }
119
+
120
+ function isSequentialBranch(value) {
121
+ return Array.isArray(value) ||
122
+ isPersistentList(value) ||
123
+ isPersistentVector(value);
124
+ }
125
+
126
+ function requireFiniteNumber(value, label) {
127
+ if (!Number.isFinite(value)) {
128
+ throw new TypeError(`${label} must be a finite number`);
129
+ }
130
+ return value;
131
+ }
132
+
133
+ function rangeCount(start, end, step) {
134
+ const distance = (end - start) / step;
135
+ if (!(distance > 0)) return 0;
136
+ return requireNonNegativeSafeInteger(Math.ceil(distance), "range count");
137
+ }
138
+
139
+ function rangeFactory(start, step, count = null) {
140
+ return () => (function* rangeValues() {
141
+ let index = 0;
142
+ while (count === null || index < count) {
143
+ yield start + index * step;
144
+ index += 1;
145
+ }
146
+ })();
147
+ }
148
+
149
+ export function range(...bounds) {
150
+ if (bounds.length === 0) {
151
+ return unboundedSequenceView(rangeFactory(0, 1));
152
+ }
153
+ if (bounds.length > 3) {
154
+ throw new TypeError("range expects zero to three arguments");
155
+ }
156
+
157
+ const start = requireFiniteNumber(
158
+ bounds.length === 1 ? 0 : bounds[0],
159
+ "range start",
160
+ );
161
+ const end = requireFiniteNumber(
162
+ bounds.length === 1 ? bounds[0] : bounds[1],
163
+ "range end",
164
+ );
165
+ const step = requireFiniteNumber(bounds.length === 3 ? bounds[2] : 1, "range step");
166
+ if (step === 0) {
167
+ throw new TypeError("range step must be non-zero");
168
+ }
169
+ const count = rangeCount(start, end, step);
170
+ return sequenceView(rangeFactory(start, step, count), count);
171
+ }
172
+
173
+ function repeatFactory(value, count = null) {
174
+ return () => (function* repeatedValues() {
175
+ let index = 0;
176
+ while (count === null || index < count) {
177
+ yield value;
178
+ index += 1;
179
+ }
180
+ })();
181
+ }
182
+
183
+ export function repeat(...arguments_) {
184
+ if (arguments_.length === 1) {
185
+ return unboundedSequenceView(repeatFactory(arguments_[0]));
186
+ }
187
+ if (arguments_.length === 2) {
188
+ const count = requireNonNegativeSafeInteger(arguments_[0], "repeat count");
189
+ return sequenceView(repeatFactory(arguments_[1], count), count);
190
+ }
191
+ throw new TypeError("repeat expects a value or a count and value");
192
+ }
193
+
194
+ function repeatedlyFactory(producer, count = null) {
195
+ return () => (function* producedValues() {
196
+ let index = 0;
197
+ while (count === null || index < count) {
198
+ yield producer();
199
+ index += 1;
200
+ }
201
+ })();
202
+ }
203
+
204
+ export function repeatedly(...arguments_) {
205
+ if (arguments_.length === 1) {
206
+ const producer = requireFunction(arguments_[0], "repeatedly producer");
207
+ return unboundedSequenceView(repeatedlyFactory(producer));
208
+ }
209
+ if (arguments_.length === 2) {
210
+ const count = requireNonNegativeSafeInteger(arguments_[0], "repeatedly count");
211
+ const producer = requireFunction(arguments_[1], "repeatedly producer");
212
+ return sequenceView(repeatedlyFactory(producer, count), count);
213
+ }
214
+ throw new TypeError("repeatedly expects a producer or a count and producer");
215
+ }
216
+
217
+ export function iterate(transform, seed) {
218
+ requireFunction(transform, "iterate transform");
219
+ return unboundedSequenceView(() => (function* iterationValues() {
220
+ let value = seed;
221
+ while (true) {
222
+ yield value;
223
+ value = transform(value);
224
+ }
225
+ })());
226
+ }
227
+
228
+ export function cycle(collection) {
229
+ const values = into(EMPTY_VECTOR, collection);
230
+ const count = collectionCount(values);
231
+ if (count === 0) return null;
232
+ return unboundedSequenceView(() => (function* cycledValues() {
233
+ let index = 0;
234
+ while (true) {
235
+ yield collectionNth(values, index);
236
+ index = (index + 1) % count;
237
+ }
238
+ })());
239
+ }
240
+
241
+ export function generate(count, producer) {
242
+ requireNonNegativeSafeInteger(count, "generate count");
243
+ requireFunction(producer, "generate producer");
244
+ return sequenceView(() => (function* generatedValues() {
245
+ for (let index = 0; index < count; index += 1) {
246
+ yield producer(index);
247
+ }
248
+ })(), count);
249
+ }
250
+
251
+ export function first(collection, notFound = null) {
252
+ const result = reduce(collection, (missing, value) =>
253
+ reduced({ value }), NOT_FOUND);
254
+ return result === NOT_FOUND ? notFound : result.value;
255
+ }
256
+
257
+ export function nthRest(limit, collection) {
258
+ requireNonNegativeSafeInteger(limit, "nthRest limit");
259
+ if (limit === 0) return collection;
260
+ return sliceSequenceView(seq(collection), limit) ?? EMPTY_LIST;
261
+ }
262
+
263
+ export function nthNext(limit, collection) {
264
+ requireNonNegativeSafeInteger(limit, "nthNext limit");
265
+ const result = limit === 0
266
+ ? seq(collection)
267
+ : sliceSequenceView(seq(collection), limit);
268
+ return result === null ? null : seq(result);
269
+ }
270
+
271
+ export function rest(collection) {
272
+ return nthRest(1, collection);
273
+ }
274
+
275
+ export function next(collection) {
276
+ return nthNext(1, collection);
277
+ }
278
+
279
+ export function prepend(value, collection) {
280
+ return prependSequenceView(value, seq(collection));
281
+ }
282
+
283
+ export function second(collection, notFound = null) {
284
+ return first(next(collection), notFound);
285
+ }
286
+
287
+ export function ffirst(collection) {
288
+ return first(first(collection));
289
+ }
290
+
291
+ export function nfirst(collection) {
292
+ return next(first(collection));
293
+ }
294
+
295
+ export function fnext(collection) {
296
+ return first(next(collection));
297
+ }
298
+
299
+ export function nnext(collection) {
300
+ return next(next(collection));
301
+ }
302
+
303
+ export function sequenceNth(index, collection, notFound = null) {
304
+ requireNonNegativeSafeInteger(index, "sequenceNth index");
305
+ let position = 0;
306
+ const result = reduce(collection, (missing, value) => {
307
+ if (position === index) return reduced({ value });
308
+ position += 1;
309
+ return missing;
310
+ }, NOT_FOUND);
311
+ return result === NOT_FOUND ? notFound : result.value;
312
+ }
313
+
314
+ export function last(collection, notFound = null) {
315
+ if (implementsProtocolOperation(IReversible, "rseq", collection)) {
316
+ return first(rseq(collection), notFound);
317
+ }
318
+ const result = reduce(collection, (missing, value) => ({ value }), NOT_FOUND);
319
+ return result === NOT_FOUND ? notFound : result.value;
320
+ }
321
+
322
+ export function takeLast(limit, collection) {
323
+ requireNonNegativeSafeInteger(limit, "takeLast limit");
324
+ if (limit === 0) return EMPTY_VECTOR;
325
+
326
+ const ring = new Array(limit);
327
+ let count = 0;
328
+ let start = 0;
329
+ reduce(collection, (state, value) => {
330
+ if (count < limit) {
331
+ ring[count] = value;
332
+ count += 1;
333
+ } else {
334
+ ring[start] = value;
335
+ start = (start + 1) % limit;
336
+ }
337
+ return state;
338
+ }, null);
339
+
340
+ const builder = transient(EMPTY_VECTOR);
341
+ for (let index = 0; index < count; index += 1) {
342
+ conjBang(builder, ring[(start + index) % limit]);
343
+ }
344
+ return persistentBang(builder);
345
+ }
346
+
347
+ export function dropLast(limit, collection) {
348
+ requireNonNegativeSafeInteger(limit, "dropLast limit");
349
+ if (limit === 0) return into(EMPTY_VECTOR, collection);
350
+
351
+ const ring = new Array(limit);
352
+ const builder = transient(EMPTY_VECTOR);
353
+ let count = 0;
354
+ let start = 0;
355
+ reduce(collection, (state, value) => {
356
+ if (count < limit) {
357
+ ring[count] = value;
358
+ count += 1;
359
+ } else {
360
+ conjBang(builder, ring[start]);
361
+ ring[start] = value;
362
+ start = (start + 1) % limit;
363
+ }
364
+ return state;
365
+ }, null);
366
+ return persistentBang(builder);
367
+ }
368
+
369
+ export function butlast(collection) {
370
+ return dropLast(1, collection);
371
+ }
372
+
373
+ export function splitAt(limit, collection) {
374
+ requireNonNegativeSafeInteger(limit, "splitAt limit");
375
+ const left = transient(EMPTY_VECTOR);
376
+ const right = transient(EMPTY_VECTOR);
377
+ let index = 0;
378
+ reduce(collection, (state, value) => {
379
+ conjBang(index < limit ? left : right, value);
380
+ index += 1;
381
+ return state;
382
+ }, null);
383
+ return persistentVector(persistentBang(left), persistentBang(right));
384
+ }
385
+
386
+ export function splitWith(predicate, collection) {
387
+ requireFunction(predicate, "splitWith predicate");
388
+ const left = transient(EMPTY_VECTOR);
389
+ const right = transient(EMPTY_VECTOR);
390
+ let prefix = true;
391
+ reduce(collection, (state, value) => {
392
+ if (prefix && isTruthy(predicate(value))) {
393
+ conjBang(left, value);
394
+ } else {
395
+ prefix = false;
396
+ conjBang(right, value);
397
+ }
398
+ return state;
399
+ }, null);
400
+ return persistentVector(persistentBang(left), persistentBang(right));
401
+ }
402
+
403
+ export function reverse(collection) {
404
+ if (implementsProtocolOperation(IReversible, "rseq", collection)) {
405
+ return into(EMPTY_VECTOR, rseq(collection));
406
+ }
407
+ const values = reduce(collection, (result, value) => {
408
+ result.push(value);
409
+ return result;
410
+ }, []);
411
+ values.reverse();
412
+ return into(EMPTY_VECTOR, values);
413
+ }
414
+
415
+ export function map(transform, collection) {
416
+ return into(EMPTY_VECTOR, mapping(transform), collection);
417
+ }
418
+
419
+ export function mapIndexed(transform, collection) {
420
+ return into(EMPTY_VECTOR, mappingIndexed(transform), collection);
421
+ }
422
+
423
+ export function keep(transform, collection) {
424
+ return into(EMPTY_VECTOR, keeping(transform), collection);
425
+ }
426
+
427
+ export function keepIndexed(transform, collection) {
428
+ return into(EMPTY_VECTOR, keepingIndexed(transform), collection);
429
+ }
430
+
431
+ export function filter(predicate, collection) {
432
+ return into(EMPTY_VECTOR, filtering(predicate), collection);
433
+ }
434
+
435
+ export function remove(predicate, collection) {
436
+ return into(EMPTY_VECTOR, removing(predicate), collection);
437
+ }
438
+
439
+ export function take(limit, collection) {
440
+ return into(EMPTY_VECTOR, taking(limit), collection);
441
+ }
442
+
443
+ export function drop(limit, collection) {
444
+ return into(EMPTY_VECTOR, dropping(limit), collection);
445
+ }
446
+
447
+ export function takeWhile(predicate, collection) {
448
+ return into(EMPTY_VECTOR, takingWhile(predicate), collection);
449
+ }
450
+
451
+ export function dropWhile(predicate, collection) {
452
+ return into(EMPTY_VECTOR, droppingWhile(predicate), collection);
453
+ }
454
+
455
+ export function takeNth(interval, collection) {
456
+ return into(EMPTY_VECTOR, takingNth(interval), collection);
457
+ }
458
+
459
+ export function interpose(separator, collection) {
460
+ return into(EMPTY_VECTOR, interposing(separator), collection);
461
+ }
462
+
463
+ export function dedupe(collection) {
464
+ return into(EMPTY_VECTOR, deduping(), collection);
465
+ }
466
+
467
+ export function distinct(collection) {
468
+ return into(EMPTY_VECTOR, distincting(), collection);
469
+ }
470
+
471
+ export function mapcat(transform, collection) {
472
+ return into(EMPTY_VECTOR, mapcatting(transform), collection);
473
+ }
474
+
475
+ export function partition(size, ...arguments_) {
476
+ if (arguments_.length === 1) {
477
+ return partitionWindows(size, size, arguments_[0]);
478
+ }
479
+ if (arguments_.length === 2) {
480
+ return partitionWindows(size, arguments_[0], arguments_[1]);
481
+ }
482
+ if (arguments_.length === 3) {
483
+ return partitionWindows(size, arguments_[0], arguments_[2], {
484
+ pad: arguments_[1],
485
+ });
486
+ }
487
+ throw new TypeError(
488
+ "partition expects size and collection, optional step, or step and pad",
489
+ );
490
+ }
491
+
492
+ export function partitionAll(size, ...arguments_) {
493
+ if (arguments_.length === 1) {
494
+ requirePositiveSafeInteger(size, "partition size");
495
+ return into(EMPTY_VECTOR, partitioningAll(size), arguments_[0]);
496
+ }
497
+ if (arguments_.length === 2) {
498
+ return partitionWindows(size, arguments_[0], arguments_[1], {
499
+ includePartial: true,
500
+ });
501
+ }
502
+ throw new TypeError(
503
+ "partitionAll expects size and collection or size, step, and collection",
504
+ );
505
+ }
506
+
507
+ export function partitionBy(classifier, collection) {
508
+ return into(EMPTY_VECTOR, partitioningBy(classifier), collection);
509
+ }
510
+
511
+ export function concat(...collections) {
512
+ const result = transient(EMPTY_VECTOR);
513
+ for (const collection of collections) {
514
+ reduce(collection, (builder, value) => conjBang(builder, value), result);
515
+ }
516
+ return persistentBang(result);
517
+ }
518
+
519
+ export function interleave(...collections) {
520
+ if (collections.length === 0) return EMPTY_VECTOR;
521
+ const sources = materializeSources(collections);
522
+ let limit = collectionCount(sources[0]);
523
+ for (let index = 1; index < sources.length; index += 1) {
524
+ limit = Math.min(limit, collectionCount(sources[index]));
525
+ }
526
+ const result = transient(EMPTY_VECTOR);
527
+ for (let valueIndex = 0; valueIndex < limit; valueIndex += 1) {
528
+ for (const source of sources) {
529
+ conjBang(result, collectionNth(source, valueIndex));
530
+ }
531
+ }
532
+ return persistentBang(result);
533
+ }
534
+
535
+ export function interleaveAll(...collections) {
536
+ if (collections.length === 0) return EMPTY_VECTOR;
537
+ const sources = materializeSources(collections);
538
+ let limit = 0;
539
+ for (const source of sources) {
540
+ limit = Math.max(limit, collectionCount(source));
541
+ }
542
+ const result = transient(EMPTY_VECTOR);
543
+ for (let valueIndex = 0; valueIndex < limit; valueIndex += 1) {
544
+ for (const source of sources) {
545
+ if (valueIndex < collectionCount(source)) {
546
+ conjBang(result, collectionNth(source, valueIndex));
547
+ }
548
+ }
549
+ }
550
+ return persistentBang(result);
551
+ }
552
+
553
+ export function some(predicate, collection, notFound = null) {
554
+ requireFunction(predicate, "some predicate");
555
+ const result = reduce(collection, (missing, value) => {
556
+ const matched = predicate(value);
557
+ return isTruthy(matched) ? reduced(matched) : missing;
558
+ }, NOT_FOUND);
559
+ return result === NOT_FOUND ? notFound : result;
560
+ }
561
+
562
+ export function every(predicate, collection) {
563
+ requireFunction(predicate, "every predicate");
564
+ return reduce(collection, (result, value) =>
565
+ isTruthy(predicate(value)) ? result : reduced(false), true);
566
+ }
567
+
568
+ export function notAny(predicate, collection) {
569
+ return !isTruthy(some(predicate, collection));
570
+ }
571
+
572
+ export function notEvery(predicate, collection) {
573
+ return !every(predicate, collection);
574
+ }
575
+
576
+ export function find(predicate, collection, notFound = null) {
577
+ requireFunction(predicate, "find predicate");
578
+ const result = reduce(collection, (missing, value) =>
579
+ isTruthy(predicate(value)) ? reduced({ value }) : missing, NOT_FOUND);
580
+ return result === NOT_FOUND ? notFound : result.value;
581
+ }
582
+
583
+ export function reductions(step, initial, collection) {
584
+ requireFunction(step, "reductions step");
585
+ const builder = transient(EMPTY_VECTOR);
586
+ const seed = unreduced(initial);
587
+ conjBang(builder, seed);
588
+ if (!isReduced(initial)) {
589
+ reduce(collection, (current, input) => {
590
+ const next = step(current, input);
591
+ const value = unreduced(next);
592
+ conjBang(builder, value);
593
+ return isReduced(next) ? reduced(value) : value;
594
+ }, seed);
595
+ }
596
+ return persistentBang(builder);
597
+ }
598
+
599
+ export function treeSeq(branchPredicate, childrenFunction, root) {
600
+ requireFunction(branchPredicate, "treeSeq branch predicate");
601
+ requireFunction(childrenFunction, "treeSeq children function");
602
+ const result = transient(EMPTY_VECTOR);
603
+ const stack = [root];
604
+ while (stack.length > 0) {
605
+ const value = stack.pop();
606
+ conjBang(result, value);
607
+ if (isTruthy(branchPredicate(value))) {
608
+ const children = into(EMPTY_VECTOR, childrenFunction(value));
609
+ for (let index = collectionCount(children) - 1; index >= 0; index -= 1) {
610
+ stack.push(collectionNth(children, index));
611
+ }
612
+ }
613
+ }
614
+ return persistentBang(result);
615
+ }
616
+
617
+ export function flatten(root) {
618
+ const result = transient(EMPTY_VECTOR);
619
+ const stack = [root];
620
+ while (stack.length > 0) {
621
+ const value = stack.pop();
622
+ if (isSequentialBranch(value)) {
623
+ const children = into(EMPTY_VECTOR, value);
624
+ for (let index = collectionCount(children) - 1; index >= 0; index -= 1) {
625
+ stack.push(collectionNth(children, index));
626
+ }
627
+ } else {
628
+ conjBang(result, value);
629
+ }
630
+ }
631
+ return persistentBang(result);
632
+ }