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.
- package/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assoc,
|
|
3
|
+
conj,
|
|
4
|
+
contains,
|
|
5
|
+
count,
|
|
6
|
+
empty,
|
|
7
|
+
get,
|
|
8
|
+
reduce,
|
|
9
|
+
reduceKV,
|
|
10
|
+
reduced,
|
|
11
|
+
} from "./collection.mjs";
|
|
12
|
+
import {
|
|
13
|
+
keys,
|
|
14
|
+
merge,
|
|
15
|
+
selectKeys,
|
|
16
|
+
vals,
|
|
17
|
+
} from "./data.mjs";
|
|
18
|
+
import { EMPTY_MAP } from "./map.mjs";
|
|
19
|
+
import { meta, withMeta } from "./metadata.mjs";
|
|
20
|
+
import {
|
|
21
|
+
EMPTY_SET,
|
|
22
|
+
isPersistentHashSet,
|
|
23
|
+
} from "./set.mjs";
|
|
24
|
+
import {
|
|
25
|
+
assocBang,
|
|
26
|
+
conjBang,
|
|
27
|
+
dissocBang,
|
|
28
|
+
persistentBang,
|
|
29
|
+
transient,
|
|
30
|
+
} from "./transient.mjs";
|
|
31
|
+
import { isTruthy } from "./truth.mjs";
|
|
32
|
+
|
|
33
|
+
const NOT_FOUND = Object.freeze({});
|
|
34
|
+
|
|
35
|
+
function requireArity(actual, expected, label) {
|
|
36
|
+
if (actual !== expected) {
|
|
37
|
+
throw new TypeError(`${label} expects ${expected} collection${expected === 1 ? "" : "s"}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function requireNonEmpty(actual, label) {
|
|
42
|
+
if (actual === 0) {
|
|
43
|
+
throw new TypeError(`${label} expects at least one collection`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function requireFunction(value, label) {
|
|
48
|
+
if (typeof value !== "function") {
|
|
49
|
+
throw new TypeError(`${label} must be a function`);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function requireJoinArity(actual) {
|
|
55
|
+
if (actual !== 2 && actual !== 3) {
|
|
56
|
+
throw new TypeError("join expects 2 or 3 relations");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function persistentMap(collection) {
|
|
61
|
+
return withMeta(merge(collection), meta(collection));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function firstValue(collection) {
|
|
65
|
+
return reduce(collection, (_missing, value) =>
|
|
66
|
+
reduced({ value }), NOT_FOUND);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function set(collection) {
|
|
70
|
+
requireArity(arguments.length, 1, "set");
|
|
71
|
+
if (isPersistentHashSet(collection)) return collection;
|
|
72
|
+
|
|
73
|
+
const result = transient(EMPTY_SET);
|
|
74
|
+
reduce(collection, (builder, value) => {
|
|
75
|
+
conjBang(builder, value);
|
|
76
|
+
return builder;
|
|
77
|
+
}, result);
|
|
78
|
+
return persistentBang(result);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function union(...collections) {
|
|
82
|
+
if (collections.length === 0) return EMPTY_SET;
|
|
83
|
+
|
|
84
|
+
const result = transient(set(collections[0]));
|
|
85
|
+
for (let index = 1; index < collections.length; index += 1) {
|
|
86
|
+
reduce(collections[index], (builder, value) => {
|
|
87
|
+
conjBang(builder, value);
|
|
88
|
+
return builder;
|
|
89
|
+
}, result);
|
|
90
|
+
}
|
|
91
|
+
return persistentBang(result);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function intersectPair(left, right) {
|
|
95
|
+
const leftSet = set(left);
|
|
96
|
+
const rightSet = set(right);
|
|
97
|
+
const candidates = count(leftSet) <= count(rightSet) ? leftSet : rightSet;
|
|
98
|
+
const membership = candidates === leftSet ? rightSet : leftSet;
|
|
99
|
+
const builder = transient(empty(leftSet));
|
|
100
|
+
reduce(candidates, (result, value) => {
|
|
101
|
+
if (contains(membership, value)) conjBang(result, value);
|
|
102
|
+
return result;
|
|
103
|
+
}, builder);
|
|
104
|
+
const retained = persistentBang(builder);
|
|
105
|
+
return count(retained) === count(leftSet) ? leftSet : retained;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function intersection(...collections) {
|
|
109
|
+
requireNonEmpty(collections.length, "intersection");
|
|
110
|
+
let result = set(collections[0]);
|
|
111
|
+
for (let index = 1;
|
|
112
|
+
index < collections.length && count(result) > 0;
|
|
113
|
+
index += 1) {
|
|
114
|
+
result = intersectPair(result, collections[index]);
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function difference(...collections) {
|
|
120
|
+
requireNonEmpty(collections.length, "difference");
|
|
121
|
+
const result = transient(set(collections[0]));
|
|
122
|
+
for (let index = 1; index < collections.length; index += 1) {
|
|
123
|
+
reduce(collections[index], (builder, value) => {
|
|
124
|
+
dissocBang(builder, value);
|
|
125
|
+
return builder;
|
|
126
|
+
}, result);
|
|
127
|
+
}
|
|
128
|
+
return persistentBang(result);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function subset(left, right) {
|
|
132
|
+
requireArity(arguments.length, 2, "subset");
|
|
133
|
+
const candidates = set(left);
|
|
134
|
+
const membership = set(right);
|
|
135
|
+
if (count(candidates) > count(membership)) return false;
|
|
136
|
+
return reduce(candidates, (result, value) =>
|
|
137
|
+
contains(membership, value) ? result : reduced(false), true);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function superset(left, right) {
|
|
141
|
+
requireArity(arguments.length, 2, "superset");
|
|
142
|
+
return subset(right, left);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function disjoint(left, right) {
|
|
146
|
+
requireArity(arguments.length, 2, "disjoint");
|
|
147
|
+
const leftSet = set(left);
|
|
148
|
+
const rightSet = set(right);
|
|
149
|
+
const candidates = count(leftSet) <= count(rightSet) ? leftSet : rightSet;
|
|
150
|
+
const membership = candidates === leftSet ? rightSet : leftSet;
|
|
151
|
+
return reduce(candidates, (result, value) =>
|
|
152
|
+
contains(membership, value) ? reduced(false) : result, true);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function select(predicate, collection) {
|
|
156
|
+
requireArity(arguments.length, 2, "select");
|
|
157
|
+
requireFunction(predicate, "select predicate");
|
|
158
|
+
const source = set(collection);
|
|
159
|
+
const builder = transient(source);
|
|
160
|
+
reduce(source, (result, value) => {
|
|
161
|
+
if (!isTruthy(predicate(value))) dissocBang(result, value);
|
|
162
|
+
return result;
|
|
163
|
+
}, builder);
|
|
164
|
+
const selected = persistentBang(builder);
|
|
165
|
+
return count(selected) === count(source) ? source : selected;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function project(relation, selectedKeys) {
|
|
169
|
+
requireArity(arguments.length, 2, "project");
|
|
170
|
+
const source = set(relation);
|
|
171
|
+
const keySet = set(selectedKeys);
|
|
172
|
+
const builder = transient(empty(source));
|
|
173
|
+
reduce(source, (result, row) => {
|
|
174
|
+
conjBang(result, selectKeys(row, keySet));
|
|
175
|
+
return result;
|
|
176
|
+
}, builder);
|
|
177
|
+
return persistentBang(builder);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function renameKeys(collection, keyMap) {
|
|
181
|
+
requireArity(arguments.length, 2, "renameKeys");
|
|
182
|
+
const source = persistentMap(collection);
|
|
183
|
+
const mappings = persistentMap(keyMap);
|
|
184
|
+
const builder = transient(source);
|
|
185
|
+
reduceKV(mappings, (result, oldKey) => {
|
|
186
|
+
dissocBang(result, oldKey);
|
|
187
|
+
return result;
|
|
188
|
+
}, builder);
|
|
189
|
+
reduceKV(mappings, (result, oldKey, newKey) => {
|
|
190
|
+
if (contains(source, oldKey)) {
|
|
191
|
+
assocBang(result, newKey, get(source, oldKey));
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
}, builder);
|
|
195
|
+
return persistentBang(builder);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function rename(relation, keyMap) {
|
|
199
|
+
requireArity(arguments.length, 2, "rename");
|
|
200
|
+
const source = set(relation);
|
|
201
|
+
const mappings = persistentMap(keyMap);
|
|
202
|
+
const builder = transient(empty(source));
|
|
203
|
+
reduce(source, (result, row) => {
|
|
204
|
+
conjBang(result, renameKeys(row, mappings));
|
|
205
|
+
return result;
|
|
206
|
+
}, builder);
|
|
207
|
+
return persistentBang(builder);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function index(relation, selectedKeys) {
|
|
211
|
+
requireArity(arguments.length, 2, "index");
|
|
212
|
+
const source = set(relation);
|
|
213
|
+
const keySet = set(selectedKeys);
|
|
214
|
+
return reduce(source, (result, row) => {
|
|
215
|
+
const indexedKey = selectKeys(row, keySet);
|
|
216
|
+
const group = get(result, indexedKey, EMPTY_SET);
|
|
217
|
+
return assoc(result, indexedKey, conj(group, row));
|
|
218
|
+
}, EMPTY_MAP);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function mapInvert(collection) {
|
|
222
|
+
requireArity(arguments.length, 1, "mapInvert");
|
|
223
|
+
const builder = transient(EMPTY_MAP);
|
|
224
|
+
reduceKV(collection, (result, key, value) => {
|
|
225
|
+
assocBang(result, value, key);
|
|
226
|
+
return result;
|
|
227
|
+
}, builder);
|
|
228
|
+
return withMeta(persistentBang(builder), meta(collection));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function joinIndexed(probe, indexed, probeKeys, keyMap, builder) {
|
|
232
|
+
return reduce(probe, (result, row) => {
|
|
233
|
+
const selected = selectKeys(row, probeKeys);
|
|
234
|
+
const indexedKey = keyMap === null
|
|
235
|
+
? selected
|
|
236
|
+
: renameKeys(selected, keyMap);
|
|
237
|
+
const matches = get(indexed, indexedKey, EMPTY_SET);
|
|
238
|
+
reduce(matches, (output, matched) => {
|
|
239
|
+
conjBang(output, merge(matched, row));
|
|
240
|
+
return output;
|
|
241
|
+
}, result);
|
|
242
|
+
return result;
|
|
243
|
+
}, builder);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function join(left, right, keyMap) {
|
|
247
|
+
requireJoinArity(arguments.length);
|
|
248
|
+
const leftSet = set(left);
|
|
249
|
+
const rightSet = set(right);
|
|
250
|
+
const builder = transient(empty(leftSet));
|
|
251
|
+
if (count(leftSet) === 0 || count(rightSet) === 0) {
|
|
252
|
+
return persistentBang(builder);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (arguments.length === 2) {
|
|
256
|
+
const leftRow = firstValue(leftSet).value;
|
|
257
|
+
const rightRow = firstValue(rightSet).value;
|
|
258
|
+
const joinKeys = intersection(set(keys(leftRow)), set(keys(rightRow)));
|
|
259
|
+
const indexedRelation = count(leftSet) <= count(rightSet)
|
|
260
|
+
? leftSet
|
|
261
|
+
: rightSet;
|
|
262
|
+
const probeRelation = indexedRelation === leftSet ? rightSet : leftSet;
|
|
263
|
+
return persistentBang(joinIndexed(
|
|
264
|
+
probeRelation,
|
|
265
|
+
index(indexedRelation, joinKeys),
|
|
266
|
+
joinKeys,
|
|
267
|
+
null,
|
|
268
|
+
builder,
|
|
269
|
+
));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const mappings = persistentMap(keyMap);
|
|
273
|
+
const effectiveMap = count(leftSet) <= count(rightSet)
|
|
274
|
+
? mapInvert(mappings)
|
|
275
|
+
: mappings;
|
|
276
|
+
const indexedRelation = count(leftSet) <= count(rightSet)
|
|
277
|
+
? leftSet
|
|
278
|
+
: rightSet;
|
|
279
|
+
const probeRelation = indexedRelation === leftSet ? rightSet : leftSet;
|
|
280
|
+
return persistentBang(joinIndexed(
|
|
281
|
+
probeRelation,
|
|
282
|
+
index(indexedRelation, vals(effectiveMap)),
|
|
283
|
+
keys(effectiveMap),
|
|
284
|
+
effectiveMap,
|
|
285
|
+
builder,
|
|
286
|
+
));
|
|
287
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export const SET_CONSTRUCTOR_TOKEN = Symbol("eliscript.set.constructor");
|
|
2
|
+
export const SET_STATE = Symbol("eliscript.set.state");
|
|
3
|
+
export const TRANSIENT_SET_CONSTRUCTOR_TOKEN = Symbol(
|
|
4
|
+
"eliscript.set.transient-constructor",
|
|
5
|
+
);
|
|
6
|
+
export const TRANSIENT_SET_STATE = Symbol("eliscript.set.transient-state");
|
|
7
|
+
|
|
8
|
+
const transientMetrics = {
|
|
9
|
+
persistentCalls: 0,
|
|
10
|
+
invalidCalls: 0,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const SET_PRESENT = Object.freeze({
|
|
14
|
+
[Symbol.toStringTag]: "EliscriptPersistentHashSetEntry",
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export function resetTransientSetMetrics() {
|
|
18
|
+
for (const key of Object.keys(transientMetrics)) {
|
|
19
|
+
transientMetrics[key] = 0;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function readTransientSetMetrics() {
|
|
24
|
+
return Object.freeze({ ...transientMetrics });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function recordTransientSetPersistent() {
|
|
28
|
+
transientMetrics.persistentCalls += 1;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function recordInvalidTransientSetCall() {
|
|
32
|
+
transientMetrics.invalidCalls += 1;
|
|
33
|
+
}
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EMPTY_MAP,
|
|
3
|
+
PersistentHashMap,
|
|
4
|
+
} from "./map.mjs";
|
|
5
|
+
import {
|
|
6
|
+
SET_CONSTRUCTOR_TOKEN,
|
|
7
|
+
SET_PRESENT,
|
|
8
|
+
SET_STATE,
|
|
9
|
+
TRANSIENT_SET_CONSTRUCTOR_TOKEN,
|
|
10
|
+
TRANSIENT_SET_STATE,
|
|
11
|
+
recordInvalidTransientSetCall,
|
|
12
|
+
recordTransientSetPersistent,
|
|
13
|
+
} from "./set-internals.mjs";
|
|
14
|
+
import {
|
|
15
|
+
VALUE_EQUAL,
|
|
16
|
+
VALUE_HASH,
|
|
17
|
+
unorderedCollectionHash,
|
|
18
|
+
} from "./value-internals.mjs";
|
|
19
|
+
import {
|
|
20
|
+
COLLECTION_COUNT,
|
|
21
|
+
COLLECTION_EMPTY,
|
|
22
|
+
COLLECTION_CONJ,
|
|
23
|
+
COLLECTION_GET,
|
|
24
|
+
COLLECTION_CONTAINS,
|
|
25
|
+
COLLECTION_REDUCE,
|
|
26
|
+
COLLECTION_DISJ,
|
|
27
|
+
COLLECTION_SEQ,
|
|
28
|
+
reduceIterable,
|
|
29
|
+
sequenceView,
|
|
30
|
+
} from "./collection-internals.mjs";
|
|
31
|
+
import {
|
|
32
|
+
EDITABLE_TRANSIENT,
|
|
33
|
+
TRANSIENT_ASSOC,
|
|
34
|
+
TRANSIENT_CONJ,
|
|
35
|
+
TRANSIENT_DISSOC,
|
|
36
|
+
TRANSIENT_PERSISTENT,
|
|
37
|
+
} from "./transient-internals.mjs";
|
|
38
|
+
import {
|
|
39
|
+
METADATA_READ,
|
|
40
|
+
METADATA_WITH,
|
|
41
|
+
} from "./metadata-internals.mjs";
|
|
42
|
+
import {
|
|
43
|
+
PERSISTENT_SET_HAS_VALUE,
|
|
44
|
+
PERSISTENT_SET_KIND,
|
|
45
|
+
persistentSetValueEqual,
|
|
46
|
+
} from "./persistent-kind.mjs";
|
|
47
|
+
|
|
48
|
+
const SET_HASH_TAG = 0x51e7_b32d;
|
|
49
|
+
|
|
50
|
+
function makeSet(map, metadata = null) {
|
|
51
|
+
return new PersistentHashSet(SET_CONSTRUCTOR_TOKEN, map, metadata);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function emptySetWithMetadata(metadata) {
|
|
55
|
+
return metadata === null ? EMPTY_SET : makeSet(EMPTY_MAP, metadata);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function makeTransientSet(set) {
|
|
59
|
+
return new TransientHashSet(TRANSIENT_SET_CONSTRUCTOR_TOKEN, set);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function activeTransientSetState(set) {
|
|
63
|
+
const state = set[TRANSIENT_SET_STATE];
|
|
64
|
+
if (!state.active) {
|
|
65
|
+
recordInvalidTransientSetCall();
|
|
66
|
+
throw new TypeError("transient hash set is no longer editable");
|
|
67
|
+
}
|
|
68
|
+
return state;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function asPersistentSet(values) {
|
|
72
|
+
return values instanceof PersistentHashSet
|
|
73
|
+
? values
|
|
74
|
+
: PersistentHashSet.from(values);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
class TransientHashSet {
|
|
78
|
+
constructor(token, set) {
|
|
79
|
+
if (token !== TRANSIENT_SET_CONSTRUCTOR_TOKEN ||
|
|
80
|
+
!(set instanceof PersistentHashSet)) {
|
|
81
|
+
throw new TypeError(
|
|
82
|
+
"transient hash sets must be created from a persistent hash set",
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const sourceMap = set[SET_STATE].map;
|
|
86
|
+
const metadata = set[SET_STATE].metadata;
|
|
87
|
+
this[TRANSIENT_SET_STATE] = {
|
|
88
|
+
active: true,
|
|
89
|
+
source: set,
|
|
90
|
+
sourceMap,
|
|
91
|
+
map: sourceMap[EDITABLE_TRANSIENT](),
|
|
92
|
+
metadata,
|
|
93
|
+
};
|
|
94
|
+
Object.defineProperty(this, "__eliscript_transient__", {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
get() {
|
|
97
|
+
throw new TypeError("transient hash sets cannot be serialized");
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
Object.freeze(this);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
[TRANSIENT_CONJ](value) {
|
|
104
|
+
const state = activeTransientSetState(this);
|
|
105
|
+
state.map[TRANSIENT_ASSOC](value, SET_PRESENT);
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
[TRANSIENT_DISSOC](value) {
|
|
110
|
+
const state = activeTransientSetState(this);
|
|
111
|
+
state.map[TRANSIENT_DISSOC](value);
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
[TRANSIENT_PERSISTENT]() {
|
|
116
|
+
const state = activeTransientSetState(this);
|
|
117
|
+
const map = state.map[TRANSIENT_PERSISTENT]();
|
|
118
|
+
const result = map === state.sourceMap
|
|
119
|
+
? state.source
|
|
120
|
+
: (map === EMPTY_MAP
|
|
121
|
+
? emptySetWithMetadata(state.metadata)
|
|
122
|
+
: makeSet(map, state.metadata));
|
|
123
|
+
state.active = false;
|
|
124
|
+
recordTransientSetPersistent();
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
toJSON() {
|
|
129
|
+
throw new TypeError("transient hash sets cannot be serialized");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
get [Symbol.toStringTag]() {
|
|
133
|
+
return "EliscriptTransientHashSet";
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export class PersistentHashSet {
|
|
138
|
+
constructor(token, map, metadata = null) {
|
|
139
|
+
if (token !== SET_CONSTRUCTOR_TOKEN || !(map instanceof PersistentHashMap)) {
|
|
140
|
+
throw new TypeError(
|
|
141
|
+
"PersistentHashSet values must be created with persistentHashSet or PersistentHashSet.from",
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
this[SET_STATE] = Object.freeze({ map, metadata });
|
|
145
|
+
Object.freeze(this);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static empty() {
|
|
149
|
+
return EMPTY_SET;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static from(values) {
|
|
153
|
+
if (values instanceof PersistentHashSet) {
|
|
154
|
+
return values;
|
|
155
|
+
}
|
|
156
|
+
let result = EMPTY_SET;
|
|
157
|
+
for (const value of values) {
|
|
158
|
+
result = result.conj(value);
|
|
159
|
+
}
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
get count() {
|
|
164
|
+
return this[SET_STATE].map.count;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get size() {
|
|
168
|
+
return this[SET_STATE].map.count;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
has(value) {
|
|
172
|
+
return this[SET_STATE].map.has(value);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
conj(value) {
|
|
176
|
+
const state = this[SET_STATE];
|
|
177
|
+
const map = state.map.assoc(value, SET_PRESENT);
|
|
178
|
+
return map === state.map ? this : makeSet(map, state.metadata);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
disj(value) {
|
|
182
|
+
const state = this[SET_STATE];
|
|
183
|
+
const map = state.map.dissoc(value);
|
|
184
|
+
if (map === state.map) {
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
return map === EMPTY_MAP
|
|
188
|
+
? emptySetWithMetadata(state.metadata)
|
|
189
|
+
: makeSet(map, state.metadata);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
union(...collections) {
|
|
193
|
+
let result = this;
|
|
194
|
+
for (const collection of collections) {
|
|
195
|
+
for (const value of collection) {
|
|
196
|
+
result = result.conj(value);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
intersection(...collections) {
|
|
203
|
+
let result = this;
|
|
204
|
+
for (const collection of collections) {
|
|
205
|
+
const other = asPersistentSet(collection);
|
|
206
|
+
if (result.count === 0) {
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
const candidates = result.count <= other.count ? result : other;
|
|
210
|
+
const membership = candidates === result ? other : result;
|
|
211
|
+
let retained = emptySetWithMetadata(this[SET_STATE].metadata);
|
|
212
|
+
for (const value of candidates) {
|
|
213
|
+
if (membership.has(value)) {
|
|
214
|
+
retained = retained.conj(value);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
result = retained.count === result.count ? result : retained;
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
difference(...collections) {
|
|
223
|
+
let result = this;
|
|
224
|
+
for (const collection of collections) {
|
|
225
|
+
for (const value of collection) {
|
|
226
|
+
result = result.disj(value);
|
|
227
|
+
}
|
|
228
|
+
if (result.count === 0) {
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
isSubsetOf(collection) {
|
|
236
|
+
const other = asPersistentSet(collection);
|
|
237
|
+
if (this.count > other.count) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
for (const value of this) {
|
|
241
|
+
if (!other.has(value)) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
isSupersetOf(collection) {
|
|
249
|
+
return asPersistentSet(collection).isSubsetOf(this);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
isDisjointFrom(collection) {
|
|
253
|
+
const other = asPersistentSet(collection);
|
|
254
|
+
const candidates = this.count <= other.count ? this : other;
|
|
255
|
+
const membership = candidates === this ? other : this;
|
|
256
|
+
for (const value of candidates) {
|
|
257
|
+
if (membership.has(value)) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
values() {
|
|
265
|
+
return this[SET_STATE].map.keys();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
keys() {
|
|
269
|
+
return this.values();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
*entries() {
|
|
273
|
+
for (const value of this) {
|
|
274
|
+
yield Object.freeze([value, value]);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
reduce(reducer, initial) {
|
|
279
|
+
if (typeof reducer !== "function") {
|
|
280
|
+
throw new TypeError("persistent hash set reducer must be a function");
|
|
281
|
+
}
|
|
282
|
+
if (arguments.length < 2) {
|
|
283
|
+
throw new TypeError("persistent hash set reduce requires an initial value");
|
|
284
|
+
}
|
|
285
|
+
let result = initial;
|
|
286
|
+
for (const value of this) {
|
|
287
|
+
result = reducer(result, value, this);
|
|
288
|
+
}
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
[COLLECTION_COUNT]() {
|
|
293
|
+
return this.count;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
[COLLECTION_EMPTY]() {
|
|
297
|
+
return emptySetWithMetadata(this[SET_STATE].metadata);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
[COLLECTION_CONJ](value) {
|
|
301
|
+
return this.conj(value);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
[COLLECTION_DISJ](value) {
|
|
305
|
+
return this.disj(value);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
[COLLECTION_GET](value, notFound = null) {
|
|
309
|
+
return this.has(value) ? value : notFound;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
[COLLECTION_CONTAINS](value) {
|
|
313
|
+
return this.has(value);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
[COLLECTION_SEQ]() {
|
|
317
|
+
return this.count === 0
|
|
318
|
+
? null
|
|
319
|
+
: sequenceView(() => this.values(), this.count);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
[COLLECTION_REDUCE](reducer, ...initial) {
|
|
323
|
+
return reduceIterable(this, reducer, ...initial);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
[EDITABLE_TRANSIENT]() {
|
|
327
|
+
return makeTransientSet(this);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
toSet() {
|
|
331
|
+
return new Set(this);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
[PERSISTENT_SET_HAS_VALUE](value) {
|
|
335
|
+
return this.has(value);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
get [PERSISTENT_SET_KIND]() {
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
[VALUE_EQUAL](other, equal) {
|
|
343
|
+
return persistentSetValueEqual(this, other, equal);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
[VALUE_HASH](hash) {
|
|
347
|
+
return unorderedCollectionHash(this, hash, SET_HASH_TAG);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
[METADATA_READ]() {
|
|
351
|
+
return this[SET_STATE].metadata;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
[METADATA_WITH](metadata) {
|
|
355
|
+
const state = this[SET_STATE];
|
|
356
|
+
return metadata === state.metadata
|
|
357
|
+
? this
|
|
358
|
+
: makeSet(state.map, metadata);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
[Symbol.iterator]() {
|
|
362
|
+
return this.values();
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
get [Symbol.toStringTag]() {
|
|
366
|
+
return "EliscriptPersistentHashSet";
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export const EMPTY_SET = makeSet(EMPTY_MAP);
|
|
371
|
+
|
|
372
|
+
export function persistentHashSet(...values) {
|
|
373
|
+
return PersistentHashSet.from(values);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
export function isPersistentHashSet(value) {
|
|
377
|
+
return value instanceof PersistentHashSet;
|
|
378
|
+
}
|