@fraczak/k 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -205,7 +205,7 @@ by `[ codeExp ]`. All members of the vector are the same code. E.g.,
205
205
  -- of integers and a vector of strings
206
206
  ---
207
207
 
208
- ### List comprehension on vectors
208
+ ### List comprehension on vectors (experimental!)
209
209
 
210
210
  A vector can be "open" by PIPE (`|`) operator so the following partial function is applied to
211
211
  each element of the vector one by one, yielding another open value. An open value can be "closed",
@@ -241,6 +241,15 @@ times value `x` appears in the list. (see `Examples/list-comprehension.k` for a
241
241
  $ int;
242
242
  ```
243
243
 
244
+ **WARNING**: The `CARET` operator makes composition not associative, i.e., if you use
245
+ nested _list comprehension_ you cannot drop paranthesis anymore.
246
+ E.g., `| (| PLUS ^ .0) ^` and `| | PLUS ^ .0 ^` are two different functions:
247
+
248
+ ```k
249
+ { |(| PLUS ^ .0) ^ x, | | PLUS ^ .0 ^ y }:
250
+ [[[1,2],[3,4]],[[5,6],[7,8]]] --> {"x":[3,11],"y":[3,7]}
251
+ ```
252
+
244
253
  ---
245
254
 
246
255
  ## Examples
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "k-language for JSON-like data transformation",
5
5
  "main": "index.mjs",
6
6
  "scripts": {
package/run.mjs CHANGED
@@ -10,7 +10,16 @@ function isOpen(x) {
10
10
  function isClosed(x) {
11
11
  return (Array.isArray(x) && !x.open);
12
12
  }
13
-
13
+
14
+ Array.prototype.toJSON = function () {
15
+ if (this.open) {
16
+ // throw new Error(`Cannot serialize open vector: ${JSON.stringify([...this])}.`);
17
+ return {"OPEN VECTOR": [...this]};
18
+ }
19
+ return this;
20
+ };
21
+
22
+
14
23
  const builtin = {
15
24
  "_log!": (arg) => {
16
25
  console.error(`_log!: ${JSON.stringify(arg)}`);
package/symbol-table.mjs CHANGED
@@ -52,26 +52,42 @@ function is_full_rel(rel) {
52
52
  }
53
53
  }
54
54
 
55
+ function does_list_comprehension (e) {
56
+ switch (e.op) {
57
+ case "caret":
58
+ return true;
59
+ case "comp":
60
+ return e.comp.some((e) => e.op === "caret");
61
+ default:
62
+ return false;
63
+ }
64
+ }
65
+
55
66
  function comp_first(e1, e2) {
56
67
  if (is_identity_rel(e1)) return e2;
57
68
  if (is_identity_rel(e2)) return e1;
58
69
  if (is_empty_rel(e1)) return e1;
59
70
  if (is_empty_rel(e2)) return e2;
60
- if (e1.op === "comp" && e2.op === "comp")
61
- return {
62
- op: "comp",
63
- comp: [].concat(e1.comp, e2.comp),
64
- };
65
- if (e1.op === "comp")
66
- return {
67
- op: "comp",
68
- comp: [].concat(e1.comp, [e2]),
69
- };
70
- if (e2.op === "comp")
71
- return {
72
- op: "comp",
73
- comp: [].concat([e1], e2.comp),
74
- };
71
+ if (! does_list_comprehension(e1) && ! does_list_comprehension(e2)) {
72
+ if (e1.op === "comp" && e2.op === "comp") {
73
+ return {
74
+ op: "comp",
75
+ comp: [].concat(e1.comp, e2.comp),
76
+ };
77
+ }
78
+ if (e1.op === "comp") {
79
+ return {
80
+ op: "comp",
81
+ comp: [].concat(e1.comp, [e2]),
82
+ };
83
+ }
84
+ if (e2.op === "comp") {
85
+ return {
86
+ op: "comp",
87
+ comp: [].concat([e1], e2.comp),
88
+ };
89
+ }
90
+ }
75
91
  return {
76
92
  op: "comp",
77
93
  comp: [e1, e2],