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,15 @@
1
+ export {
2
+ blank_QMARK_ as blank,
3
+ contains_QMARK_ as contains,
4
+ empty_QMARK_ as empty,
5
+ ends_with_QMARK_ as endsWith,
6
+ join,
7
+ repeat,
8
+ slice,
9
+ starts_at_QMARK_ as startsAt,
10
+ starts_with_QMARK_ as startsWith,
11
+ strip_prefix as stripPrefix,
12
+ strip_suffix as stripSuffix,
13
+ trim,
14
+ whitespace_QMARK_ as whitespace,
15
+ } from "./text-impl.mjs";
@@ -0,0 +1,668 @@
1
+ import {
2
+ conj,
3
+ contains,
4
+ empty,
5
+ isReduced,
6
+ memoizedSequenceView,
7
+ reduce,
8
+ reduced,
9
+ reductionView,
10
+ seq,
11
+ unreduced,
12
+ } from "./collection.mjs";
13
+ import { implementsProtocolOperation } from "./protocol.mjs";
14
+ import { EMPTY_SET } from "./set.mjs";
15
+ import {
16
+ IEditable,
17
+ conjBang,
18
+ persistentBang,
19
+ transient,
20
+ } from "./transient.mjs";
21
+ import { isTruthy } from "./truth.mjs";
22
+ import { equalValues } from "./value.mjs";
23
+ import { EMPTY_VECTOR } from "./vector.mjs";
24
+
25
+ const REDUCING_FUNCTION = Symbol("eliscript.transducer.reducing-function");
26
+ const ZERO_INPUT = Symbol("eliscript.transducer.zero-input");
27
+ const NO_PREVIOUS_VALUE = Symbol("eliscript.transducer.no-previous-value");
28
+ const NO_PARTITION_KEY = Symbol("eliscript.transducer.no-partition-key");
29
+ const NO_REDUCTION_INITIAL = Symbol("eliscript.transducer.no-reduction-initial");
30
+
31
+ function identity(value) {
32
+ return value;
33
+ }
34
+
35
+ function requireFunction(value, label) {
36
+ if (typeof value !== "function") {
37
+ throw new TypeError(`${label} must be a function`);
38
+ }
39
+ return value;
40
+ }
41
+
42
+ function requirePositiveSafeInteger(value, label) {
43
+ if (!Number.isSafeInteger(value) || value <= 0) {
44
+ throw new TypeError(`${label} must be a positive safe integer`);
45
+ }
46
+ return value;
47
+ }
48
+
49
+ function asReducingFunction(value, label) {
50
+ requireFunction(value, label);
51
+ return value[REDUCING_FUNCTION] === true ? value : completing(value);
52
+ }
53
+
54
+ function makeTransducer(transform, zeroInput = false) {
55
+ Object.defineProperty(transform, ZERO_INPUT, { value: zeroInput });
56
+ return Object.freeze(transform);
57
+ }
58
+
59
+ function applyTransducer(transducer, reducingFunction) {
60
+ requireFunction(transducer, "transducer");
61
+ return asReducingFunction(
62
+ transducer(reducingFunction),
63
+ "transducer result",
64
+ );
65
+ }
66
+
67
+ function createPartitionBuffer() {
68
+ let builder = transient(EMPTY_VECTOR);
69
+ let length = 0;
70
+ return Object.freeze({
71
+ append(value) {
72
+ builder = conjBang(builder, value);
73
+ length += 1;
74
+ return length;
75
+ },
76
+ flush(result, downstream) {
77
+ if (length === 0) return result;
78
+ const partition = persistentBang(builder);
79
+ builder = transient(EMPTY_VECTOR);
80
+ length = 0;
81
+ return downstream(result, partition);
82
+ },
83
+ });
84
+ }
85
+
86
+ const IDENTITY_TRANSDUCER = makeTransducer((reducingFunction) =>
87
+ reducingFunction);
88
+
89
+ export function completing(step, complete = identity) {
90
+ requireFunction(step, "reducing step");
91
+ requireFunction(complete, "reducing completion");
92
+
93
+ const reducingFunction = (...arguments_) => {
94
+ if (arguments_.length === 1) {
95
+ return complete(arguments_[0]);
96
+ }
97
+ if (arguments_.length === 2) {
98
+ return step(arguments_[0], arguments_[1]);
99
+ }
100
+ throw new TypeError(
101
+ "reducing functions accept one completion argument or two step arguments",
102
+ );
103
+ };
104
+ Object.defineProperty(reducingFunction, REDUCING_FUNCTION, { value: true });
105
+ return Object.freeze(reducingFunction);
106
+ }
107
+
108
+ export function mapping(transform) {
109
+ requireFunction(transform, "mapping transform");
110
+ return makeTransducer((reducingFunction) => {
111
+ const downstream = asReducingFunction(
112
+ reducingFunction,
113
+ "mapping reducing function",
114
+ );
115
+ return completing(
116
+ (result, input) => downstream(result, transform(input)),
117
+ (result) => downstream(result),
118
+ );
119
+ });
120
+ }
121
+
122
+ export function mappingIndexed(transform) {
123
+ requireFunction(transform, "mapping-indexed transform");
124
+ return makeTransducer((reducingFunction) => {
125
+ const downstream = asReducingFunction(
126
+ reducingFunction,
127
+ "mapping-indexed reducing function",
128
+ );
129
+ let index = 0;
130
+ return completing(
131
+ (result, input) => {
132
+ const currentIndex = index;
133
+ index += 1;
134
+ return downstream(result, transform(currentIndex, input));
135
+ },
136
+ (result) => downstream(result),
137
+ );
138
+ });
139
+ }
140
+
141
+ export function keeping(transform) {
142
+ requireFunction(transform, "keeping transform");
143
+ return makeTransducer((reducingFunction) => {
144
+ const downstream = asReducingFunction(
145
+ reducingFunction,
146
+ "keeping reducing function",
147
+ );
148
+ return completing(
149
+ (result, input) => {
150
+ const value = transform(input);
151
+ return value === null ? result : downstream(result, value);
152
+ },
153
+ (result) => downstream(result),
154
+ );
155
+ });
156
+ }
157
+
158
+ export function keepingIndexed(transform) {
159
+ requireFunction(transform, "keeping-indexed transform");
160
+ return makeTransducer((reducingFunction) => {
161
+ const downstream = asReducingFunction(
162
+ reducingFunction,
163
+ "keeping-indexed reducing function",
164
+ );
165
+ let index = 0;
166
+ return completing(
167
+ (result, input) => {
168
+ const currentIndex = index;
169
+ index += 1;
170
+ const value = transform(currentIndex, input);
171
+ return value === null ? result : downstream(result, value);
172
+ },
173
+ (result) => downstream(result),
174
+ );
175
+ });
176
+ }
177
+
178
+ export function catting() {
179
+ return makeTransducer((reducingFunction) => {
180
+ const downstream = asReducingFunction(
181
+ reducingFunction,
182
+ "catting reducing function",
183
+ );
184
+ return completing(
185
+ (result, input) => {
186
+ let stopped = false;
187
+ const flattened = reduce(
188
+ input,
189
+ (nestedResult, value) => {
190
+ const stepped = downstream(nestedResult, value);
191
+ if (isReduced(stepped)) stopped = true;
192
+ return stepped;
193
+ },
194
+ result,
195
+ );
196
+ return stopped ? reduced(flattened) : flattened;
197
+ },
198
+ (result) => downstream(result),
199
+ );
200
+ });
201
+ }
202
+
203
+ export function mapcatting(transform) {
204
+ requireFunction(transform, "mapcatting transform");
205
+ return composeTransducers(mapping(transform), catting());
206
+ }
207
+
208
+ export function filtering(predicate) {
209
+ requireFunction(predicate, "filtering predicate");
210
+ return makeTransducer((reducingFunction) => {
211
+ const downstream = asReducingFunction(
212
+ reducingFunction,
213
+ "filtering reducing function",
214
+ );
215
+ return completing(
216
+ (result, input) => isTruthy(predicate(input))
217
+ ? downstream(result, input)
218
+ : result,
219
+ (result) => downstream(result),
220
+ );
221
+ });
222
+ }
223
+
224
+ export function removing(predicate) {
225
+ requireFunction(predicate, "removing predicate");
226
+ return makeTransducer((reducingFunction) => {
227
+ const downstream = asReducingFunction(
228
+ reducingFunction,
229
+ "removing reducing function",
230
+ );
231
+ return completing(
232
+ (result, input) => isTruthy(predicate(input))
233
+ ? result
234
+ : downstream(result, input),
235
+ (result) => downstream(result),
236
+ );
237
+ });
238
+ }
239
+
240
+ export function taking(limit) {
241
+ if (!Number.isSafeInteger(limit) || limit < 0) {
242
+ throw new TypeError("taking limit must be a non-negative safe integer");
243
+ }
244
+ return makeTransducer((reducingFunction) => {
245
+ const downstream = asReducingFunction(
246
+ reducingFunction,
247
+ "taking reducing function",
248
+ );
249
+ let remaining = limit;
250
+ return completing(
251
+ (result, input) => {
252
+ if (remaining === 0) {
253
+ return reduced(result);
254
+ }
255
+ remaining -= 1;
256
+ const stepped = downstream(result, input);
257
+ return remaining === 0 && !isReduced(stepped)
258
+ ? reduced(stepped)
259
+ : stepped;
260
+ },
261
+ (result) => downstream(result),
262
+ );
263
+ }, limit === 0);
264
+ }
265
+
266
+ export function dropping(limit) {
267
+ if (!Number.isSafeInteger(limit) || limit < 0) {
268
+ throw new TypeError("dropping limit must be a non-negative safe integer");
269
+ }
270
+ return makeTransducer((reducingFunction) => {
271
+ const downstream = asReducingFunction(
272
+ reducingFunction,
273
+ "dropping reducing function",
274
+ );
275
+ let remaining = limit;
276
+ return completing(
277
+ (result, input) => {
278
+ if (remaining > 0) {
279
+ remaining -= 1;
280
+ return result;
281
+ }
282
+ return downstream(result, input);
283
+ },
284
+ (result) => downstream(result),
285
+ );
286
+ });
287
+ }
288
+
289
+ export function takingWhile(predicate) {
290
+ requireFunction(predicate, "taking-while predicate");
291
+ return makeTransducer((reducingFunction) => {
292
+ const downstream = asReducingFunction(
293
+ reducingFunction,
294
+ "taking-while reducing function",
295
+ );
296
+ return completing(
297
+ (result, input) => isTruthy(predicate(input))
298
+ ? downstream(result, input)
299
+ : reduced(result),
300
+ (result) => downstream(result),
301
+ );
302
+ });
303
+ }
304
+
305
+ export function droppingWhile(predicate) {
306
+ requireFunction(predicate, "dropping-while predicate");
307
+ return makeTransducer((reducingFunction) => {
308
+ const downstream = asReducingFunction(
309
+ reducingFunction,
310
+ "dropping-while reducing function",
311
+ );
312
+ let dropping = true;
313
+ return completing(
314
+ (result, input) => {
315
+ if (dropping && isTruthy(predicate(input))) return result;
316
+ dropping = false;
317
+ return downstream(result, input);
318
+ },
319
+ (result) => downstream(result),
320
+ );
321
+ });
322
+ }
323
+
324
+ export function takingNth(interval) {
325
+ requirePositiveSafeInteger(interval, "taking-nth interval");
326
+ return makeTransducer((reducingFunction) => {
327
+ const downstream = asReducingFunction(
328
+ reducingFunction,
329
+ "taking-nth reducing function",
330
+ );
331
+ let remaining = 0;
332
+ return completing(
333
+ (result, input) => {
334
+ if (remaining > 0) {
335
+ remaining -= 1;
336
+ return result;
337
+ }
338
+ remaining = interval - 1;
339
+ return downstream(result, input);
340
+ },
341
+ (result) => downstream(result),
342
+ );
343
+ });
344
+ }
345
+
346
+ export function interposing(separator) {
347
+ return makeTransducer((reducingFunction) => {
348
+ const downstream = asReducingFunction(
349
+ reducingFunction,
350
+ "interposing reducing function",
351
+ );
352
+ let started = false;
353
+ return completing(
354
+ (result, input) => {
355
+ if (!started) {
356
+ started = true;
357
+ return downstream(result, input);
358
+ }
359
+ const separated = downstream(result, separator);
360
+ return isReduced(separated)
361
+ ? separated
362
+ : downstream(separated, input);
363
+ },
364
+ (result) => downstream(result),
365
+ );
366
+ });
367
+ }
368
+
369
+ export function partitioningAll(size) {
370
+ requirePositiveSafeInteger(size, "partitioning-all size");
371
+ return makeTransducer((reducingFunction) => {
372
+ const downstream = asReducingFunction(
373
+ reducingFunction,
374
+ "partitioning-all reducing function",
375
+ );
376
+ const buffer = createPartitionBuffer();
377
+ return completing(
378
+ (result, input) => buffer.append(input) === size
379
+ ? buffer.flush(result, downstream)
380
+ : result,
381
+ (result) => downstream(unreduced(buffer.flush(result, downstream))),
382
+ );
383
+ });
384
+ }
385
+
386
+ export function partitioningBy(classifier) {
387
+ requireFunction(classifier, "partitioning-by classifier");
388
+ return makeTransducer((reducingFunction) => {
389
+ const downstream = asReducingFunction(
390
+ reducingFunction,
391
+ "partitioning-by reducing function",
392
+ );
393
+ const buffer = createPartitionBuffer();
394
+ let previousKey = NO_PARTITION_KEY;
395
+ return completing(
396
+ (result, input) => {
397
+ const key = classifier(input);
398
+ if (previousKey === NO_PARTITION_KEY || equalValues(previousKey, key)) {
399
+ previousKey = key;
400
+ buffer.append(input);
401
+ return result;
402
+ }
403
+ const stepped = buffer.flush(result, downstream);
404
+ if (isReduced(stepped)) return stepped;
405
+ previousKey = key;
406
+ buffer.append(input);
407
+ return stepped;
408
+ },
409
+ (result) => downstream(unreduced(buffer.flush(result, downstream))),
410
+ );
411
+ });
412
+ }
413
+
414
+ export function deduping() {
415
+ return makeTransducer((reducingFunction) => {
416
+ const downstream = asReducingFunction(
417
+ reducingFunction,
418
+ "deduping reducing function",
419
+ );
420
+ let previous = NO_PREVIOUS_VALUE;
421
+ return completing(
422
+ (result, input) => {
423
+ if (previous !== NO_PREVIOUS_VALUE && equalValues(previous, input)) {
424
+ return result;
425
+ }
426
+ previous = input;
427
+ return downstream(result, input);
428
+ },
429
+ (result) => downstream(result),
430
+ );
431
+ });
432
+ }
433
+
434
+ export function distincting() {
435
+ return makeTransducer((reducingFunction) => {
436
+ const downstream = asReducingFunction(
437
+ reducingFunction,
438
+ "distincting reducing function",
439
+ );
440
+ let seen = EMPTY_SET;
441
+ return completing(
442
+ (result, input) => {
443
+ if (contains(seen, input)) return result;
444
+ seen = conj(seen, input);
445
+ return downstream(result, input);
446
+ },
447
+ (result) => downstream(result),
448
+ );
449
+ });
450
+ }
451
+
452
+ export function composeTransducers(...transducers) {
453
+ for (const transducer of transducers) {
454
+ requireFunction(transducer, "composed transducer");
455
+ }
456
+ return makeTransducer(
457
+ (reducingFunction) => transducers.reduceRight(
458
+ (downstream, transducer) => applyTransducer(transducer, downstream),
459
+ asReducingFunction(reducingFunction, "composed reducing function"),
460
+ ),
461
+ transducers.some((transducer) => transducer[ZERO_INPUT] === true),
462
+ );
463
+ }
464
+
465
+ export function transduce(transducer, reducer, initial, collection) {
466
+ if (arguments.length !== 4) {
467
+ throw new TypeError(
468
+ "transduce requires a transducer, reducer, initial value, and collection",
469
+ );
470
+ }
471
+ const transformed = applyTransducer(
472
+ transducer,
473
+ asReducingFunction(reducer, "transduce reducer"),
474
+ );
475
+ const seed = unreduced(initial);
476
+ const result = transducer[ZERO_INPUT] === true
477
+ ? seed
478
+ : reduce(collection, transformed, seed);
479
+ return unreduced(transformed(unreduced(result)));
480
+ }
481
+
482
+ export function eduction(...arguments_) {
483
+ if (arguments_.length < 2) {
484
+ throw new TypeError("eduction requires one or more transducers and a collection");
485
+ }
486
+ const collection = arguments_[arguments_.length - 1];
487
+ const transducer = composeTransducers(...arguments_.slice(0, -1));
488
+ return reductionView((reducer, ...initial) => {
489
+ requireFunction(reducer, "eduction reducer");
490
+ if (initial.length > 0) {
491
+ return transduce(
492
+ transducer,
493
+ completing(reducer),
494
+ initial[0],
495
+ collection,
496
+ );
497
+ }
498
+ return transduce(
499
+ transducer,
500
+ completing(
501
+ (result, value) => result === NO_REDUCTION_INITIAL
502
+ ? value
503
+ : reducer(result, value),
504
+ (result) => {
505
+ if (result === NO_REDUCTION_INITIAL) {
506
+ throw new TypeError(
507
+ "cannot reduce an empty eduction without an initial value",
508
+ );
509
+ }
510
+ return result;
511
+ },
512
+ ),
513
+ NO_REDUCTION_INITIAL,
514
+ collection,
515
+ );
516
+ });
517
+ }
518
+
519
+ function transformedSequenceIterator(transducer, collection) {
520
+ const output = [];
521
+ let outputIndex = 0;
522
+ let input = null;
523
+ let transformed = null;
524
+ let accumulator = null;
525
+ let initialized = false;
526
+ let complete = false;
527
+
528
+ function closeInput() {
529
+ if (input !== null && typeof input.return === "function") input.return();
530
+ input = null;
531
+ }
532
+
533
+ function finish() {
534
+ if (complete) return;
535
+ complete = true;
536
+ closeInput();
537
+ accumulator = unreduced(transformed(unreduced(accumulator)));
538
+ }
539
+
540
+ function initialize() {
541
+ if (initialized) return;
542
+ initialized = true;
543
+ transformed = applyTransducer(
544
+ transducer,
545
+ completing((result, value) => {
546
+ output.push(value);
547
+ return result;
548
+ }),
549
+ );
550
+ if (transducer[ZERO_INPUT] === true) {
551
+ finish();
552
+ return;
553
+ }
554
+ const source = seq(collection);
555
+ if (source === null) {
556
+ finish();
557
+ return;
558
+ }
559
+ input = source[Symbol.iterator]();
560
+ }
561
+
562
+ function pump() {
563
+ initialize();
564
+ while (outputIndex === output.length && !complete) {
565
+ output.length = 0;
566
+ outputIndex = 0;
567
+ let next;
568
+ try {
569
+ next = input.next();
570
+ } catch (error) {
571
+ closeInput();
572
+ complete = true;
573
+ throw error;
574
+ }
575
+ if (next.done) {
576
+ finish();
577
+ continue;
578
+ }
579
+ let stepped;
580
+ try {
581
+ stepped = transformed(accumulator, next.value);
582
+ } catch (error) {
583
+ closeInput();
584
+ complete = true;
585
+ throw error;
586
+ }
587
+ if (isReduced(stepped)) {
588
+ accumulator = unreduced(stepped);
589
+ finish();
590
+ } else {
591
+ accumulator = stepped;
592
+ }
593
+ }
594
+ }
595
+
596
+ return {
597
+ next() {
598
+ pump();
599
+ if (outputIndex === output.length) {
600
+ return { value: undefined, done: true };
601
+ }
602
+ const value = output[outputIndex];
603
+ outputIndex += 1;
604
+ return { value, done: false };
605
+ },
606
+ return(value) {
607
+ output.length = 0;
608
+ outputIndex = 0;
609
+ complete = true;
610
+ closeInput();
611
+ return { value, done: true };
612
+ },
613
+ [Symbol.iterator]() {
614
+ return this;
615
+ },
616
+ };
617
+ }
618
+
619
+ export function sequence(transducer, collection) {
620
+ if (arguments.length !== 2) {
621
+ throw new TypeError("sequence requires a transducer and collection");
622
+ }
623
+ requireFunction(transducer, "sequence transducer");
624
+ return memoizedSequenceView(() =>
625
+ transformedSequenceIterator(transducer, collection));
626
+ }
627
+
628
+ export function runBang(procedure, collection) {
629
+ if (arguments.length !== 2) {
630
+ throw new TypeError("run! requires a procedure and collection");
631
+ }
632
+ requireFunction(procedure, "run! procedure");
633
+ reduce(collection, (_result, value) => {
634
+ procedure(value);
635
+ return null;
636
+ }, null);
637
+ return null;
638
+ }
639
+
640
+ export function into(target, ...arguments_) {
641
+ if (arguments_.length !== 1 && arguments_.length !== 2) {
642
+ throw new TypeError(
643
+ "into requires a target and source, or a target, transducer, and source",
644
+ );
645
+ }
646
+ const transducer = arguments_.length === 1
647
+ ? IDENTITY_TRANSDUCER
648
+ : arguments_[0];
649
+ const source = arguments_.length === 1 ? arguments_[0] : arguments_[1];
650
+ const seed = empty(target);
651
+ if (implementsProtocolOperation(IEditable, "transient", seed)) {
652
+ return transduce(
653
+ transducer,
654
+ completing(
655
+ (result, value) => conjBang(result, value),
656
+ (result) => persistentBang(result),
657
+ ),
658
+ transient(seed),
659
+ source,
660
+ );
661
+ }
662
+ return transduce(
663
+ transducer,
664
+ (result, value) => conj(result, value),
665
+ seed,
666
+ source,
667
+ );
668
+ }
@@ -0,0 +1,38 @@
1
+ import {
2
+ defineProtocol,
3
+ protocolMethod,
4
+ protocolSlot,
5
+ } from "./protocol.mjs";
6
+
7
+ export const I_EDITABLE = defineProtocol("IEditable", ["transient"]);
8
+ export const I_TRANSIENT_COLLECTION = defineProtocol(
9
+ "ITransientCollection",
10
+ ["conj!", "assoc!", "dissoc!", "persistent!"],
11
+ );
12
+
13
+ export const EDITABLE_TRANSIENT = protocolSlot(I_EDITABLE, "transient");
14
+ export const TRANSIENT_CONJ = protocolSlot(I_TRANSIENT_COLLECTION, "conj!");
15
+ export const TRANSIENT_ASSOC = protocolSlot(I_TRANSIENT_COLLECTION, "assoc!");
16
+ export const TRANSIENT_DISSOC = protocolSlot(I_TRANSIENT_COLLECTION, "dissoc!");
17
+ export const TRANSIENT_PERSISTENT = protocolSlot(
18
+ I_TRANSIENT_COLLECTION,
19
+ "persistent!",
20
+ );
21
+
22
+ export const dispatchEditableTransient = protocolMethod(I_EDITABLE, "transient");
23
+ export const dispatchTransientConj = protocolMethod(
24
+ I_TRANSIENT_COLLECTION,
25
+ "conj!",
26
+ );
27
+ export const dispatchTransientAssoc = protocolMethod(
28
+ I_TRANSIENT_COLLECTION,
29
+ "assoc!",
30
+ );
31
+ export const dispatchTransientDissoc = protocolMethod(
32
+ I_TRANSIENT_COLLECTION,
33
+ "dissoc!",
34
+ );
35
+ export const dispatchTransientPersistent = protocolMethod(
36
+ I_TRANSIENT_COLLECTION,
37
+ "persistent!",
38
+ );