@elaraai/east 0.0.1-beta.1 → 0.0.1-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/LICENSE.md +15 -666
  2. package/README.md +15 -6
  3. package/dist/src/analyze.d.ts +4 -4
  4. package/dist/src/analyze.d.ts.map +1 -1
  5. package/dist/src/analyze.js +142 -44
  6. package/dist/src/analyze.js.map +1 -1
  7. package/dist/src/ast.d.ts +18 -7
  8. package/dist/src/ast.d.ts.map +1 -1
  9. package/dist/src/ast.js +1 -146
  10. package/dist/src/ast.js.map +1 -1
  11. package/dist/src/ast_to_ir.d.ts +7 -0
  12. package/dist/src/ast_to_ir.d.ts.map +1 -1
  13. package/dist/src/ast_to_ir.js +80 -11
  14. package/dist/src/ast_to_ir.js.map +1 -1
  15. package/dist/src/builtins.d.ts +1 -1
  16. package/dist/src/builtins.d.ts.map +1 -1
  17. package/dist/src/builtins.js +89 -64
  18. package/dist/src/builtins.js.map +1 -1
  19. package/dist/src/comparison.d.ts.map +1 -1
  20. package/dist/src/comparison.js +19 -2
  21. package/dist/src/comparison.js.map +1 -1
  22. package/dist/src/compile.d.ts.map +1 -1
  23. package/dist/src/compile.js +137 -221
  24. package/dist/src/compile.js.map +1 -1
  25. package/dist/src/default.d.ts.map +1 -1
  26. package/dist/src/default.js +2 -0
  27. package/dist/src/default.js.map +1 -1
  28. package/dist/src/eastir.d.ts +12 -10
  29. package/dist/src/eastir.d.ts.map +1 -1
  30. package/dist/src/eastir.js +20 -19
  31. package/dist/src/eastir.js.map +1 -1
  32. package/dist/src/expr/array.d.ts +66 -0
  33. package/dist/src/expr/array.d.ts.map +1 -1
  34. package/dist/src/expr/array.js +161 -51
  35. package/dist/src/expr/array.js.map +1 -1
  36. package/dist/src/expr/ast.d.ts.map +1 -1
  37. package/dist/src/expr/ast.js +6 -0
  38. package/dist/src/expr/ast.js.map +1 -1
  39. package/dist/src/expr/asyncfunction.d.ts +49 -0
  40. package/dist/src/expr/asyncfunction.d.ts.map +1 -0
  41. package/dist/src/expr/asyncfunction.js +60 -0
  42. package/dist/src/expr/asyncfunction.js.map +1 -0
  43. package/dist/src/expr/blob.d.ts +25 -1
  44. package/dist/src/expr/blob.d.ts.map +1 -1
  45. package/dist/src/expr/blob.js +36 -1
  46. package/dist/src/expr/blob.js.map +1 -1
  47. package/dist/src/expr/block.d.ts +82 -8
  48. package/dist/src/expr/block.d.ts.map +1 -1
  49. package/dist/src/expr/block.js +278 -16
  50. package/dist/src/expr/block.js.map +1 -1
  51. package/dist/src/expr/dict.js +47 -47
  52. package/dist/src/expr/dict.js.map +1 -1
  53. package/dist/src/expr/expr.d.ts +9 -0
  54. package/dist/src/expr/expr.d.ts.map +1 -1
  55. package/dist/src/expr/expr.js +5 -1
  56. package/dist/src/expr/expr.js.map +1 -1
  57. package/dist/src/expr/index.d.ts +112 -2
  58. package/dist/src/expr/index.d.ts.map +1 -1
  59. package/dist/src/expr/index.js +110 -8
  60. package/dist/src/expr/index.js.map +1 -1
  61. package/dist/src/expr/libs/array.js +1 -1
  62. package/dist/src/expr/libs/array.js.map +1 -1
  63. package/dist/src/expr/libs/dict.js +3 -3
  64. package/dist/src/expr/libs/dict.js.map +1 -1
  65. package/dist/src/expr/libs/integer.d.ts +19 -0
  66. package/dist/src/expr/libs/integer.d.ts.map +1 -1
  67. package/dist/src/expr/libs/integer.js +47 -0
  68. package/dist/src/expr/libs/integer.js.map +1 -1
  69. package/dist/src/expr/libs/set.js +2 -2
  70. package/dist/src/expr/libs/set.js.map +1 -1
  71. package/dist/src/expr/ref.js +1 -1
  72. package/dist/src/expr/ref.js.map +1 -1
  73. package/dist/src/expr/set.js +38 -38
  74. package/dist/src/expr/set.js.map +1 -1
  75. package/dist/src/expr/struct.d.ts +2 -0
  76. package/dist/src/expr/struct.d.ts.map +1 -1
  77. package/dist/src/expr/types.d.ts +8 -5
  78. package/dist/src/expr/types.d.ts.map +1 -1
  79. package/dist/src/expr/variant.d.ts +53 -0
  80. package/dist/src/expr/variant.d.ts.map +1 -1
  81. package/dist/src/expr/variant.js +56 -0
  82. package/dist/src/expr/variant.js.map +1 -1
  83. package/dist/src/fuzz.d.ts.map +1 -1
  84. package/dist/src/fuzz.js +5 -0
  85. package/dist/src/fuzz.js.map +1 -1
  86. package/dist/src/internal.d.ts +8 -0
  87. package/dist/src/internal.d.ts.map +1 -1
  88. package/dist/src/internal.js +8 -0
  89. package/dist/src/internal.js.map +1 -1
  90. package/dist/src/ir.d.ts +231 -33
  91. package/dist/src/ir.d.ts.map +1 -1
  92. package/dist/src/ir.js +3 -1
  93. package/dist/src/ir.js.map +1 -1
  94. package/dist/src/serialization/beast.d.ts.map +1 -1
  95. package/dist/src/serialization/beast.js +59 -18
  96. package/dist/src/serialization/beast.js.map +1 -1
  97. package/dist/src/serialization/beast2-stream.d.ts.map +1 -1
  98. package/dist/src/serialization/beast2-stream.js +5 -0
  99. package/dist/src/serialization/beast2-stream.js.map +1 -1
  100. package/dist/src/serialization/beast2.d.ts.map +1 -1
  101. package/dist/src/serialization/beast2.js +10 -4
  102. package/dist/src/serialization/beast2.js.map +1 -1
  103. package/dist/src/serialization/csv.d.ts +139 -0
  104. package/dist/src/serialization/csv.d.ts.map +1 -0
  105. package/dist/src/serialization/csv.js +615 -0
  106. package/dist/src/serialization/csv.js.map +1 -0
  107. package/dist/src/serialization/east.d.ts.map +1 -1
  108. package/dist/src/serialization/east.js +9 -1
  109. package/dist/src/serialization/east.js.map +1 -1
  110. package/dist/src/serialization/index.d.ts +1 -0
  111. package/dist/src/serialization/index.d.ts.map +1 -1
  112. package/dist/src/serialization/index.js +1 -0
  113. package/dist/src/serialization/index.js.map +1 -1
  114. package/dist/src/serialization/json.d.ts.map +1 -1
  115. package/dist/src/serialization/json.js +8 -2
  116. package/dist/src/serialization/json.js.map +1 -1
  117. package/dist/src/type_of_type.d.ts +20 -3
  118. package/dist/src/type_of_type.d.ts.map +1 -1
  119. package/dist/src/type_of_type.js +101 -13
  120. package/dist/src/type_of_type.js.map +1 -1
  121. package/dist/src/types.d.ts +59 -16
  122. package/dist/src/types.d.ts.map +1 -1
  123. package/dist/src/types.js +141 -91
  124. package/dist/src/types.js.map +1 -1
  125. package/package.json +4 -4
@@ -3,6 +3,8 @@ import { ArrayType, BooleanType, FunctionType, IntegerType, NullType, StringType
3
3
  import { valueOrExprToAst, valueOrExprToAstTyped } from "./ast.js";
4
4
  import { AstSymbol, Expr, FactorySymbol, TypeSymbol } from "./expr.js";
5
5
  import { none, some } from "../containers/variant.js";
6
+ import { CsvSerializeConfigType, csvSerializeOptionsToValue } from "../serialization/csv.js";
7
+ import { BlobType } from "../types.js";
6
8
  /**
7
9
  * Expression representing mutable array values and operations.
8
10
  *
@@ -62,6 +64,31 @@ export class ArrayExpr extends Expr {
62
64
  arguments: [this[AstSymbol]],
63
65
  });
64
66
  }
67
+ /**
68
+ * Returns the length of the array (an alias for size).
69
+ *
70
+ * @returns An IntegerExpr representing the number of elements
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const getLength = East.function([ArrayType(IntegerType)], IntegerType, ($, arr) => {
75
+ * $.return(arr.length());
76
+ * });
77
+ * const compiled = East.compile(getLength.toIR(), []);
78
+ * compiled([1n, 2n, 3n]); // 3n
79
+ * compiled([]); // 0n
80
+ * ```
81
+ */
82
+ length() {
83
+ return this[FactorySymbol]({
84
+ ast_type: "Builtin",
85
+ type: IntegerType,
86
+ location: get_location(2),
87
+ builtin: "ArraySize",
88
+ type_parameters: [this.value_type],
89
+ arguments: [this[AstSymbol]],
90
+ });
91
+ }
65
92
  /**
66
93
  * Checks if an element exists at the specified index (i.e., if the index is in bounds).
67
94
  *
@@ -119,7 +146,57 @@ export class ArrayExpr extends Expr {
119
146
  get(key, defaultFn) {
120
147
  const keyAst = valueOrExprToAstTyped(key, IntegerType);
121
148
  if (defaultFn !== undefined) {
122
- const defaultFnAst = valueOrExprToAstTyped(defaultFn, FunctionType([IntegerType], this.value_type, null));
149
+ const defaultFnAst = valueOrExprToAstTyped(defaultFn, FunctionType([IntegerType], this.value_type));
150
+ return this[FactorySymbol]({
151
+ ast_type: "Builtin",
152
+ type: this.value_type,
153
+ location: get_location(2),
154
+ builtin: "ArrayGetOrDefault",
155
+ type_parameters: [this.value_type],
156
+ arguments: [this[AstSymbol], keyAst, defaultFnAst],
157
+ });
158
+ }
159
+ else {
160
+ return this[FactorySymbol]({
161
+ ast_type: "Builtin",
162
+ type: this.value_type,
163
+ location: get_location(2),
164
+ builtin: "ArrayGet",
165
+ type_parameters: [this.value_type],
166
+ arguments: [this[AstSymbol], keyAst],
167
+ });
168
+ }
169
+ }
170
+ /**
171
+ * Gets the element at the specified index (alias for get).
172
+ *
173
+ * @param key - The zero-based index
174
+ * @param defaultFn - Optional function to provide a default value for out-of-bounds indices
175
+ * @returns An expression of the array's element type
176
+ *
177
+ * @throws East runtime error if index is out of bounds and no defaultFn is provided
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * const getElement = East.function([ArrayType(IntegerType), IntegerType], IntegerType, ($, arr, index) => {
182
+ * $.return(arr.get(index));
183
+ * });
184
+ * const compiled = East.compile(getElement.toIR(), []);
185
+ * compiled([10n, 20n, 30n], 1n); // 20n
186
+ * // compiled([10n, 20n, 30n], 5n) would throw error
187
+ *
188
+ * // With default value
189
+ * const getOrDefault = East.function([ArrayType(IntegerType), IntegerType], IntegerType, ($, arr, index) => {
190
+ * $.return(arr.get(index, ($, i) => -1n));
191
+ * });
192
+ * compiled = East.compile(getOrDefault.toIR(), []);
193
+ * compiled([10n, 20n, 30n], 5n); // -1n (out of bounds)
194
+ * ```
195
+ */
196
+ at(key, defaultFn) {
197
+ const keyAst = valueOrExprToAstTyped(key, IntegerType);
198
+ if (defaultFn !== undefined) {
199
+ const defaultFnAst = valueOrExprToAstTyped(defaultFn, FunctionType([IntegerType], this.value_type));
123
200
  return this[FactorySymbol]({
124
201
  ast_type: "Builtin",
125
202
  type: this.value_type,
@@ -208,7 +285,7 @@ export class ArrayExpr extends Expr {
208
285
  const location = get_location(2);
209
286
  const keyAst = valueOrExprToAstTyped(key, IntegerType);
210
287
  const value2Type = value[TypeSymbol];
211
- const updateFnExpr = Expr.from(updateFn, FunctionType([value2Type, this.value_type, IntegerType], this.value_type, null));
288
+ const updateFnExpr = Expr.from(updateFn, FunctionType([value2Type, this.value_type, IntegerType], this.value_type));
212
289
  return this[FactorySymbol]({
213
290
  ast_type: "Builtin",
214
291
  type: NullType,
@@ -417,7 +494,7 @@ export class ArrayExpr extends Expr {
417
494
  throw new Error(`Expected array type for mergeAll, got ${array_type ? printType(array_type) : "unknown"}`);
418
495
  }
419
496
  const value2Type = array_type.value;
420
- const mergeFnExpr = Expr.from(mergeFn, FunctionType([this.value_type, value2Type, IntegerType], this.value_type, null));
497
+ const mergeFnExpr = Expr.from(mergeFn, FunctionType([this.value_type, value2Type, IntegerType], this.value_type));
421
498
  return this[FactorySymbol]({
422
499
  ast_type: "Builtin",
423
500
  type: NullType,
@@ -912,7 +989,7 @@ export class ArrayExpr extends Expr {
912
989
  default_function_ast = Expr.ast(default_function);
913
990
  }
914
991
  else {
915
- const default_function_expr = Expr.from(onMissing, FunctionType([IntegerType], this.value_type, null));
992
+ const default_function_expr = Expr.from(onMissing, FunctionType([IntegerType], this.value_type));
916
993
  default_function_ast = Expr.ast(default_function_expr);
917
994
  }
918
995
  return this[FactorySymbol]({
@@ -1094,7 +1171,7 @@ export class ArrayExpr extends Expr {
1094
1171
  }
1095
1172
  }
1096
1173
  filterMap(fn) {
1097
- const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], undefined, null));
1174
+ const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], undefined));
1098
1175
  const returnType = fnAst.type.output;
1099
1176
  if (returnType.type !== "Variant") {
1100
1177
  throw new Error(`Expected Function to return an Option type, got ${printType(returnType)}`);
@@ -1113,7 +1190,7 @@ export class ArrayExpr extends Expr {
1113
1190
  });
1114
1191
  }
1115
1192
  firstMap(fn) {
1116
- const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], undefined, null));
1193
+ const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], undefined));
1117
1194
  const returnType = fnAst.type.output;
1118
1195
  if (returnType.type !== "Variant") {
1119
1196
  throw new Error(`Expected Function to return an Option type, got ${printType(returnType)}`);
@@ -1208,7 +1285,7 @@ export class ArrayExpr extends Expr {
1208
1285
  reduce(combineFn, init) {
1209
1286
  const initAst = valueOrExprToAst(init);
1210
1287
  const returnType = initAst.type;
1211
- const combineAst = valueOrExprToAstTyped(combineFn, FunctionType([returnType, this.value_type, IntegerType], returnType, null));
1288
+ const combineAst = valueOrExprToAstTyped(combineFn, FunctionType([returnType, this.value_type, IntegerType], returnType));
1212
1289
  return this[FactorySymbol]({
1213
1290
  ast_type: "Builtin",
1214
1291
  type: returnType,
@@ -1219,9 +1296,9 @@ export class ArrayExpr extends Expr {
1219
1296
  });
1220
1297
  }
1221
1298
  mapReduce(mapFn, combineFn) {
1222
- const mapAst = valueOrExprToAstTyped(mapFn, FunctionType([this.value_type, IntegerType], undefined, null));
1299
+ const mapAst = valueOrExprToAstTyped(mapFn, FunctionType([this.value_type, IntegerType], undefined));
1223
1300
  const mapType = mapAst.type.output;
1224
- const combineAst = valueOrExprToAstTyped(combineFn, FunctionType([mapType, mapType], mapType, null));
1301
+ const combineAst = valueOrExprToAstTyped(combineFn, FunctionType([mapType, mapType], mapType));
1225
1302
  return this[FactorySymbol]({
1226
1303
  ast_type: "Builtin",
1227
1304
  type: mapType,
@@ -1274,7 +1351,7 @@ export class ArrayExpr extends Expr {
1274
1351
  const result = this.firstMap(($, v, _k) => v.not().ifElse(() => some(null), () => none));
1275
1352
  return Expr.match(result, { some: () => false, none: () => true });
1276
1353
  }
1277
- const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], BooleanType, null));
1354
+ const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], BooleanType));
1278
1355
  // Short-circuit on first false value
1279
1356
  const result = this.firstMap(($, v, k) => {
1280
1357
  const result = Expr.fromAst(fnAst)(v, k);
@@ -1324,7 +1401,7 @@ export class ArrayExpr extends Expr {
1324
1401
  const result = this.firstMap(($, v, _k) => v.ifElse(() => some(null), () => none));
1325
1402
  return Expr.match(result, { some: () => true, none: () => false });
1326
1403
  }
1327
- const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], BooleanType, null));
1404
+ const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], BooleanType));
1328
1405
  // Short-circuit on first true value
1329
1406
  const result = this.firstMap(($, v, k) => {
1330
1407
  const result = Expr.fromAst(fnAst)(v, k);
@@ -1341,7 +1418,7 @@ export class ArrayExpr extends Expr {
1341
1418
  return this.reduce(($, previous, value) => previous.add(value), zero);
1342
1419
  }
1343
1420
  else {
1344
- const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], undefined, null));
1421
+ const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], undefined));
1345
1422
  const returnType = fnAst.type.output;
1346
1423
  if (!(isTypeEqual(returnType, IntegerType) || isTypeEqual(returnType, FloatType))) {
1347
1424
  throw new Error(`Can only perform sum on array of numbers (Integer or Float), got ${printType(returnType)}`);
@@ -1363,7 +1440,7 @@ export class ArrayExpr extends Expr {
1363
1440
  }
1364
1441
  }
1365
1442
  else {
1366
- const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], undefined, null));
1443
+ const fnAst = valueOrExprToAstTyped(fn, FunctionType([this.value_type, IntegerType], undefined));
1367
1444
  const returnType = fnAst.type.output;
1368
1445
  if (isTypeEqual(returnType, IntegerType)) {
1369
1446
  return this.reduce(($, previous, value, key) => previous.add(Expr.fromAst(fnAst)(value, key).toFloat()), 0.0).divide(this.size().toFloat());
@@ -1522,7 +1599,7 @@ export class ArrayExpr extends Expr {
1522
1599
  });
1523
1600
  }
1524
1601
  toSet(keyFn) {
1525
- const keyFnAst = valueOrExprToAstTyped(keyFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1602
+ const keyFnAst = valueOrExprToAstTyped(keyFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1526
1603
  const keyType = keyFnAst.type.output;
1527
1604
  return this[FactorySymbol]({
1528
1605
  ast_type: "Builtin",
@@ -1534,9 +1611,9 @@ export class ArrayExpr extends Expr {
1534
1611
  });
1535
1612
  }
1536
1613
  toDict(keyFn, valueFn, onConflictFn) {
1537
- const keyFnAst = valueOrExprToAstTyped(keyFn ?? ((_$, x, i) => i), FunctionType([this.value_type, IntegerType], undefined, null));
1614
+ const keyFnAst = valueOrExprToAstTyped(keyFn ?? ((_$, x, i) => i), FunctionType([this.value_type, IntegerType], undefined));
1538
1615
  const keyType = keyFnAst.type.output;
1539
- const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1616
+ const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1540
1617
  const valueType = valueFnAst.type.output;
1541
1618
  let onConflictAst;
1542
1619
  if (onConflictFn === undefined) {
@@ -1545,7 +1622,7 @@ export class ArrayExpr extends Expr {
1545
1622
  onConflictAst = Expr.ast(onConflictFunction);
1546
1623
  }
1547
1624
  else {
1548
- onConflictAst = valueOrExprToAstTyped(onConflictFn, FunctionType([valueType, valueType, keyType], valueType, null));
1625
+ onConflictAst = valueOrExprToAstTyped(onConflictFn, FunctionType([valueType, valueType, keyType], valueType));
1549
1626
  }
1550
1627
  return this[FactorySymbol]({
1551
1628
  ast_type: "Builtin",
@@ -1557,7 +1634,7 @@ export class ArrayExpr extends Expr {
1557
1634
  });
1558
1635
  }
1559
1636
  flatMap(fn) {
1560
- const fnAst = valueOrExprToAstTyped(fn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1637
+ const fnAst = valueOrExprToAstTyped(fn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1561
1638
  const returnType = fnAst.type.output;
1562
1639
  if (returnType.type !== "Array") {
1563
1640
  throw new Error(`Expected Function to return an Array type, got ${printType(returnType)}`);
@@ -1573,7 +1650,7 @@ export class ArrayExpr extends Expr {
1573
1650
  });
1574
1651
  }
1575
1652
  flattenToSet(fn) {
1576
- const fnAst = valueOrExprToAstTyped(fn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1653
+ const fnAst = valueOrExprToAstTyped(fn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1577
1654
  const returnType = fnAst.type.output;
1578
1655
  if (returnType.type !== "Set") {
1579
1656
  throw new Error(`Expected Function to return a Set type, got ${printType(returnType)}`);
@@ -1589,7 +1666,7 @@ export class ArrayExpr extends Expr {
1589
1666
  });
1590
1667
  }
1591
1668
  flattenToDict(fn, onConflictFn) {
1592
- const fnAst = valueOrExprToAstTyped(fn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1669
+ const fnAst = valueOrExprToAstTyped(fn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1593
1670
  const returnType = fnAst.type.output;
1594
1671
  if (returnType.type !== "Dict") {
1595
1672
  throw new Error(`Expected Function to return a Dict type, got ${printType(returnType)}`);
@@ -1603,7 +1680,7 @@ export class ArrayExpr extends Expr {
1603
1680
  onConflictAst = Expr.ast(onConflictFunction);
1604
1681
  }
1605
1682
  else {
1606
- onConflictAst = valueOrExprToAstTyped(onConflictFn, FunctionType([valueType, valueType, keyType], valueType, null));
1683
+ onConflictAst = valueOrExprToAstTyped(onConflictFn, FunctionType([valueType, valueType, keyType], valueType));
1607
1684
  }
1608
1685
  return this[FactorySymbol]({
1609
1686
  ast_type: "Builtin",
@@ -1616,11 +1693,11 @@ export class ArrayExpr extends Expr {
1616
1693
  }
1617
1694
  groupReduce(keyFn, initFn, reduceFn) {
1618
1695
  // Note - initFn has to be before reduceFn, otherwise the TypeScript type inference doesn't work properly
1619
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1696
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1620
1697
  const keyType = keyFnAst.type.output;
1621
- const initFnAst = valueOrExprToAstTyped(initFn, FunctionType([keyType], undefined, null));
1698
+ const initFnAst = valueOrExprToAstTyped(initFn, FunctionType([keyType], undefined));
1622
1699
  const initType = initFnAst.type.output;
1623
- const reduceFnAst = valueOrExprToAstTyped(reduceFn, FunctionType([initType, this.value_type, IntegerType], initType, null));
1700
+ const reduceFnAst = valueOrExprToAstTyped(reduceFn, FunctionType([initType, this.value_type, IntegerType], initType));
1624
1701
  return this[FactorySymbol]({
1625
1702
  ast_type: "Builtin",
1626
1703
  type: DictType(keyType, initType),
@@ -1631,27 +1708,27 @@ export class ArrayExpr extends Expr {
1631
1708
  });
1632
1709
  }
1633
1710
  groupSize(keyFn) {
1634
- const keyFnAst = valueOrExprToAstTyped(keyFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1711
+ const keyFnAst = valueOrExprToAstTyped(keyFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1635
1712
  return this.toDict(((_$, elem, idx) => Expr.fromAst(keyFnAst)(elem, idx)), ((_$) => 1n), ((_$, a, b) => a.add(b)));
1636
1713
  }
1637
1714
  groupEvery(keyFn, predFn) {
1638
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1639
- const predFnAst = valueOrExprToAstTyped(predFn, FunctionType([this.value_type, IntegerType], BooleanType, null));
1715
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1716
+ const predFnAst = valueOrExprToAstTyped(predFn, FunctionType([this.value_type, IntegerType], BooleanType));
1640
1717
  return this.groupReduce(((_$, elem, idx) => Expr.fromAst(keyFnAst)(elem, idx)), (() => true), ((_$, acc, elem, idx) => {
1641
1718
  const pred = Expr.fromAst(predFnAst)(elem, idx);
1642
1719
  return acc.and(() => pred);
1643
1720
  }));
1644
1721
  }
1645
1722
  groupSome(keyFn, predFn) {
1646
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1647
- const predFnAst = valueOrExprToAstTyped(predFn, FunctionType([this.value_type, IntegerType], BooleanType, null));
1723
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1724
+ const predFnAst = valueOrExprToAstTyped(predFn, FunctionType([this.value_type, IntegerType], BooleanType));
1648
1725
  return this.groupReduce(((_$, elem, idx) => Expr.fromAst(keyFnAst)(elem, idx)), (() => false), ((_$, acc, elem, idx) => {
1649
1726
  const pred = Expr.fromAst(predFnAst)(elem, idx);
1650
1727
  return acc.or(() => pred);
1651
1728
  }));
1652
1729
  }
1653
1730
  groupFindAll(keyFn, value, projFn) {
1654
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1731
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1655
1732
  const keyFnExpr = Expr.fromAst(keyFnAst);
1656
1733
  if (projFn === undefined) {
1657
1734
  // Without projection: find where element == value
@@ -1673,7 +1750,7 @@ export class ArrayExpr extends Expr {
1673
1750
  }
1674
1751
  }
1675
1752
  groupFindFirst(keyFn, value, projFn) {
1676
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1753
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1677
1754
  const keyFnExpr = Expr.fromAst(keyFnAst);
1678
1755
  if (projFn === undefined) {
1679
1756
  // Without projection: find where element == value
@@ -1695,18 +1772,18 @@ export class ArrayExpr extends Expr {
1695
1772
  }
1696
1773
  }
1697
1774
  groupFindMinimum(keyFn, byFn) {
1698
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1699
- const byFnAst = valueOrExprToAstTyped(byFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1775
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1776
+ const byFnAst = valueOrExprToAstTyped(byFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1700
1777
  return this.toDict(((_$, elem, idx) => Expr.fromAst(keyFnAst)(elem, idx)), ((_$, elem, idx) => ({ by: Expr.fromAst(byFnAst)(elem, idx), index: idx })), ((_$, a, b) => Expr.lessEqual(a.by, b.by).ifElse(() => a, () => b))).map(((_$, v) => v.index));
1701
1778
  }
1702
1779
  groupFindMaximum(keyFn, byFn) {
1703
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1704
- const byFnAst = valueOrExprToAstTyped(byFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1780
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1781
+ const byFnAst = valueOrExprToAstTyped(byFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1705
1782
  return this.toDict(((_$, elem, idx) => Expr.fromAst(keyFnAst)(elem, idx)), ((_$, elem, idx) => ({ by: Expr.fromAst(byFnAst)(elem, idx), index: idx })), ((_$, a, b) => Expr.greaterEqual(a.by, b.by).ifElse(() => a, () => b))).map(((_$, v) => v.index));
1706
1783
  }
1707
1784
  groupSum(keyFn, valueFn) {
1708
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1709
- const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1785
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1786
+ const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1710
1787
  const valueType = valueFnAst.type.output;
1711
1788
  const isInteger = isTypeEqual(valueType, IntegerType);
1712
1789
  const isFloat = isTypeEqual(valueType, FloatType);
@@ -1716,8 +1793,8 @@ export class ArrayExpr extends Expr {
1716
1793
  return this.toDict(((_$, elem, idx) => Expr.fromAst(keyFnAst)(elem, idx)), ((_$, elem, idx) => Expr.fromAst(valueFnAst)(elem, idx)), ((_$, a, b) => a.add(b)));
1717
1794
  }
1718
1795
  groupMean(keyFn, valueFn) {
1719
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1720
- const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1796
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1797
+ const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1721
1798
  const valueType = valueFnAst.type.output;
1722
1799
  const isInteger = isTypeEqual(valueType, IntegerType);
1723
1800
  const isFloat = isTypeEqual(valueType, FloatType);
@@ -1730,8 +1807,8 @@ export class ArrayExpr extends Expr {
1730
1807
  }), ((_$, a, b) => ({ sum: a.sum.add(b.sum), count: a.count.add(b.count) }))).map(((_$, v) => v.sum.divide(v.count.toFloat())));
1731
1808
  }
1732
1809
  groupToArrays(keyFn, valueFn) {
1733
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1734
- const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1810
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1811
+ const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1735
1812
  const keyFnExpr = Expr.fromAst(keyFnAst);
1736
1813
  const valueFnExpr = Expr.fromAst(valueFnAst);
1737
1814
  const valueType = valueFnAst.type.output;
@@ -1742,8 +1819,8 @@ export class ArrayExpr extends Expr {
1742
1819
  }));
1743
1820
  }
1744
1821
  groupToSets(keyFn, valueFn) {
1745
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1746
- const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1822
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1823
+ const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1747
1824
  const keyFnExpr = Expr.fromAst(keyFnAst);
1748
1825
  const valueFnExpr = Expr.fromAst(valueFnAst);
1749
1826
  const valueType = valueFnAst.type.output;
@@ -1754,19 +1831,19 @@ export class ArrayExpr extends Expr {
1754
1831
  }));
1755
1832
  }
1756
1833
  groupMinimum(keyFn, byFn) {
1757
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1758
- const byFnAst = valueOrExprToAstTyped(byFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1834
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1835
+ const byFnAst = valueOrExprToAstTyped(byFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1759
1836
  return this.toDict(((_$, elem, idx) => Expr.fromAst(keyFnAst)(elem, idx)), ((_$, elem, idx) => ({ by: Expr.fromAst(byFnAst)(elem, idx), elem })), ((_$, a, b) => Expr.lessEqual(a.by, b.by).ifElse(() => a, () => b))).map(((_$, v) => v.elem));
1760
1837
  }
1761
1838
  groupMaximum(keyFn, byFn) {
1762
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1763
- const byFnAst = valueOrExprToAstTyped(byFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1839
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1840
+ const byFnAst = valueOrExprToAstTyped(byFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1764
1841
  return this.toDict(((_$, elem, idx) => Expr.fromAst(keyFnAst)(elem, idx)), ((_$, elem, idx) => ({ by: Expr.fromAst(byFnAst)(elem, idx), elem })), ((_$, a, b) => Expr.greaterEqual(a.by, b.by).ifElse(() => a, () => b))).map(((_$, v) => v.elem));
1765
1842
  }
1766
1843
  groupToDicts(keyFn, keyFn2, valueFn, combineFn) {
1767
- const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined, null));
1768
- const keyFn2Ast = valueOrExprToAstTyped(keyFn2, FunctionType([this.value_type, IntegerType], undefined, null));
1769
- const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined, null));
1844
+ const keyFnAst = valueOrExprToAstTyped(keyFn, FunctionType([this.value_type, IntegerType], undefined));
1845
+ const keyFn2Ast = valueOrExprToAstTyped(keyFn2, FunctionType([this.value_type, IntegerType], undefined));
1846
+ const valueFnAst = valueOrExprToAstTyped(valueFn ?? ((_$, x) => x), FunctionType([this.value_type, IntegerType], undefined));
1770
1847
  const keyFnExpr = Expr.fromAst(keyFnAst);
1771
1848
  const keyFn2Expr = Expr.fromAst(keyFn2Ast);
1772
1849
  const valueFnExpr = Expr.fromAst(valueFnAst);
@@ -1774,7 +1851,7 @@ export class ArrayExpr extends Expr {
1774
1851
  const valueType = valueFnAst.type.output;
1775
1852
  if (combineFn !== undefined) {
1776
1853
  // With conflict resolution - use tryGet + match to check existence, then insert or combine
1777
- const combineFnAst = valueOrExprToAstTyped(combineFn, FunctionType([valueType, valueType], valueType, null));
1854
+ const combineFnAst = valueOrExprToAstTyped(combineFn, FunctionType([valueType, valueType], valueType));
1778
1855
  const combineFnExpr = Expr.fromAst(combineFnAst);
1779
1856
  return this.groupReduce(((_$, elem, idx) => keyFnExpr(elem, idx)), ((_$, _key) => Expr.from(new Map(), DictType(key2Type, valueType))), (($, dict, elem, idx) => {
1780
1857
  const innerKey = keyFn2Expr(elem, idx);
@@ -1801,5 +1878,38 @@ export class ArrayExpr extends Expr {
1801
1878
  }));
1802
1879
  }
1803
1880
  }
1881
+ /**
1882
+ * Encodes this array of structs as CSV data.
1883
+ *
1884
+ * @param options - CSV serialization options
1885
+ * @returns A BlobExpr containing the encoded CSV data
1886
+ *
1887
+ * @remarks Only works on arrays of structs with primitive or optional primitive fields.
1888
+ *
1889
+ * @example
1890
+ * ```ts
1891
+ * const PersonType = StructType({ name: StringType, age: IntegerType });
1892
+ *
1893
+ * const toCsv = East.function([ArrayType(PersonType)], BlobType, ($, people) => {
1894
+ * $.return(people.encodeCsv({ delimiter: ',' }));
1895
+ * });
1896
+ * const compiled = East.compile(toCsv.toIR(), []);
1897
+ * const blob = compiled([{ name: "Alice", age: 30n }, { name: "Bob", age: 25n }]);
1898
+ * new TextDecoder().decode(blob); // "name,age\r\nAlice,30\r\nBob,25"
1899
+ * ```
1900
+ */
1901
+ encodeCsv(options) {
1902
+ // Convert options to East config value
1903
+ const configValue = csvSerializeOptionsToValue(options);
1904
+ const configAst = valueOrExprToAstTyped(configValue, CsvSerializeConfigType);
1905
+ return this[FactorySymbol]({
1906
+ ast_type: "Builtin",
1907
+ type: BlobType,
1908
+ location: get_location(2),
1909
+ builtin: "ArrayEncodeCsv",
1910
+ type_parameters: [this.value_type, CsvSerializeConfigType],
1911
+ arguments: [this[AstSymbol], configAst],
1912
+ });
1913
+ }
1804
1914
  }
1805
1915
  //# sourceMappingURL=array.js.map