@mark1russell7/vex 0.14.0 → 0.15.0

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/build/index.js CHANGED
@@ -1065,14 +1065,19 @@ function domainOf(domains, value) {
1065
1065
  }
1066
1066
  /** This function tells if the `liftScalar` setting lifts the argument at `index`. */
1067
1067
  const liftsAt = (lift, index) => lift === true || Array.isArray(lift) && lift[index] === true;
1068
+ // src/checked.ts
1069
+ /**
1070
+ * The cache of the lists that passed the check of LIST.SHAPE. `isVexList` adds each frozen list that passes its check.
1071
+ * The interpreter adds each list that it makes from the items of such lists, because these items passed the check.
1072
+ * The package index does not export this module.
1073
+ */
1074
+ const CHECKED = /* @__PURE__ */ new WeakSet();
1068
1075
  // src/list.ts
1069
1076
  /** This function makes a list. */
1070
1077
  const vexList = (items) => Object.freeze({
1071
1078
  kind: "vex.list",
1072
1079
  items: Object.freeze([...items])
1073
1080
  });
1074
- /** The lists that passed the check of `isVexList` and cannot change. Each list of `vexList` is frozen. */
1075
- const CHECKED = /* @__PURE__ */ new WeakSet();
1076
1081
  /** This function tells if a value is an error value: a text `code`, `kind` and `message`, and a `path` (LIST.SHAPE). */
1077
1082
  const isErrorValue = (u) => {
1078
1083
  if (typeof u !== "object" || u === null) return false;
@@ -1127,7 +1132,10 @@ const LIST_OPS = /* @__PURE__ */ new Set([
1127
1132
  "flat",
1128
1133
  "without",
1129
1134
  "get",
1130
- "indexOf"
1135
+ "indexOf",
1136
+ "distinct",
1137
+ "group",
1138
+ "join"
1131
1139
  ]);
1132
1140
  /** This function tells if a name is the name of a core list op. */
1133
1141
  const isListOp = (op) => LIST_OPS.has(op);
@@ -2088,7 +2096,9 @@ const WITH_ARGUMENT$1 = /* @__PURE__ */ new Set([
2088
2096
  "append",
2089
2097
  "without",
2090
2098
  "get",
2091
- "indexOf"
2099
+ "indexOf",
2100
+ "group",
2101
+ "join"
2092
2102
  ]);
2093
2103
  /**
2094
2104
  * This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
@@ -2116,10 +2126,33 @@ function flat(vs, strict) {
2116
2126
  }
2117
2127
  return vexList(out);
2118
2128
  }
2129
+ /**
2130
+ * This function gives the groups of `group(list, keys)` (LIST.GROUP). The item at a position has the key at the same
2131
+ * position. A pair with an error item or an error key is not in a group.
2132
+ */
2133
+ function group(items, keys, strict) {
2134
+ goodOf(items, strict);
2135
+ const list = asList(keys, missing);
2136
+ if (list === void 0) return stop("kind-mismatch", `"group" needs a list of keys, not a ${describe(keys)}`);
2137
+ goodOf(list.items, strict);
2138
+ if (list.items.length !== items.length) return stop("kind-mismatch", "\"group\" needs one key for each item");
2139
+ const groups = /* @__PURE__ */ new Map();
2140
+ items.forEach((it, i) => {
2141
+ const k = list.items[i].result;
2142
+ if (!it.result.ok || !k.ok) return;
2143
+ if (typeof k.value !== "string") stop("kind-mismatch", "\"group\" needs text keys");
2144
+ groups.set(k.value, [...groups.get(k.value) ?? [], it]);
2145
+ });
2146
+ return vexList([...groups].map(([key, g]) => ({
2147
+ key,
2148
+ result: ok(vexList(g))
2149
+ })));
2150
+ }
2119
2151
  /** This function applies a list op (LIST.*). The error items are skipped, except with `{ strict: true }`, and except `#DEPTH!`. */
2120
2152
  function listOp(op, items, rest, domains) {
2121
2153
  const o = WITH_ARGUMENT$1.has(op) ? rest[1] : rest[0];
2122
2154
  const strict = o?.strict === true;
2155
+ if (op === "group") return group(items, rest[0], strict);
2123
2156
  const good = goodOf(items, strict);
2124
2157
  const vs = good.map((it) => it.result.ok ? it.result.value : void 0);
2125
2158
  const nums = () => vs.every((x) => typeof x === "number") ? vs : stop("kind-mismatch", `"${op}" needs numbers`);
@@ -2161,6 +2194,16 @@ function listOp(op, items, rest, domains) {
2161
2194
  const i = good.findIndex((it) => it.key === key);
2162
2195
  return i >= 0 ? vs[i] : stop("missing-field", `the list has no item with the key ${JSON.stringify(key)}`);
2163
2196
  }
2197
+ case "distinct": {
2198
+ if (!vs.every(isIndexValue)) return stop("kind-mismatch", "\"distinct\" needs numbers, texts or booleans");
2199
+ const keep = vs.map((x, i) => vs.indexOf(x) === i || Number.isNaN(x));
2200
+ return vexList(good.filter((_it, i) => keep[i]));
2201
+ }
2202
+ case "join": {
2203
+ const sep = rest[0];
2204
+ if (typeof sep !== "string") return stop("kind-mismatch", `"join" needs a text separator, not a ${describe(sep)}`);
2205
+ return vs.every((x) => typeof x === "string") ? vs.join(sep) : stop("kind-mismatch", "\"join\" needs texts");
2206
+ }
2164
2207
  case "indexOf": {
2165
2208
  const x = rest[0];
2166
2209
  if (!isIndexValue(x)) return stop("kind-mismatch", `"indexOf" needs a number, a text or a boolean, not a ${describe(x)}`);
@@ -2999,6 +3042,33 @@ var Reader = class {
2999
3042
  }
3000
3043
  };
3001
3044
  // src/evaluate.ts
3045
+ /**
3046
+ * A scope: the names of one `let`, a function, an item or a loop. It is in front of the outer names, and it does not
3047
+ * copy them. A name of a scope hides an outer name of the same name. The names of one
3048
+ * scope are distinct (FN.BIND, LOOP.NAMES, AXIS.LIST.SCOPE).
3049
+ */
3050
+ var Scope = class Scope {
3051
+ #outer;
3052
+ #names;
3053
+ #values;
3054
+ constructor(outer, names, values) {
3055
+ this.#outer = outer;
3056
+ this.#names = names;
3057
+ this.#values = values;
3058
+ }
3059
+ get(name) {
3060
+ const at = this.#names.indexOf(name);
3061
+ return at >= 0 ? this.#values[at] : this.#outer.get(name);
3062
+ }
3063
+ /** This method gives a copy of this scope with the values `values` at the positions `at` (LOOP.NAMES). */
3064
+ with(at, values) {
3065
+ const next = [...this.#values];
3066
+ at.forEach((position, i) => {
3067
+ next[position] = values[i];
3068
+ });
3069
+ return new Scope(this.#outer, this.#names, next);
3070
+ }
3071
+ };
3002
3072
  /** The names of the special forms. Their arguments are not evaluated before the call. */
3003
3073
  const SPECIAL_FORMS = /* @__PURE__ */ new Set([
3004
3074
  "if",
@@ -3120,17 +3190,30 @@ function explain(expr, opts) {
3120
3190
  }
3121
3191
  /** This function gives the axis inside the `where` axes of an axis. */
3122
3192
  const baseAxis = (a) => a.t === "where" ? baseAxis(a.axis) : a;
3193
+ /** This function evaluates the nodes in order. The first error stops it and is the result. */
3194
+ function allValues(ns, f, c) {
3195
+ const values = [];
3196
+ for (const n of ns) {
3197
+ const r = n(f, c);
3198
+ if (!r.ok) return r;
3199
+ values.push(r);
3200
+ }
3201
+ return ok(values);
3202
+ }
3203
+ /** The primitive types of KIND.PRIMITIVE. */
3204
+ const PRIMITIVE_TYPES = [
3205
+ "number",
3206
+ "string",
3207
+ "boolean"
3208
+ ];
3209
+ /** The spec of an op without an entry in the op table of a domain with `methods: "all"`. */
3210
+ const NO_SPEC = Object.freeze({});
3123
3211
  /**
3124
3212
  * This function adds the names `as` of a loop to the names of a body or of a step. The first name holds the value of
3125
3213
  * the item, and the second its index (AXIS.LIST.ITEM, AXIS.LIST.INDEX). A loop without `as` keeps its names.
3126
3214
  */
3127
3215
  function withItem(env, as, t) {
3128
- if (as === void 0) return env;
3129
- const out = new Map(env);
3130
- const [item, index] = as;
3131
- out.set(item, ok(t.value));
3132
- if (index !== void 0) out.set(index, ok(t.index));
3133
- return out;
3216
+ return as === void 0 ? env : new Scope(env, as, [ok(t.value), ok(t.index)]);
3134
3217
  }
3135
3218
  var Compiler = class {
3136
3219
  #domains;
@@ -3138,10 +3221,19 @@ var Compiler = class {
3138
3221
  #extensions;
3139
3222
  /** The index of the keys of each list that `get` read (LIST.GET). A list does not change, so its index stays right. */
3140
3223
  #keyIndex = /* @__PURE__ */ new WeakMap();
3224
+ /**
3225
+ * The domains that can accept a value of each primitive type, in order (KIND.PRIMITIVE). These are the domains of
3226
+ * that type and the domains without a primitive type.
3227
+ */
3228
+ #ofType = /* @__PURE__ */ new Map();
3229
+ /** The domains without a primitive type. Only they can accept a value of another type, for example an object. */
3230
+ #open;
3141
3231
  constructor(opts) {
3142
3232
  this.#domains = opts.domains ?? [];
3143
3233
  this.#fns = opts.fns;
3144
3234
  this.#extensions = opts.extensions;
3235
+ this.#open = this.#domains.filter((d) => d.primitive === void 0);
3236
+ for (const t of PRIMITIVE_TYPES) this.#ofType.set(t, this.#domains.filter((d) => d.primitive === void 0 || d.primitive === t));
3145
3237
  }
3146
3238
  #err(kind, message, f, path, extra = {}) {
3147
3239
  return fail(vexError(kind, message, {
@@ -3163,7 +3255,10 @@ var Compiler = class {
3163
3255
  #fill(values, f, path) {
3164
3256
  const [n, value] = values;
3165
3257
  if (!isFillCount(n)) return this.#err("kind-mismatch", `"fill" needs a whole number from 0 to 100000, not ${describeType(n)}`, f, path, { op: "fill" });
3166
- return value === void 0 ? this.#err("bad-expression", "\"fill\" needs argument 2", f, path, { op: "fill" }) : ok(fillList(n, value));
3258
+ if (value === void 0) return this.#err("bad-expression", "\"fill\" needs argument 2", f, path, { op: "fill" });
3259
+ const list = fillList(n, value);
3260
+ CHECKED.add(list);
3261
+ return ok(list);
3167
3262
  }
3168
3263
  #emit(c, info, f, result, reads) {
3169
3264
  if (c.trace !== void 0) {
@@ -3204,14 +3299,15 @@ var Compiler = class {
3204
3299
  case "fn": return this.#fn(e, path);
3205
3300
  case "call": return this.#callFn(e, info);
3206
3301
  case "let": {
3207
- const binds = Object.entries(e.bind).map(([name, be], i) => [name, this.node(be, [...path, i])]);
3302
+ const names = Object.keys(e.bind);
3303
+ const binds = this.#nodes(Object.values(e.bind), path);
3208
3304
  const body = this.node(e.body, [...path, binds.length]);
3209
3305
  return (f, c) => {
3210
- const env = new Map(f.env);
3211
- for (const [name, n] of binds) env.set(name, n(f, c));
3306
+ const values = [];
3307
+ for (const n of binds) values.push(n(f, c));
3212
3308
  return this.#emit(c, info, f, body({
3213
3309
  pos: f.pos,
3214
- env
3310
+ env: new Scope(f.env, names, values)
3215
3311
  }, c));
3216
3312
  };
3217
3313
  }
@@ -3267,11 +3363,11 @@ var Compiler = class {
3267
3363
  const callee = bound.value;
3268
3364
  if (!(callee instanceof Closure)) return this.#emit(c, info, f, this.#err("kind-mismatch", `the name "${e.fn}" is a value, not a function`, f, path));
3269
3365
  if (callee.params.length !== args.length) return this.#emit(c, info, f, this.#err("bad-expression", `the function "${e.fn}" takes ${String(callee.params.length)} arguments, not ${String(args.length)}`, f, path));
3270
- const env = new Map(callee.env);
3271
- callee.params.forEach((p, i) => env.set(p, args[i](f, c)));
3366
+ const values = [];
3367
+ for (const n of args) values.push(n(f, c));
3272
3368
  return this.#emit(c, info, f, callee.body({
3273
3369
  pos: f.pos,
3274
- env
3370
+ env: new Scope(callee.env, callee.params, values)
3275
3371
  }, c));
3276
3372
  };
3277
3373
  }
@@ -3440,28 +3536,18 @@ var Compiler = class {
3440
3536
  * functions give the first values, and the values after one step. The first error stops each of them.
3441
3537
  */
3442
3538
  #names(e, path) {
3443
- const inits = Object.entries(e.init).map(([name, x], i) => [name, this.node(x, [...path, i])]);
3444
- const steps = Object.entries(e.step).map(([name, x], i) => [name, this.node(x, [...path, inits.length + i])]);
3539
+ const names = Object.keys(e.init);
3540
+ const inits = this.#nodes(Object.values(e.init), path);
3541
+ const steps = Object.values(e.step).map((x, i) => this.node(x, [...path, inits.length + i]));
3542
+ const at = Object.keys(e.step).map((name) => names.indexOf(name));
3445
3543
  return {
3446
3544
  first: (f, c) => {
3447
- const env = new Map(f.env);
3448
- for (const [name, n] of inits) {
3449
- const r = n(f, c);
3450
- if (!r.ok) return r;
3451
- env.set(name, r);
3452
- }
3453
- return ok(env);
3545
+ const values = allValues(inits, f, c);
3546
+ return values.ok ? ok(new Scope(f.env, names, values.value)) : values;
3454
3547
  },
3455
- next: (f, c, names = f.env) => {
3456
- const values = [];
3457
- for (const [name, n] of steps) {
3458
- const r = n(f, c);
3459
- if (!r.ok) return r;
3460
- values.push([name, r]);
3461
- }
3462
- const env = new Map(names);
3463
- for (const [name, r] of values) env.set(name, r);
3464
- return ok(env);
3548
+ next: (f, c, scope) => {
3549
+ const values = allValues(steps, f, c);
3550
+ return values.ok ? ok(scope.with(at, values.value)) : values;
3465
3551
  }
3466
3552
  };
3467
3553
  }
@@ -3555,7 +3641,7 @@ var Compiler = class {
3555
3641
  if (until !== void 0) return this.#emit(c, info, f, this.#err("limit", `"iterate" made its ${String(limit)} steps, and its test "until" is not true`, f, path));
3556
3642
  break;
3557
3643
  }
3558
- const after = names.next(frame, c);
3644
+ const after = names.next(frame, c, env);
3559
3645
  if (!after.ok) return this.#emit(c, info, f, after);
3560
3646
  env = after.value;
3561
3647
  }
@@ -3638,6 +3724,7 @@ var Compiler = class {
3638
3724
  const { path } = info;
3639
3725
  const listOp = isListOp(op) ? op : void 0;
3640
3726
  const fn = this.#fns !== void 0 && Object.hasOwn(this.#fns, op) ? this.#fns[op] : void 0;
3727
+ const named = isOpName(op);
3641
3728
  return (f, c) => {
3642
3729
  const all = this.#all(args, f, c, path, "arguments");
3643
3730
  if (!all.ok) return this.#emit(c, info, f, all);
@@ -3648,8 +3735,10 @@ var Compiler = class {
3648
3735
  if (values.length > 0) for (const d of this.#domains) {
3649
3736
  if (!safeIs(d, self)) continue;
3650
3737
  accepted ??= d;
3651
- const resolved = resolveOp(d, self, op);
3652
- if (resolved !== void 0) return this.#emit(c, info, f, this.#call(resolved, op, values.slice(1), f, path));
3738
+ const spec = named && Object.hasOwn(d.ops, op) ? d.ops[op] : void 0;
3739
+ if (spec?.fn !== void 0) return this.#emit(c, info, f, this.#call(d, spec, spec.fn, self, op, values.slice(1), f, path));
3740
+ const method = spec !== void 0 || d.methods === "all" ? findMethod(self, op) : void 0;
3741
+ if (method !== void 0) return this.#emit(c, info, f, this.#call(d, spec ?? NO_SPEC, (receiver, ...given) => method.apply(receiver, [...given]), self, op, values.slice(1), f, path));
3653
3742
  }
3654
3743
  if (fn !== void 0) return this.#emit(c, info, f, this.#checkResult(this.#invoke(() => fn(...values), op, f, path), f, path, op));
3655
3744
  if (listOp !== void 0 && Array.isArray(self)) return this.#emit(c, info, f, this.#listOp(listOp, listOfArray(self, this.#missing(f, path)), values.slice(1), f, path));
@@ -3694,8 +3783,15 @@ var Compiler = class {
3694
3783
  };
3695
3784
  }
3696
3785
  }
3697
- #call(op, name, rest, f, path) {
3698
- const { domain, spec } = op;
3786
+ /** This method starts an op that resolveOp found, for example a method of the receiver. */
3787
+ #callResolved(op, self, name, rest, f, path) {
3788
+ return this.#call(op.domain, op.spec, (_self, ...args) => op.call(args), self, name, rest, f, path);
3789
+ }
3790
+ /**
3791
+ * This method starts the op `name` of the domain `domain` with the receiver `self` and the arguments `rest`. It lifts
3792
+ * the numbers, checks the arguments and checks the result.
3793
+ */
3794
+ #call(domain, spec, run, self, name, rest, f, path) {
3699
3795
  const lift = spec.liftScalar;
3700
3796
  const from = domain.fromScalar;
3701
3797
  let callArgs = rest;
@@ -3711,7 +3807,13 @@ var Compiler = class {
3711
3807
  return this.#err(k, `argument ${i + 1} of "${name}" is a ${describeType(callArgs[i])}, not a ${kind === "domain" ? domain.name : kind}`, f, path, { op: name });
3712
3808
  }
3713
3809
  }
3714
- return this.#checkResult(this.#invoke(() => op.call(callArgs), name, f, path), f, path, name);
3810
+ let value;
3811
+ try {
3812
+ value = run(self, ...callArgs);
3813
+ } catch (thrown) {
3814
+ return this.#threw(thrown, `"${name}" threw an exception`, f, path, { op: name });
3815
+ }
3816
+ return this.#checkResult(ok(value), f, path, name);
3715
3817
  }
3716
3818
  #invoke(run, op, f, path) {
3717
3819
  try {
@@ -3720,6 +3822,14 @@ var Compiler = class {
3720
3822
  return this.#threw(thrown, `"${op}" threw an exception`, f, path, { op });
3721
3823
  }
3722
3824
  }
3825
+ /**
3826
+ * This method gives the first domain that accepts a value. It starts only the tests of the domains without a primitive
3827
+ * type: a domain of the type of the value accepts it (KIND.PRIMITIVE).
3828
+ */
3829
+ #domainOf(value) {
3830
+ const candidates = this.#ofType.get(typeof value) ?? this.#open;
3831
+ for (const d of candidates) if (d.primitive !== void 0 || testIs(d, value)) return d;
3832
+ }
3723
3833
  /** This method checks the value that an op gave: no `undefined`, finite numbers, and valid domain values. */
3724
3834
  #checkResult(r, f, path, op) {
3725
3835
  if (!r.ok) return r;
@@ -3727,7 +3837,7 @@ var Compiler = class {
3727
3837
  const extra = { op };
3728
3838
  if (value === void 0 || value === null) return this.#err("undefined-result", `"${op}" gave no value`, f, path, extra);
3729
3839
  if (typeof value === "number" && !Number.isFinite(value)) return this.#err("not-finite", `"${op}" gave ${value}`, f, path, extra);
3730
- const d = this.#domains.find((dom) => safeIs(dom, value));
3840
+ const d = this.#domainOf(value);
3731
3841
  if (d?.valid !== void 0) {
3732
3842
  let valid;
3733
3843
  try {
@@ -3743,10 +3853,12 @@ var Compiler = class {
3743
3853
  const optsArg = WITH_ARGUMENT.has(op) ? rest[1] : rest[0];
3744
3854
  const strict = optsArg?.strict === true;
3745
3855
  if (op === "get") return this.#get(list, rest[0], strict, f, path);
3856
+ if (op === "group") return this.#group(list, rest[0], strict, f, path);
3746
3857
  const good = goodItems(list, strict);
3747
3858
  if (!good.ok) return good;
3748
3859
  const okItems = good.value;
3749
- const okValues = okItems.flatMap((it) => it.result.ok ? [it.result.value] : []);
3860
+ const okValues = [];
3861
+ for (const it of okItems) okValues.push(valueOf(it));
3750
3862
  const numbers = () => {
3751
3863
  const bad = okValues.find((x) => typeof x !== "number");
3752
3864
  return bad === void 0 ? ok(okValues) : this.#err("kind-mismatch", `"${op}" needs numbers, but an item is a ${describeType(bad)}`, f, path, { op });
@@ -3770,19 +3882,36 @@ var Compiler = class {
3770
3882
  case "drop": {
3771
3883
  const n = whole();
3772
3884
  if (!n.ok) return n;
3773
- return ok(vexList(op === "take" ? okItems.slice(0, n.value) : okItems.slice(n.value)));
3885
+ return ok(checkedList(op === "take" ? okItems.slice(0, n.value) : okItems.slice(n.value)));
3774
3886
  }
3775
3887
  case "append": {
3776
3888
  const more = other();
3777
- return more.ok ? ok(vexList([...okItems, ...more.value])) : more;
3889
+ return more.ok ? ok(checkedList([...okItems, ...more.value])) : more;
3778
3890
  }
3779
3891
  case "without": {
3780
3892
  const more = other();
3781
3893
  if (!more.ok) return more;
3782
3894
  const gone = new Set(more.value.map((it) => it.key));
3783
- return ok(vexList(okItems.filter((it) => !gone.has(it.key))));
3895
+ return ok(checkedList(okItems.filter((it) => !gone.has(it.key))));
3784
3896
  }
3785
3897
  case "flat": return this.#flat(okValues, strict, f, path);
3898
+ case "distinct": {
3899
+ const seen = /* @__PURE__ */ new Set();
3900
+ const out = [];
3901
+ for (let i = 0; i < okValues.length; i++) {
3902
+ const x = okValues[i];
3903
+ if (!isIndexValue(x)) return this.#err("kind-mismatch", `"distinct" needs numbers, texts or booleans, but an item is a ${describeType(x)}`, f, path, { op });
3904
+ if (Number.isNaN(x) || !seen.has(x)) out.push(okItems[i]);
3905
+ seen.add(x);
3906
+ }
3907
+ return ok(checkedList(out));
3908
+ }
3909
+ case "join": {
3910
+ const sep = rest[0];
3911
+ if (typeof sep !== "string") return this.#err("kind-mismatch", `"join" needs a text separator, not a ${describeType(sep)}`, f, path, { op });
3912
+ const bad = okValues.find((x) => typeof x !== "string");
3913
+ return bad === void 0 ? ok(okValues.join(sep)) : this.#err("kind-mismatch", `"join" needs texts, but an item is a ${describeType(bad)}`, f, path, { op });
3914
+ }
3786
3915
  case "indexOf": {
3787
3916
  const x = rest[0];
3788
3917
  if (!isIndexValue(x)) return this.#err("kind-mismatch", `"indexOf" needs a number, a text or a boolean, not a ${describeType(x)}`, f, path, { op });
@@ -3843,6 +3972,38 @@ var Compiler = class {
3843
3972
  return index.values.has(key) ? ok(index.values.get(key)) : this.#err("missing-field", `the list has no item with the key ${JSON.stringify(key)}`, f, path, { op: "get" });
3844
3973
  }
3845
3974
  /**
3975
+ * This method gives the groups of a list (LIST.GROUP). The item at a position has the key at the same position of
3976
+ * `keys`, so the two lists must have the same number of items. A pair with an error item or an error key is not in a
3977
+ * group (LIST.LENIENT). Each key gives one item, in the order of its first pair, and its value is a list of its items.
3978
+ */
3979
+ #group(list, keys, strict, f, path) {
3980
+ const good = goodItems(list, strict);
3981
+ if (!good.ok) return good;
3982
+ const listed = asList(keys, this.#missing(f, path));
3983
+ if (listed === void 0) return this.#err("kind-mismatch", `"group" needs a list of keys as its second argument, not a ${describeType(keys)}`, f, path, { op: "group" });
3984
+ const goodKeys = goodItems(listed, strict);
3985
+ if (!goodKeys.ok) return goodKeys;
3986
+ const { items } = list;
3987
+ const keyItems = listed.items;
3988
+ if (keyItems.length !== items.length) return this.#err("kind-mismatch", `"group" needs one key for each item, but the list has ${String(items.length)} items and ${String(keyItems.length)} keys`, f, path, { op: "group" });
3989
+ const groups = /* @__PURE__ */ new Map();
3990
+ for (let i = 0; i < items.length; i++) {
3991
+ const it = items[i];
3992
+ const k = keyItems[i].result;
3993
+ if (!it.result.ok || !k.ok) continue;
3994
+ if (typeof k.value !== "string") return this.#err("kind-mismatch", `"group" needs text keys, but a key is a ${describeType(k.value)}`, f, path, { op: "group" });
3995
+ const g = groups.get(k.value);
3996
+ if (g === void 0) groups.set(k.value, [it]);
3997
+ else g.push(it);
3998
+ }
3999
+ const out = [];
4000
+ for (const [key, g] of groups) out.push({
4001
+ key,
4002
+ result: ok(checkedList(g))
4003
+ });
4004
+ return ok(checkedList(out));
4005
+ }
4006
+ /**
3846
4007
  * This method joins the lists that are the values of the items of a list, in order (LIST.FLAT). Each list follows the
3847
4008
  * rules of LIST.BUILD. A value that is not a list gives `#VALUE!`.
3848
4009
  */
@@ -3855,7 +4016,7 @@ var Compiler = class {
3855
4016
  if (!good.ok) return good;
3856
4017
  out.push(...good.value);
3857
4018
  }
3858
- return ok(vexList(out));
4019
+ return ok(checkedList(out));
3859
4020
  }
3860
4021
  /**
3861
4022
  * This method sorts the items of a list by their sort values, from the smallest (LIST.SORT). The sort is stable. The
@@ -3878,7 +4039,7 @@ var Compiler = class {
3878
4039
  const b = keys[j];
3879
4040
  return a < b ? -1 : a > b ? 1 : 0;
3880
4041
  });
3881
- return ok(vexList(order.map((i) => items[i])));
4042
+ return ok(checkedList(order.map((i) => items[i])));
3882
4043
  }
3883
4044
  /**
3884
4045
  * This method gives the sort value of each item for the option `by` of `sortBy` (LIST.SORT.BY). The op of the name
@@ -3894,7 +4055,7 @@ var Compiler = class {
3894
4055
  if (op !== void 0) break;
3895
4056
  }
3896
4057
  if (op === void 0) return this.#err("unknown-op", `no domain gives "${by}" for a ${describeType(x)}`, f, path, { op: by });
3897
- const r = this.#call(op, by, [], f, path);
4058
+ const r = this.#callResolved(op, x, by, [], f, path);
3898
4059
  if (!r.ok) return r;
3899
4060
  out.push(r.value);
3900
4061
  }
@@ -3923,7 +4084,7 @@ var Compiler = class {
3923
4084
  const d = this.#domains.find((dom) => safeIs(dom, acc) && resolveOp(dom, acc, opName) !== void 0);
3924
4085
  const op = d === void 0 ? void 0 : resolveOp(d, acc, opName);
3925
4086
  if (op === void 0) return this.#err("unknown-op", `no domain gives "${opName}" for a ${describeType(acc)}`, f, path, { op: opName });
3926
- const r = this.#call(op, opName, [values[i]], f, path);
4087
+ const r = this.#callResolved(op, acc, opName, [values[i]], f, path);
3927
4088
  if (!r.ok) return r;
3928
4089
  acc = r.value;
3929
4090
  }
@@ -3939,19 +4100,39 @@ const WITH_ARGUMENT = /* @__PURE__ */ new Set([
3939
4100
  "append",
3940
4101
  "without",
3941
4102
  "get",
3942
- "indexOf"
4103
+ "indexOf",
4104
+ "group",
4105
+ "join"
3943
4106
  ]);
3944
4107
  /**
3945
4108
  * This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
3946
4109
  * error comes first (EVAL.DEPTH). With `strict`, the first error item is the result.
3947
4110
  */
3948
4111
  function goodItems(list, strict) {
3949
- const depth = list.items.find((it) => !it.result.ok && isDepthError(it.result.error))?.result;
3950
- if (depth !== void 0 && !depth.ok) return fail(depth.error);
3951
- const firstError = list.items.find((it) => !it.result.ok)?.result;
3952
- if (strict && firstError !== void 0 && !firstError.ok) return fail(firstError.error);
3953
- return ok(list.items.filter((it) => it.result.ok));
4112
+ const { items } = list;
4113
+ let first;
4114
+ for (const it of items) {
4115
+ if (it.result.ok) continue;
4116
+ if (isDepthError(it.result.error)) return fail(it.result.error);
4117
+ first ??= it.result.error;
4118
+ }
4119
+ if (first === void 0) return ok(items);
4120
+ if (strict) return fail(first);
4121
+ const out = [];
4122
+ for (const it of items) if (it.result.ok) out.push(it);
4123
+ return ok(out);
4124
+ }
4125
+ /**
4126
+ * This function makes a list from the items of lists that passed the check of LIST.SHAPE, or from items of the same
4127
+ * shape. Thus the list skips the check of `isVexList`.
4128
+ */
4129
+ function checkedList(items) {
4130
+ const list = vexList(items);
4131
+ CHECKED.add(list);
4132
+ return list;
3954
4133
  }
4134
+ /** This function gives the value of an item with a value. */
4135
+ const valueOf = (it) => it.result.value;
3955
4136
  /** This function makes the index of the keys of a list. An error item is not an item of its key (LIST.LENIENT). */
3956
4137
  function keyIndexOf(list) {
3957
4138
  const values = /* @__PURE__ */ new Map();
@@ -3976,8 +4157,16 @@ const FORBIDDEN_FIELDS = /* @__PURE__ */ new Set([
3976
4157
  ]);
3977
4158
  /** This function tells if a segment of a path can name a field. A compiler uses the same rule as the interpreter. */
3978
4159
  const isFieldName = (seg) => seg.length > 0 && !FORBIDDEN_FIELDS.has(seg);
3979
- /** This function runs the `is` test of a domain. A test that throws counts as `false`. */
4160
+ /**
4161
+ * This function tells if a domain accepts a value. A domain with a primitive type accepts each value of that type and
4162
+ * no other value (KIND.PRIMITIVE), so its test `is` does not start. For another domain, a test that throws counts as
4163
+ * `false`.
4164
+ */
3980
4165
  function safeIs(d, value) {
4166
+ return d.primitive !== void 0 ? typeof value === d.primitive : testIs(d, value);
4167
+ }
4168
+ /** This function starts the `is` test of a domain. A test that throws counts as `false`. */
4169
+ function testIs(d, value) {
3981
4170
  try {
3982
4171
  return d.is(value);
3983
4172
  } catch {
@@ -5541,6 +5730,15 @@ var ListChainImpl = class ListChainImpl {
5541
5730
  flat(opts) {
5542
5731
  return this.#list("flat", [], opts);
5543
5732
  }
5733
+ distinct(opts) {
5734
+ return this.#list("distinct", [], opts);
5735
+ }
5736
+ group(keys, opts) {
5737
+ return this.#list("group", [listProgram("group", keys)], opts);
5738
+ }
5739
+ join(sep, opts) {
5740
+ return this.#op("join", [typeof sep === "string" ? lit(sep) : sep.program], opts);
5741
+ }
5544
5742
  each(body, opts) {
5545
5743
  const inner = {
5546
5744
  ...this.#s,