@mark1russell7/vex-domains 0.17.2 → 0.18.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/src/insets.ts ADDED
@@ -0,0 +1,79 @@
1
+ import { defineDomain, type Domain, type FnOp } from "@mark1russell7/vex";
2
+ import { Vec2 } from "./vec2.ts";
3
+ import type { XY } from "./xy.ts";
4
+
5
+ /**
6
+ * The four sides of an inset, as plain data. The `start` sides are the leading pair, and the `end` sides are the
7
+ * trailing pair. Each pair is in the order of a 2D vector: `x`, then `y`. The names are logical, so a side does not
8
+ * depend on the direction of an axis.
9
+ */
10
+ export interface Insets {
11
+ readonly startX: number;
12
+ readonly startY: number;
13
+ readonly endX: number;
14
+ readonly endY: number;
15
+ }
16
+
17
+ type InsetsNum1 = (a: Insets) => number;
18
+ type InsetsXY1 = (a: Insets) => XY;
19
+ type InsetsVec1 = (a: Insets) => Vec2;
20
+
21
+ /** The ops of `InsetsDomain`. */
22
+ export type InsetsOps = {
23
+ readonly startX: FnOp<Insets, InsetsNum1>;
24
+ readonly startY: FnOp<Insets, InsetsNum1>;
25
+ readonly endX: FnOp<Insets, InsetsNum1>;
26
+ readonly endY: FnOp<Insets, InsetsNum1>;
27
+ readonly start: FnOp<Insets, InsetsXY1>;
28
+ readonly end: FnOp<Insets, InsetsXY1>;
29
+ readonly startVec2: FnOp<Insets, InsetsVec1>;
30
+ readonly endVec2: FnOp<Insets, InsetsVec1>;
31
+ readonly add: FnOp<Insets, (a: Insets, b: Insets) => Insets> & { readonly liftScalar: true };
32
+ readonly scale: FnOp<Insets, (a: Insets, k: number) => Insets>;
33
+ };
34
+
35
+ /**
36
+ * The domain of insets: an object with the four number fields of `Insets` and no other field. The op `insets` of
37
+ * `NumDomain` makes one, and a number argument of `add` lifts to four equal sides. `start` and `end` give plain `XY`
38
+ * values, so a module can copy them. `startVec2` and `endVec2` give `Vec2` values, and a module cannot copy them. Put
39
+ * `InsetsDomain` before `NumRecordDomain`, because a record of numbers accepts the same objects.
40
+ */
41
+ export const InsetsDomain: Domain<"Insets", Insets, InsetsOps> = defineDomain<"Insets", Insets, InsetsOps>({
42
+ name: "Insets",
43
+ // LOOP.SIZE.OPS: each value has a fixed size, so each op has a size rule.
44
+ size: "fixed",
45
+ fields: ["startX", "startY", "endX", "endY"],
46
+ // A plain object: its prototype is Object.prototype or null, so an instance of a class is not a value of this domain.
47
+ is: (u: unknown): u is Insets => {
48
+ if (typeof u !== "object" || u === null || ![Object.prototype, null].includes(Object.getPrototypeOf(u) as object | null) || Object.keys(u).length !== 4) return false;
49
+ const v = u as Insets;
50
+ return typeof v.startX === "number" && typeof v.startY === "number" && typeof v.endX === "number" && typeof v.endY === "number";
51
+ },
52
+ fromScalar: (n: number): Insets => ({ startX: n, startY: n, endX: n, endY: n }),
53
+ valid: (v: Insets): boolean => Number.isFinite(v.startX) && Number.isFinite(v.startY) && Number.isFinite(v.endX) && Number.isFinite(v.endY),
54
+ show: (v: Insets): string => `(${v.startX}, ${v.startY}, ${v.endX}, ${v.endY})`,
55
+ samples: (): readonly Insets[] => [
56
+ { startX: 0, startY: 0, endX: 0, endY: 0 },
57
+ { startX: 1.5, startY: -2, endX: 3, endY: 4 },
58
+ { startX: 8, startY: 8, endX: 8, endY: 8 },
59
+ ],
60
+ ops: {
61
+ startX: { fn: (a: Insets): number => a.startX, params: [], returns: "number" },
62
+ startY: { fn: (a: Insets): number => a.startY, params: [], returns: "number" },
63
+ endX: { fn: (a: Insets): number => a.endX, params: [], returns: "number" },
64
+ endY: { fn: (a: Insets): number => a.endY, params: [], returns: "number" },
65
+ start: { fn: (a: Insets): XY => ({ x: a.startX, y: a.startY }), params: [], returns: "domain:XY" },
66
+ end: { fn: (a: Insets): XY => ({ x: a.endX, y: a.endY }), params: [], returns: "domain:XY" },
67
+ startVec2: { fn: (a: Insets): Vec2 => new Vec2(a.startX, a.startY), params: [], returns: "domain:Vec2" },
68
+ endVec2: { fn: (a: Insets): Vec2 => new Vec2(a.endX, a.endY), params: [], returns: "domain:Vec2" },
69
+ add: {
70
+ fn: (a: Insets, b: Insets): Insets => ({ startX: a.startX + b.startX, startY: a.startY + b.startY, endX: a.endX + b.endX, endY: a.endY + b.endY }),
71
+ laws: ["commutative", "associative"],
72
+ identity: (): Insets => ({ startX: 0, startY: 0, endX: 0, endY: 0 }),
73
+ liftScalar: true,
74
+ params: ["domain"],
75
+ returns: "domain",
76
+ },
77
+ scale: { fn: (a: Insets, k: number): Insets => ({ startX: a.startX * k, startY: a.startY * k, endX: a.endX * k, endY: a.endY * k }), params: ["number"], returns: "domain" },
78
+ },
79
+ });
package/src/json.ts CHANGED
@@ -52,9 +52,10 @@ export const JsonDomain: Domain<"Json", JsonValue, JsonOps> = defineDomain<"Json
52
52
  [Object.prototype, null].includes(Object.getPrototypeOf(u) as object | null) &&
53
53
  !((u as { readonly kind?: unknown }).kind === "vex.list" && Array.isArray((u as { readonly items?: unknown }).items))),
54
54
  samples: (): readonly JsonValue[] => [{ a: 1, b: [true, null], c: { d: "x" } }, [1, "x", null, [2]], "x", 2, false],
55
+ // LOOP.SIZE.OPS: each op gives a part of its value, or a list with one item for each field or leaf of its value.
55
56
  ops: {
56
57
  // Each op reads no name outside itself, so a module can copy its text.
57
- kind: { fn: (v: JsonValue): JsonKind => (Array.isArray(v) ? "array" : (typeof v as JsonKind)), params: [], returns: "string" },
58
+ kind: { fn: (v: JsonValue): JsonKind => (Array.isArray(v) ? "array" : (typeof v as JsonKind)), params: [], returns: "string", size: "fixed" },
58
59
  kinds: {
59
60
  fn: (c: JsonValue): readonly JsonItemKind[] => {
60
61
  if (typeof c !== "object") throw new Error(`"kinds" needs an array or an object, not a ${typeof c}`);
@@ -67,6 +68,7 @@ export const JsonDomain: Domain<"Json", JsonValue, JsonOps> = defineDomain<"Json
67
68
  });
68
69
  },
69
70
  params: [],
71
+ size: { size: ["scaled"], keys: "position" },
70
72
  },
71
73
  fields: {
72
74
  fn: (o: JsonValue): readonly string[] => {
@@ -74,6 +76,7 @@ export const JsonDomain: Domain<"Json", JsonValue, JsonOps> = defineDomain<"Json
74
76
  return Object.keys(o);
75
77
  },
76
78
  params: [],
79
+ size: { size: [1], keys: "position" },
77
80
  },
78
81
  field: {
79
82
  fn: (o: JsonValue, k: unknown): unknown => {
@@ -86,8 +89,13 @@ export const JsonDomain: Domain<"Json", JsonValue, JsonOps> = defineDomain<"Json
86
89
  },
87
90
  params: ["any"],
88
91
  returns: "any",
92
+ // LOOP.SIZE.PART: a field of an object is an item of the object, and the op takes no other value.
93
+ size: {
94
+ size: [1, 0],
95
+ byParts: { size: [[1, "item"], 0], count: [[1, "item"], 0], inner: [[1, "item"], 0], item: [[1, "part"], 0], part: [[1, "part"], 0] },
96
+ },
89
97
  },
90
- // The walk of the schema of Graph as a flat list (JSON.LEAVES). The function reads no name outside itself, so a
98
+ // The walk of a schema as a flat list (JSON.LEAVES). The function reads no name outside itself, so a
91
99
  // module can copy it. A stack of the objects, not recursion, keeps the order of a recursive walk.
92
100
  leaves: {
93
101
  fn: (o: JsonValue, depth: number): readonly JsonLeaf[] => {
@@ -115,6 +123,9 @@ export const JsonDomain: Domain<"Json", JsonValue, JsonOps> = defineDomain<"Json
115
123
  return Object.freeze(out);
116
124
  },
117
125
  params: ["number"],
126
+ // A path repeats the keys above it, at most 100 levels, so the size of the list is the size of the object times a
127
+ // fixed factor.
128
+ size: { size: ["scaled", 0], keys: "position" },
118
129
  },
119
130
  },
120
131
  });
package/src/ndvector.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { defineDomain, type Domain, type FnOp, type OpTable } from "@mark1russell7/vex";
1
+ import { defineDomain, type Domain, type FnOp, type OpTable, type SizeRule } from "@mark1russell7/vex";
2
2
 
3
3
  /**
4
4
  * An immutable vector with named components. The type parameter `K` is the set of component names. Ops that
@@ -132,24 +132,30 @@ export type NDVectorOps = OpTable<NDVector, NDOpNames>;
132
132
  /** The ops of `LiftedNDVectorDomain`. */
133
133
  export type LiftedNDVectorOps = OpTable<NDVector, NDOpNames, "add" | "subtract" | "multiply" | "dot">;
134
134
 
135
+ /** The size rule of an op whose result has the keys of both vectors, each key one time (LOOP.SIZE.OPS). */
136
+ const KEYS = { size: [1, 1], union: true } as const satisfies SizeRule;
137
+
138
+ /** The size rule of an op that lifts a number to the vector `{ scalar: n }`: the keys of both, and maybe one key more. */
139
+ const LIFTED = { size: [1, 1], union: true, constant: true } as const satisfies SizeRule;
140
+
135
141
  const ndOps = {
136
- add: { laws: ["commutative", "associative"], identity: (): NDVector => NDVector.zero(), params: ["domain"] },
137
- subtract: { params: ["domain"] },
138
- multiply: { laws: ["commutative", "associative"], params: ["domain"] },
139
- min: { laws: ["commutative", "associative", "idempotent"], params: ["domain"] },
140
- max: { laws: ["commutative", "associative", "idempotent"], params: ["domain"] },
141
- mergePreferNonZero: { laws: ["associative", "idempotent"], params: ["domain"] },
142
- scale: { params: ["number"] },
143
- clamp: { params: ["number", "number"] },
144
- dot: { laws: ["commutative"], params: ["domain"] },
145
- length: {},
146
- norm1: {},
147
- normInf: {},
148
- equals: { laws: ["commutative"], params: ["domain"] },
149
- pick: {},
150
- withKeys: {},
151
- get: { params: ["string"] },
152
- set: { params: ["string", "number"] },
142
+ add: { laws: ["commutative", "associative"], identity: (): NDVector => NDVector.zero(), params: ["domain"], size: KEYS },
143
+ subtract: { params: ["domain"], size: KEYS },
144
+ multiply: { laws: ["commutative", "associative"], params: ["domain"], size: KEYS },
145
+ min: { laws: ["commutative", "associative", "idempotent"], params: ["domain"], size: KEYS },
146
+ max: { laws: ["commutative", "associative", "idempotent"], params: ["domain"], size: KEYS },
147
+ mergePreferNonZero: { laws: ["associative", "idempotent"], params: ["domain"], size: KEYS },
148
+ scale: { params: ["number"], size: { size: [1, 0] } },
149
+ clamp: { params: ["number", "number"], size: { size: [1, 0, 0] } },
150
+ dot: { laws: ["commutative"], params: ["domain"], size: "scalar" },
151
+ length: { size: "scalar" },
152
+ norm1: { size: "scalar" },
153
+ normInf: { size: "scalar" },
154
+ equals: { laws: ["commutative"], params: ["domain"], size: "scalar" },
155
+ pick: { size: { size: [1, 1, 0], union: true } },
156
+ withKeys: { size: KEYS },
157
+ get: { params: ["string"], size: "scalar" },
158
+ set: { params: ["string", "number"], size: { size: [1, 1, 0], union: true } },
153
159
  } as const satisfies NDVectorOps;
154
160
 
155
161
  const isNDVector = (u: unknown): u is NDVector => u instanceof NDVector;
@@ -178,9 +184,9 @@ export const LiftedNDVectorDomain: Domain<"NDVector(lifted)", NDVector, LiftedND
178
184
  show: (v: NDVector): string => v.toString(),
179
185
  ops: {
180
186
  ...ndOps,
181
- add: { ...ndOps.add, liftScalar: true },
182
- subtract: { ...ndOps.subtract, liftScalar: true },
183
- multiply: { ...ndOps.multiply, liftScalar: true },
187
+ add: { ...ndOps.add, liftScalar: true, size: LIFTED },
188
+ subtract: { ...ndOps.subtract, liftScalar: true, size: LIFTED },
189
+ multiply: { ...ndOps.multiply, liftScalar: true, size: LIFTED },
184
190
  dot: { ...ndOps.dot, liftScalar: true },
185
191
  },
186
192
  });
@@ -205,6 +211,6 @@ export const NumRecordDomain: Domain<"NumRecord", NumRecord, NumRecordOps> = def
205
211
  name: "NumRecord",
206
212
  is: isNumRecord,
207
213
  ops: {
208
- toNDVector: { fn: (r: NumRecord): NDVector => new NDVector(r) },
214
+ toNDVector: { fn: (r: NumRecord): NDVector => new NDVector(r), size: { size: [1] } },
209
215
  },
210
216
  });
package/src/scalars.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { defineDomain, type Domain, type FnOp } from "@mark1russell7/vex";
2
2
  import { LocalDate } from "./date.ts";
3
+ import type { Insets } from "./insets.ts";
3
4
  import { Vec2 } from "./vec2.ts";
4
5
  import type { XY } from "./xy.ts";
5
6
 
@@ -21,6 +22,7 @@ export type NumOps = {
21
22
  readonly clamp: FnOp<number, NumFn3>;
22
23
  readonly abs: FnOp<number, NumFn1>;
23
24
  readonly negate: FnOp<number, NumFn1>;
25
+ readonly sign: FnOp<number, NumFn1>;
24
26
  readonly sqrt: FnOp<number, NumFn1>;
25
27
  readonly ln: FnOp<number, NumFn1>;
26
28
  readonly exp: FnOp<number, NumFn1>;
@@ -32,6 +34,7 @@ export type NumOps = {
32
34
  readonly date: FnOp<number, (year: number, month: number, day: number) => LocalDate>;
33
35
  readonly vec2: FnOp<number, (x: number, y: number) => Vec2>;
34
36
  readonly xy: FnOp<number, (x: number, y: number) => XY>;
37
+ readonly insets: FnOp<number, (startX: number, startY: number, endX: number, endY: number) => Insets>;
35
38
  readonly floor: FnOp<number, NumFn1>;
36
39
  readonly ceil: FnOp<number, NumFn1>;
37
40
  readonly round: FnOp<number, NumFn1>;
@@ -44,23 +47,29 @@ export type NumOps = {
44
47
 
45
48
  /** The domain of numbers. A result that is not a finite number is the error `#NUM!`. */
46
49
  export const NumDomain: Domain<"Num", number, NumOps> = defineDomain<"Num", number, NumOps>({
50
+ // COMPILE.MODULE.FORM: a vector in a module is plain data, so `vec2` needs no class there.
51
+ moduleForm: { ops: { vec2: { fn: (x: number, y: number): { readonly x: number; readonly y: number } => ({ x, y }), returns: "domain:Vec2" } } },
47
52
  name: "Num",
53
+ // LOOP.SIZE.OPS: each value has a fixed size, so each op has a size rule.
54
+ size: "fixed",
48
55
  is: (u: unknown): u is number => typeof u === "number",
49
56
  primitive: "number",
50
57
  valid: (n: number): boolean => Number.isFinite(n),
51
58
  show: (n: number): string => String(n),
52
59
  ops: {
53
- add: { fn: (a: number, b: number): number => a + b, laws: ["commutative", "associative"], identity: (): number => 0, params: ["number"], returns: "number" },
54
- subtract: { fn: (a: number, b: number): number => a - b, params: ["number"], returns: "number" },
60
+ add: { fn: (a: number, b: number): number => a + b, laws: ["commutative", "associative"], identity: (): number => 0, params: ["number"], returns: "number", monotone: ["up", "up"] },
61
+ subtract: { fn: (a: number, b: number): number => a - b, params: ["number"], returns: "number", monotone: ["up", "down"] },
55
62
  multiply: { fn: (a: number, b: number): number => a * b, laws: ["commutative", "associative"], identity: (): number => 1, params: ["number"], returns: "number" },
56
63
  divide: { fn: (a: number, b: number): number => a / b, params: ["number"], returns: "number" },
57
64
  mod: { fn: (a: number, b: number): number => a % b, params: ["number"], returns: "number" },
58
65
  pow: { fn: (a: number, b: number): number => a ** b, params: ["number"], returns: "number" },
59
- min: { fn: (a: number, b: number): number => Math.min(a, b), laws: ["commutative", "associative", "idempotent"], params: ["number"], returns: "number" },
60
- max: { fn: (a: number, b: number): number => Math.max(a, b), laws: ["commutative", "associative", "idempotent"], params: ["number"], returns: "number" },
61
- clamp: { fn: (a: number, lo: number, hi: number): number => Math.min(hi, Math.max(lo, a)), params: ["number", "number"], returns: "number" },
66
+ min: { fn: (a: number, b: number): number => Math.min(a, b), laws: ["commutative", "associative", "idempotent"], params: ["number"], returns: "number", monotone: ["up", "up"] },
67
+ max: { fn: (a: number, b: number): number => Math.max(a, b), laws: ["commutative", "associative", "idempotent"], params: ["number"], returns: "number", monotone: ["up", "up"] },
68
+ clamp: { fn: (a: number, lo: number, hi: number): number => Math.min(hi, Math.max(lo, a)), params: ["number", "number"], returns: "number", monotone: ["up", "up", "up"] },
62
69
  abs: { fn: (a: number): number => Math.abs(a), returns: "number" },
63
- negate: { fn: (a: number): number => -a, returns: "number" },
70
+ negate: { fn: (a: number): number => -a, returns: "number", monotone: ["down"] },
71
+ // NUM.SIGN: the check of the result turns NaN into #NUM!, and an infinite number has a sign.
72
+ sign: { fn: (a: number): number => Math.sign(a), returns: "number", monotone: ["up"] },
64
73
  sqrt: { fn: (a: number): number => Math.sqrt(a), returns: "number" },
65
74
  ln: { fn: (a: number): number => Math.log(a), returns: "number" },
66
75
  exp: { fn: (a: number): number => Math.exp(a), returns: "number" },
@@ -76,9 +85,11 @@ export const NumDomain: Domain<"Num", number, NumOps> = defineDomain<"Num", numb
76
85
  vec2: { fn: (x: number, y: number): Vec2 => new Vec2(x, y), params: ["number"] },
77
86
  // The same as plain data, for a standalone module: xy(x, y). The result is a value of XYDomain.
78
87
  xy: { fn: (x: number, y: number): XY => ({ x, y }), params: ["number"], returns: "domain:XY" },
79
- floor: { fn: (a: number): number => Math.floor(a), returns: "number" },
80
- ceil: { fn: (a: number): number => Math.ceil(a), returns: "number" },
81
- round: { fn: (a: number): number => Math.round(a), returns: "number" },
88
+ // The leading sides, then the trailing sides: insets(startX, startY, endX, endY). The result is a value of InsetsDomain.
89
+ insets: { fn: (startX: number, startY: number, endX: number, endY: number): Insets => ({ startX, startY, endX, endY }), params: ["number", "number", "number"], returns: "domain:Insets" },
90
+ floor: { fn: (a: number): number => Math.floor(a), returns: "number", monotone: ["up"] },
91
+ ceil: { fn: (a: number): number => Math.ceil(a), returns: "number", monotone: ["up"] },
92
+ round: { fn: (a: number): number => Math.round(a), returns: "number", monotone: ["up"] },
82
93
  gt: { fn: (a: number, b: number): boolean => a > b, params: ["number"], returns: "boolean" },
83
94
  gte: { fn: (a: number, b: number): boolean => a >= b, params: ["number"], returns: "boolean" },
84
95
  lt: { fn: (a: number, b: number): boolean => a < b, params: ["number"], returns: "boolean" },
@@ -102,14 +113,16 @@ export type BoolOps = {
102
113
  /** The domain of booleans. For lazy logic, use the special forms `and`, `or` and `if` of the IR. */
103
114
  export const BoolDomain: Domain<"Bool", boolean, BoolOps> = defineDomain<"Bool", boolean, BoolOps>({
104
115
  name: "Bool",
116
+ // LOOP.SIZE.OPS: each value has a fixed size, so each op has a size rule.
117
+ size: "fixed",
105
118
  is: (u: unknown): u is boolean => typeof u === "boolean",
106
119
  primitive: "boolean",
107
120
  show: (b: boolean): string => String(b),
108
121
  ops: {
109
- and: { fn: (a: boolean, b: boolean): boolean => a && b, laws: ["commutative", "associative", "idempotent"], identity: (): boolean => true, params: ["boolean"], returns: "boolean" },
110
- or: { fn: (a: boolean, b: boolean): boolean => a || b, laws: ["commutative", "associative", "idempotent"], identity: (): boolean => false, params: ["boolean"], returns: "boolean" },
122
+ and: { fn: (a: boolean, b: boolean): boolean => a && b, laws: ["commutative", "associative", "idempotent"], identity: (): boolean => true, params: ["boolean"], returns: "boolean", monotone: ["up", "up"] },
123
+ or: { fn: (a: boolean, b: boolean): boolean => a || b, laws: ["commutative", "associative", "idempotent"], identity: (): boolean => false, params: ["boolean"], returns: "boolean", monotone: ["up", "up"] },
111
124
  xor: { fn: (a: boolean, b: boolean): boolean => a !== b, laws: ["commutative", "associative"], identity: (): boolean => false, params: ["boolean"], returns: "boolean" },
112
- not: { fn: (a: boolean): boolean => !a, returns: "boolean" },
125
+ not: { fn: (a: boolean): boolean => !a, returns: "boolean", monotone: ["down"] },
113
126
  eq: { fn: (a: boolean, b: boolean): boolean => a === b, laws: ["commutative"], params: ["boolean"], returns: "boolean" },
114
127
  },
115
128
  });
package/src/schema.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * The schema program (EXAMPLE.SCHEMA). It reads a list of JSON documents. For each field path, it gives the distinct
3
- * kinds of the values and the number of documents with the path. The app Graph shows this schema for a sample of a collection.
3
+ * kinds of the values and the number of documents with the path.
4
4
  *
5
5
  * The walk `leaves(d, 3)` goes into an object that is not empty at a depth under 3. It records an empty object, or an
6
6
  * object at the depth 3, with the kind `object`. It does not go into an array. The kind of an array is `array<k1|k2>`,
@@ -21,6 +21,7 @@ export interface JsonFieldSchema {
21
21
  /** These are the domains of `SCHEMA_PROGRAM`. `JsonDomain` is last, so `eq`, `concat` and `gt` go to the domains of numbers and texts. */
22
22
  export const SCHEMA_DOMAINS: readonly AnyDomain[] = [NumDomain, BoolDomain, TextDomain, JsonDomain];
23
23
 
24
+ // Stryker disable next-line ArrayDeclaration: the added item is a text with no field, so its row is an error, and flat skips an error item (LIST.BUILD), so the list gives no notes, as the empty list does
24
25
  const EMPTY = lit(vexList([]));
25
26
  /** A loop over the items of a list, with the names of the item and of its position (AXIS.LIST.ITEM). */
26
27
  const loop = (list: Expr, as: ItemNames, body: Expr): Expr => each(axes.list(list), body, undefined, as);
@@ -36,6 +37,7 @@ const notes = app(
36
37
  loop(
37
38
  v("docs"),
38
39
  ["d", "i"],
40
+ // Stryker disable next-line BooleanLiteral: flat skips an error item (LIST.BUILD), so a document without a kind gives no notes with true too
39
41
  loop(app("if", app("ifError", app("eq", app("kind", v("d")), lit("object")), lit(false)), app("leaves", v("d"), lit(3)), EMPTY), ["l"], rec({ path: field(v("l"), "path"), kind: app("if", app("eq", field(v("l"), "kind"), lit("array")), call("arrayKind", field(v("l"), "value")), field(v("l"), "kind")), doc: v("i") })),
40
42
  ),
41
43
  );
package/src/text.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * The domain of texts. The op `concat` joins a text and a value of any kind. It is the operator `&` of a
3
3
  * spreadsheet, and of the formula language of the sheet app.
4
4
  */
5
- import { defineDomain, type Domain, type FnOp } from "@mark1russell7/vex";
5
+ import { defineDomain, type Domain, type FnOp, type VexList } from "@mark1russell7/vex";
6
6
 
7
7
  type TextFn1 = (a: string) => string;
8
8
 
@@ -10,6 +10,7 @@ type TextFn1 = (a: string) => string;
10
10
  export type TextOps = {
11
11
  readonly concat: FnOp<string, (a: string, b: unknown) => string>;
12
12
  readonly length: FnOp<string, (a: string) => number>;
13
+ readonly codePoints: FnOp<string, (a: string) => VexList<number>>;
13
14
  readonly upper: FnOp<string, TextFn1>;
14
15
  readonly lower: FnOp<string, TextFn1>;
15
16
  readonly trim: FnOp<string, TextFn1>;
@@ -29,13 +30,28 @@ export const TextDomain: Domain<"Text", string, TextOps> = defineDomain<"Text",
29
30
  is: (u: unknown): u is string => typeof u === "string",
30
31
  primitive: "string",
31
32
  show: (s: string): string => JSON.stringify(s),
33
+ // LOOP.SIZE.OPS: `concat` adds the JSON text of a value that is not a text. `upper` and `lower` can make a text
34
+ // longer, for example "ß" becomes "SS". The new letters do not change again, so a text grows at most three times in
35
+ // total, and not at each step.
32
36
  ops: {
33
- concat: { fn: (a: string, b: unknown): string => a + textOf(b), params: ["any"], returns: "string" },
37
+ concat: { fn: (a: string, b: unknown): string => a + textOf(b), params: ["any"], returns: "string", size: { size: [1, "text"] } },
34
38
  // The length counts UTF-16 code units, like the function LEN of a spreadsheet.
35
39
  length: { fn: (a: string): number => a.length, returns: "number" },
36
- upper: { fn: (a: string): string => a.toUpperCase(), returns: "string" },
37
- lower: { fn: (a: string): string => a.toLowerCase(), returns: "string" },
38
- trim: { fn: (a: string): string => a.trim(), returns: "string" },
40
+ // The list has one item for each code point, so a pair of surrogates is one item, and a lone surrogate is an item
41
+ // with its own value (TEXT.CODEPOINTS). The function reads no name of Vex, so a module can copy it. The rule tells
42
+ // that the count is at most the length. Each item has a number and a new key, so `keys` adds a part for each item.
43
+ codePoints: {
44
+ fn: (a: string): VexList<number> =>
45
+ Object.freeze({
46
+ kind: "vex.list",
47
+ items: Object.freeze(Array.from(a, (ch, i) => ({ key: String(i), result: { ok: true as const, value: ch.codePointAt(0) as number } }))),
48
+ }),
49
+ returns: "list:number",
50
+ size: { size: [1], inner: [0], item: [0], keys: "position" },
51
+ },
52
+ upper: { fn: (a: string): string => a.toUpperCase(), returns: "string", size: { size: [1] } },
53
+ lower: { fn: (a: string): string => a.toLowerCase(), returns: "string", size: { size: [1] } },
54
+ trim: { fn: (a: string): string => a.trim(), returns: "string", size: { size: [1] } },
39
55
  contains: { fn: (a: string, b: string): boolean => a.includes(b), params: ["string"], returns: "boolean" },
40
56
  eq: { fn: (a: string, b: string): boolean => a === b, laws: ["commutative"], params: ["string"], returns: "boolean" },
41
57
  },
@@ -0,0 +1,76 @@
1
+ import type { ModuleForm } from "@mark1russell7/vex";
2
+
3
+ /** A 2D vector in a standalone module: plain data with the fields of `Vec2`. */
4
+ interface Plain {
5
+ readonly x: number;
6
+ readonly y: number;
7
+ }
8
+
9
+ /**
10
+ * The form of `Vec2Domain` in a standalone module (COMPILE.MODULE.FORM). A module has no import, and a copy of the class
11
+ * is another class. Thus a vector in a module is plain data, the data of `toJSON` of a `Vec2`. Each op has the arithmetic
12
+ * of the method with its name, so it has the same bits. The test accepts a `Vec2` too, so a host can give its vectors.
13
+ */
14
+ export const VEC2_FORM: ModuleForm = {
15
+ is: (u: unknown): boolean => typeof u === "object" && u !== null && typeof (u as Plain).x === "number" && typeof (u as Plain).y === "number",
16
+ fromScalar: (n: number): Plain => ({ x: n, y: n }),
17
+ samples: (): readonly Plain[] => [{ x: 0, y: 0 }, { x: 1.5, y: -2 }, { x: 3, y: 4 }],
18
+ ops: {
19
+ add: { fn: (a: Plain, b: Plain): Plain => ({ x: a.x + b.x, y: a.y + b.y }), identity: (): Plain => ({ x: 0, y: 0 }) },
20
+ subtract: { fn: (a: Plain, b: Plain): Plain => ({ x: a.x - b.x, y: a.y - b.y }) },
21
+ multiply: { fn: (a: Plain, b: Plain): Plain => ({ x: a.x * b.x, y: a.y * b.y }), identity: (): Plain => ({ x: 1, y: 1 }) },
22
+ divide: { fn: (a: Plain, b: Plain): Plain => ({ x: a.x / b.x, y: a.y / b.y }) },
23
+ min: { fn: (a: Plain, b: Plain): Plain => ({ x: Math.min(a.x, b.x), y: Math.min(a.y, b.y) }) },
24
+ max: { fn: (a: Plain, b: Plain): Plain => ({ x: Math.max(a.x, b.x), y: Math.max(a.y, b.y) }) },
25
+ scale: { fn: (a: Plain, k: number): Plain => ({ x: a.x * k, y: a.y * k }) },
26
+ negate: { fn: (a: Plain): Plain => ({ x: -a.x, y: -a.y }) },
27
+ halve: { fn: (a: Plain): Plain => ({ x: a.x * 0.5, y: a.y * 0.5 }) },
28
+ abs: { fn: (a: Plain): Plain => ({ x: Math.abs(a.x), y: Math.abs(a.y) }) },
29
+ sign: { fn: (a: Plain): Plain => ({ x: Math.sign(a.x), y: Math.sign(a.y) }) },
30
+ round: { fn: (a: Plain): Plain => ({ x: Math.round(a.x), y: Math.round(a.y) }) },
31
+ floor: { fn: (a: Plain): Plain => ({ x: Math.floor(a.x), y: Math.floor(a.y) }) },
32
+ ceil: { fn: (a: Plain): Plain => ({ x: Math.ceil(a.x), y: Math.ceil(a.y) }) },
33
+ clamp: { fn: (a: Plain, lo: number, hi: number): Plain => ({ x: Math.min(hi, Math.max(lo, a.x)), y: Math.min(hi, Math.max(lo, a.y)) }) },
34
+ swap: { fn: (a: Plain): Plain => ({ x: a.y, y: a.x }) },
35
+ compose: { fn: (a: Plain, main: number, cross: number): Plain => ({ x: a.x * main + a.y * cross, y: a.y * main + a.x * cross }) },
36
+ select: { fn: (m: Plain, a: Plain, b: Plain): Plain => ({ x: m.x !== 0 ? a.x : b.x, y: m.y !== 0 ? a.y : b.y }) },
37
+ rotate: {
38
+ fn: (a: Plain, radians: number): Plain => {
39
+ const c = Math.cos(radians);
40
+ const s = Math.sin(radians);
41
+ return { x: c * a.x - s * a.y, y: s * a.x + c * a.y };
42
+ },
43
+ },
44
+ normalize: {
45
+ fn: (a: Plain): Plain => {
46
+ const len = Math.hypot(a.x, a.y);
47
+ return len === 0 ? { x: a.x, y: a.y } : { x: a.x * (1 / len), y: a.y * (1 / len) };
48
+ },
49
+ },
50
+ minAxis: { fn: (a: Plain): Plain => (Number.isNaN(a.x) || Number.isNaN(a.y) ? { x: Number.NaN, y: Number.NaN } : a.x <= a.y ? { x: 1, y: 0 } : { x: 0, y: 1 }) },
51
+ lerp: { fn: (a: Plain, o: Plain, t: number): Plain => ({ x: a.x + (o.x - a.x) * t, y: a.y + (o.y - a.y) * t }) },
52
+ length: { fn: (a: Plain): number => Math.hypot(a.x, a.y) },
53
+ lengthSq: { fn: (a: Plain): number => a.x * a.x + a.y * a.y },
54
+ distance: { fn: (a: Plain, o: Plain): number => Math.hypot(a.x - o.x, a.y - o.y) },
55
+ dot: { fn: (a: Plain, o: Plain): number => a.x * o.x + a.y * o.y },
56
+ along: { fn: (a: Plain, dir: Plain): number => a.x * dir.x + a.y * dir.y },
57
+ across: { fn: (a: Plain, dir: Plain): number => a.x * dir.y + a.y * dir.x },
58
+ cross: { fn: (a: Plain, o: Plain): number => a.x * o.y - a.y * o.x },
59
+ angle: { fn: (a: Plain): number => Math.atan2(a.y, a.x) },
60
+ area: { fn: (a: Plain): number => a.x * a.y },
61
+ sum: { fn: (a: Plain): number => a.x + a.y },
62
+ minComponent: { fn: (a: Plain): number => Math.min(a.x, a.y) },
63
+ maxComponent: { fn: (a: Plain): number => Math.max(a.x, a.y) },
64
+ equals: { fn: (a: Plain, o: Plain): boolean => a.x === o.x && a.y === o.y },
65
+ anyNonPositive: { fn: (a: Plain): boolean => a.x <= 0 || a.y <= 0 },
66
+ allPositive: { fn: (a: Plain): boolean => a.x > 0 && a.y > 0 },
67
+ anyNegative: { fn: (a: Plain): boolean => a.x < 0 || a.y < 0 },
68
+ allNonNegative: { fn: (a: Plain): boolean => a.x >= 0 && a.y >= 0 },
69
+ anyZero: { fn: (a: Plain): boolean => a.x === 0 || a.y === 0 },
70
+ allZero: { fn: (a: Plain): boolean => a.x === 0 && a.y === 0 },
71
+ anyLessThan: { fn: (a: Plain, n: number): boolean => a.x < n || a.y < n },
72
+ allLessThan: { fn: (a: Plain, n: number): boolean => a.x < n && a.y < n },
73
+ anyGreaterThan: { fn: (a: Plain, n: number): boolean => a.x > n || a.y > n },
74
+ allGreaterThan: { fn: (a: Plain, n: number): boolean => a.x > n && a.y > n },
75
+ },
76
+ };
package/src/vec2.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { defineDomain, type Domain, type FnOp, type OpTable } from "@mark1russell7/vex";
2
+ import { VEC2_FORM } from "./vec2-form.ts";
2
3
 
3
4
  /**
4
5
  * An immutable 2D vector. The methods live on the prototype, so a vector costs two numbers. Division is real
@@ -60,6 +61,10 @@ export class Vec2 {
60
61
  abs(): Vec2 {
61
62
  return new Vec2(Math.abs(this.x), Math.abs(this.y));
62
63
  }
64
+ /** The component-wise sign, as `Math.sign` gives it. A component `NaN` stays `NaN`, and the domain check gives `#NUM!`. */
65
+ sign(): Vec2 {
66
+ return new Vec2(Math.sign(this.x), Math.sign(this.y));
67
+ }
63
68
  round(): Vec2 {
64
69
  return new Vec2(Math.round(this.x), Math.round(this.y));
65
70
  }
@@ -100,6 +105,14 @@ export class Vec2 {
100
105
  const len = this.length();
101
106
  return len === 0 ? this : this.scale(1 / len);
102
107
  }
108
+ /**
109
+ * The unit vector of the smaller component: (1, 0) if `x <= y`, else (0, 1). The comparison uses the signed values, so
110
+ * `v.abs().minAxis()` compares sizes. A tie gives (1, 0). A component `NaN` gives `NaN` components, thus `#NUM!`.
111
+ */
112
+ minAxis(): Vec2 {
113
+ if (Number.isNaN(this.x) || Number.isNaN(this.y)) return new Vec2(Number.NaN, Number.NaN);
114
+ return this.x <= this.y ? new Vec2(1, 0) : new Vec2(0, 1);
115
+ }
103
116
  /** The linear interpolation from this vector to `o`. */
104
117
  lerp(o: Vec2, t: number): Vec2 {
105
118
  return new Vec2(this.x + (o.x - this.x) * t, this.y + (o.y - this.y) * t);
@@ -188,8 +201,8 @@ export class Vec2 {
188
201
  }
189
202
 
190
203
  type Vec2OpNames =
191
- | "add" | "subtract" | "multiply" | "divide" | "min" | "max" | "scale" | "negate" | "halve" | "abs" | "round"
192
- | "floor" | "ceil" | "clamp" | "swap" | "compose" | "select" | "rotate" | "normalize" | "lerp" | "length"
204
+ | "add" | "subtract" | "multiply" | "divide" | "min" | "max" | "scale" | "negate" | "halve" | "abs" | "sign" | "round"
205
+ | "floor" | "ceil" | "clamp" | "swap" | "compose" | "select" | "rotate" | "normalize" | "minAxis" | "lerp" | "length"
193
206
  | "lengthSq" | "distance" | "dot" | "along" | "across" | "cross" | "angle" | "area" | "sum" | "minComponent"
194
207
  | "maxComponent" | "equals" | "anyNonPositive" | "allPositive" | "anyNegative" | "allNonNegative" | "anyZero"
195
208
  | "allZero" | "anyLessThan" | "allLessThan" | "anyGreaterThan" | "allGreaterThan";
@@ -203,6 +216,8 @@ export type Vec2Ops = OpTable<Vec2, Vec2OpNames, "add" | "subtract" | "multiply"
203
216
  /** The domain of 2D vectors. A number argument of a component-wise op lifts to a vector with that value twice. */
204
217
  export const Vec2Domain: Domain<"Vec2", Vec2, Vec2Ops> = defineDomain<"Vec2", Vec2, Vec2Ops>({
205
218
  name: "Vec2",
219
+ // LOOP.SIZE.OPS: each value has a fixed size, so each op has a size rule.
220
+ size: "fixed",
206
221
  is: (u: unknown): u is Vec2 => u instanceof Vec2,
207
222
  fromScalar: (n: number): Vec2 => Vec2.scalar(n),
208
223
  valid: (v: Vec2): boolean => Number.isFinite(v.x) && Number.isFinite(v.y),
@@ -213,6 +228,8 @@ export const Vec2Domain: Domain<"Vec2", Vec2, Vec2Ops> = defineDomain<"Vec2", Ve
213
228
  const [x, y] = Array.isArray(u) ? (u as readonly unknown[]) : [];
214
229
  return new Vec2(typeof x === "number" ? x : Number.NaN, typeof y === "number" ? y : Number.NaN);
215
230
  },
231
+ // COMPILE.MODULE.FORM: a module cannot hold this class, so a vector in a module is plain data.
232
+ moduleForm: { ...VEC2_FORM, literal: (u: unknown): { readonly x: number; readonly y: number } | undefined => (u instanceof Vec2 ? u.toJSON() : undefined) },
216
233
  ops: {
217
234
  // The components are fields of a vector, not methods, so these two ops are functions.
218
235
  x: { fn: (v: Vec2): number => v.x, params: [], returns: "number" },
@@ -227,6 +244,7 @@ export const Vec2Domain: Domain<"Vec2", Vec2, Vec2Ops> = defineDomain<"Vec2", Ve
227
244
  negate: { params: [], returns: "domain" },
228
245
  halve: { params: [], returns: "domain" },
229
246
  abs: { params: [], returns: "domain" },
247
+ sign: { params: [], returns: "domain" },
230
248
  round: { params: [], returns: "domain" },
231
249
  floor: { params: [], returns: "domain" },
232
250
  ceil: { params: [], returns: "domain" },
@@ -236,6 +254,7 @@ export const Vec2Domain: Domain<"Vec2", Vec2, Vec2Ops> = defineDomain<"Vec2", Ve
236
254
  select: { liftScalar: true, params: ["domain", "domain"], returns: "domain" },
237
255
  rotate: { params: ["number"], returns: "domain" },
238
256
  normalize: { params: [], returns: "domain" },
257
+ minAxis: { params: [], returns: "domain" },
239
258
  lerp: { params: ["domain", "number"], returns: "domain" },
240
259
  length: { params: [], returns: "number" },
241
260
  lengthSq: { params: [], returns: "number" },
package/src/wrappers.ts CHANGED
@@ -19,6 +19,7 @@ export type FnOps = OpTable<Fn, "invoke">;
19
19
  export const FnDomain: Domain<"Fn", Fn, FnOps> = defineDomain<"Fn", Fn, FnOps>({
20
20
  name: "Fn",
21
21
  is: (u: unknown): u is Fn => u instanceof Fn,
22
+ // LOOP.SIZE.OPS: `invoke` calls a function of the value, so it has no size rule.
22
23
  ops: { invoke: {} },
23
24
  });
24
25
 
@@ -64,5 +65,6 @@ export type MaybeOps = OpTable<Maybe<unknown>, "isTop" | "isBottom" | "map" | "c
64
65
  export const MaybeDomain: Domain<"Maybe", Maybe<unknown>, MaybeOps> = defineDomain<"Maybe", Maybe<unknown>, MaybeOps>({
65
66
  name: "Maybe",
66
67
  is: (u: unknown): u is Maybe<unknown> => u instanceof Maybe,
67
- ops: { isTop: {}, isBottom: {}, map: {}, chain: {}, valueOr: {} },
68
+ // LOOP.SIZE.OPS: `map` and `chain` call a function of the value, so they have no size rule.
69
+ ops: { isTop: { size: "scalar" }, isBottom: { size: "scalar" }, map: {}, chain: {}, valueOr: { size: { size: [1, 1] } } },
68
70
  });
package/src/xy.ts CHANGED
@@ -26,6 +26,8 @@ export type XYOps = {
26
26
  readonly scale: FnOp<XY, (a: XY, k: number) => XY>;
27
27
  readonly negate: FnOp<XY, XYFn1>;
28
28
  readonly abs: FnOp<XY, XYFn1>;
29
+ readonly sign: FnOp<XY, XYFn1>;
30
+ readonly minAxis: FnOp<XY, XYFn1>;
29
31
  readonly floor: FnOp<XY, XYFn1>;
30
32
  readonly ceil: FnOp<XY, XYFn1>;
31
33
  readonly round: FnOp<XY, XYFn1>;
@@ -44,6 +46,9 @@ export type XYOps = {
44
46
  */
45
47
  export const XYDomain: Domain<"XY", XY, XYOps> = defineDomain<"XY", XY, XYOps>({
46
48
  name: "XY",
49
+ // LOOP.SIZE.OPS: each value has a fixed size, so each op has a size rule.
50
+ size: "fixed",
51
+ fields: ["x", "y"],
47
52
  // A plain object: its prototype is Object.prototype or null, so an instance of a class such as Vec2 is not an XY.
48
53
  is: (u: unknown): u is XY => typeof u === "object" && u !== null && [Object.prototype, null].includes(Object.getPrototypeOf(u) as object | null) && Object.keys(u).length === 2 && typeof (u as XY).x === "number" && typeof (u as XY).y === "number",
49
54
  valid: (v: XY): boolean => Number.isFinite(v.x) && Number.isFinite(v.y),
@@ -61,6 +66,9 @@ export const XYDomain: Domain<"XY", XY, XYOps> = defineDomain<"XY", XY, XYOps>({
61
66
  scale: { fn: (a: XY, k: number): XY => ({ x: a.x * k, y: a.y * k }), params: ["number"], returns: "domain" },
62
67
  negate: { fn: (a: XY): XY => ({ x: -a.x, y: -a.y }), params: [], returns: "domain" },
63
68
  abs: { fn: (a: XY): XY => ({ x: Math.abs(a.x), y: Math.abs(a.y) }), params: [], returns: "domain" },
69
+ sign: { fn: (a: XY): XY => ({ x: Math.sign(a.x), y: Math.sign(a.y) }), params: [], returns: "domain" },
70
+ // The rules of minAxis of Vec2 (VEC2.MINAXIS). NaN components fail the check of the result, so `NaN` gives #NUM!.
71
+ minAxis: { fn: (a: XY): XY => (Number.isNaN(a.x) || Number.isNaN(a.y) ? { x: Number.NaN, y: Number.NaN } : a.x <= a.y ? { x: 1, y: 0 } : { x: 0, y: 1 }), params: [], returns: "domain" },
64
72
  floor: { fn: (a: XY): XY => ({ x: Math.floor(a.x), y: Math.floor(a.y) }), params: [], returns: "domain" },
65
73
  ceil: { fn: (a: XY): XY => ({ x: Math.ceil(a.x), y: Math.ceil(a.y) }), params: [], returns: "domain" },
66
74
  round: { fn: (a: XY): XY => ({ x: Math.round(a.x), y: Math.round(a.y) }), params: [], returns: "domain" },