@fraczak/k 1.2.0 → 1.2.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/Benchmark/range1000 +1000 -0
- package/Benchmark/range200 +200 -0
- package/README.md +222 -194
- package/package.json +4 -4
- package/run.coffee +43 -43
- package/run.js +76 -73
- package/Examples/bnat.k +0 -121
- package/Examples/code.k +0 -21
- package/Examples/error.k +0 -8
- package/Examples/factorial.k +0 -12
- package/Examples/fib.k +0 -17
- package/Examples/pow.k +0 -7
- package/Examples/rec-code.k +0 -80
- package/Examples/test.k +0 -13
- package/Examples/typed-fib.k +0 -19
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fraczak/k",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "k-language for JSON-like data transformation",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"prepare": "coffee -c symbol-table.coffee; jison parser.jison",
|
|
8
|
-
|
|
8
|
+
"prepublishOnly": "coffee -c *.coffee; echo '#!/usr/bin/env node' | cat - k.js > k.sh; echo '#!/usr/bin/env node' | cat - repl.js > repl.sh;",
|
|
9
9
|
"all": "coffee -c *.coffee; npm run prepare; npm run prepublishOnly",
|
|
10
10
|
"test": "coffee -c symbol-table.coffee; jison parser.jison; coffee test.coffee"
|
|
11
11
|
},
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"k-repl": "./repl.sh"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
19
|
+
"coffeescript": "^2.7.0",
|
|
20
|
+
"jison": "^0.4.18"
|
|
21
21
|
}
|
|
22
22
|
}
|
package/run.coffee
CHANGED
|
@@ -58,16 +58,20 @@ verify = (code, value) ->
|
|
|
58
58
|
# representatives = run.defs.representatives
|
|
59
59
|
# defCodes = JSON.stringify run.defs.codes
|
|
60
60
|
# console.log {code,value, representatives, defCodes}
|
|
61
|
+
return false unless code?
|
|
61
62
|
switch code.code
|
|
62
63
|
when "vector"
|
|
64
|
+
return false unless Array.isArray value
|
|
63
65
|
value.every (x) ->
|
|
64
66
|
verify code.vector, x
|
|
65
67
|
when "product"
|
|
68
|
+
return false unless "object" is typeof value
|
|
66
69
|
do (fields = Object.keys(value)) ->
|
|
67
70
|
return false unless fields.length is Object.keys(code.product).length
|
|
68
71
|
fields.every (label) ->
|
|
69
72
|
verify code.product[label], value[label]
|
|
70
73
|
when "union"
|
|
74
|
+
return false unless "object" is typeof value
|
|
71
75
|
do (fields = Object.keys(value)) ->
|
|
72
76
|
return false unless fields.length is 1
|
|
73
77
|
verify code.union[fields[0]], value[fields[0]]
|
|
@@ -77,50 +81,46 @@ verify = (code, value) ->
|
|
|
77
81
|
codes[code] value
|
|
78
82
|
|
|
79
83
|
run = (exp, value) ->
|
|
84
|
+
"use strict"
|
|
80
85
|
# console.log {exp,value}
|
|
81
86
|
return undefined if value is undefined
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return undefined
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
result[label] = value
|
|
121
|
-
result
|
|
122
|
-
, {}
|
|
123
|
-
else
|
|
124
|
-
console.log exp.op
|
|
87
|
+
switch exp.op
|
|
88
|
+
when "code"
|
|
89
|
+
return value if verify exp.code, value
|
|
90
|
+
when "identity"
|
|
91
|
+
return value
|
|
92
|
+
when "str", "int"
|
|
93
|
+
return exp[exp.op]
|
|
94
|
+
when "ref"
|
|
95
|
+
defn = run.defs.rels[exp.ref]?[0]
|
|
96
|
+
return run defn, value if defn?
|
|
97
|
+
return builtin[exp.ref] value
|
|
98
|
+
when "dot"
|
|
99
|
+
return value[exp.dot]
|
|
100
|
+
when "comp"
|
|
101
|
+
return exp.comp.reduce (value, exp) ->
|
|
102
|
+
run exp, value unless value is undefined
|
|
103
|
+
, value
|
|
104
|
+
when "union"
|
|
105
|
+
for e in exp.union
|
|
106
|
+
result = run e, value
|
|
107
|
+
return result unless result is undefined
|
|
108
|
+
return undefined
|
|
109
|
+
when "vector"
|
|
110
|
+
result = []
|
|
111
|
+
for e in exp.vector
|
|
112
|
+
r = run e, value
|
|
113
|
+
return if r is undefined
|
|
114
|
+
result.push r
|
|
115
|
+
return result
|
|
116
|
+
when "product"
|
|
117
|
+
result = {}
|
|
118
|
+
for {label,exp} in exp.product
|
|
119
|
+
r = run exp, value
|
|
120
|
+
return if r is undefined
|
|
121
|
+
result[label] = r
|
|
122
|
+
return result
|
|
123
|
+
else
|
|
124
|
+
console.error exp.op
|
|
125
125
|
|
|
126
126
|
module.exports = run
|
package/run.js
CHANGED
|
@@ -120,15 +120,24 @@
|
|
|
120
120
|
};
|
|
121
121
|
|
|
122
122
|
verify = function(code, value) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
123
|
+
if (code == null) {
|
|
124
|
+
// representatives = run.defs.representatives
|
|
125
|
+
// defCodes = JSON.stringify run.defs.codes
|
|
126
|
+
// console.log {code,value, representatives, defCodes}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
126
129
|
switch (code.code) {
|
|
127
130
|
case "vector":
|
|
131
|
+
if (!Array.isArray(value)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
128
134
|
return value.every(function(x) {
|
|
129
135
|
return verify(code.vector, x);
|
|
130
136
|
});
|
|
131
137
|
case "product":
|
|
138
|
+
if ("object" !== typeof value) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
132
141
|
return (function(fields) {
|
|
133
142
|
if (fields.length !== Object.keys(code.product).length) {
|
|
134
143
|
return false;
|
|
@@ -138,6 +147,9 @@
|
|
|
138
147
|
});
|
|
139
148
|
})(Object.keys(value));
|
|
140
149
|
case "union":
|
|
150
|
+
if ("object" !== typeof value) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
141
153
|
return (function(fields) {
|
|
142
154
|
if (fields.length !== 1) {
|
|
143
155
|
return false;
|
|
@@ -155,83 +167,74 @@
|
|
|
155
167
|
};
|
|
156
168
|
|
|
157
169
|
run = function(exp, value) {
|
|
158
|
-
|
|
170
|
+
"use strict";
|
|
171
|
+
var defn, e, i, j, k, label, len, len1, len2, r, ref, ref1, ref2, ref3, result;
|
|
159
172
|
if (value === void 0) {
|
|
160
173
|
// console.log {exp,value}
|
|
161
174
|
return void 0;
|
|
162
175
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (verify(exp.code, value)) {
|
|
167
|
-
return value;
|
|
168
|
-
}
|
|
169
|
-
break;
|
|
170
|
-
case "identity":
|
|
176
|
+
switch (exp.op) {
|
|
177
|
+
case "code":
|
|
178
|
+
if (verify(exp.code, value)) {
|
|
171
179
|
return value;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return exp.vector.map(function(exp) {
|
|
200
|
-
var result;
|
|
201
|
-
result = run(exp, value);
|
|
202
|
-
if (result === void 0) {
|
|
203
|
-
throw new Error("Undefined");
|
|
204
|
-
}
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
case "identity":
|
|
183
|
+
return value;
|
|
184
|
+
case "str":
|
|
185
|
+
case "int":
|
|
186
|
+
return exp[exp.op];
|
|
187
|
+
case "ref":
|
|
188
|
+
defn = (ref = run.defs.rels[exp.ref]) != null ? ref[0] : void 0;
|
|
189
|
+
if (defn != null) {
|
|
190
|
+
return run(defn, value);
|
|
191
|
+
}
|
|
192
|
+
return builtin[exp.ref](value);
|
|
193
|
+
case "dot":
|
|
194
|
+
return value[exp.dot];
|
|
195
|
+
case "comp":
|
|
196
|
+
return exp.comp.reduce(function(value, exp) {
|
|
197
|
+
if (value !== void 0) {
|
|
198
|
+
return run(exp, value);
|
|
199
|
+
}
|
|
200
|
+
}, value);
|
|
201
|
+
case "union":
|
|
202
|
+
ref1 = exp.union;
|
|
203
|
+
for (i = 0, len = ref1.length; i < len; i++) {
|
|
204
|
+
e = ref1[i];
|
|
205
|
+
result = run(e, value);
|
|
206
|
+
if (result !== void 0) {
|
|
205
207
|
return result;
|
|
206
|
-
});
|
|
207
|
-
case "union":
|
|
208
|
-
ref1 = exp.union;
|
|
209
|
-
for (i = 0, len = ref1.length; i < len; i++) {
|
|
210
|
-
e = ref1[i];
|
|
211
|
-
result = (function() {
|
|
212
|
-
try {
|
|
213
|
-
return run(e, value);
|
|
214
|
-
} catch (error) {}
|
|
215
|
-
})();
|
|
216
|
-
if (result !== void 0) {
|
|
217
|
-
return result;
|
|
218
|
-
}
|
|
219
208
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
209
|
+
}
|
|
210
|
+
return void 0;
|
|
211
|
+
case "vector":
|
|
212
|
+
result = [];
|
|
213
|
+
ref2 = exp.vector;
|
|
214
|
+
for (j = 0, len1 = ref2.length; j < len1; j++) {
|
|
215
|
+
e = ref2[j];
|
|
216
|
+
r = run(e, value);
|
|
217
|
+
if (r === void 0) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
result.push(r);
|
|
221
|
+
}
|
|
222
|
+
return result;
|
|
223
|
+
case "product":
|
|
224
|
+
result = {};
|
|
225
|
+
ref3 = exp.product;
|
|
226
|
+
for (k = 0, len2 = ref3.length; k < len2; k++) {
|
|
227
|
+
({label, exp} = ref3[k]);
|
|
228
|
+
r = run(exp, value);
|
|
229
|
+
if (r === void 0) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
result[label] = r;
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
default:
|
|
236
|
+
return console.error(exp.op);
|
|
237
|
+
}
|
|
235
238
|
};
|
|
236
239
|
|
|
237
240
|
module.exports = run;
|
package/Examples/bnat.k
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
-- Standard binary encoding of natural
|
|
2
|
-
-- numbers with addition (+).
|
|
3
|
-
|
|
4
|
-
_ = {{} _};
|
|
5
|
-
_1 = {() "1"};
|
|
6
|
-
_0 = {() "0"};
|
|
7
|
-
|
|
8
|
-
n0 = _ ;
|
|
9
|
-
n1 = _ _1;
|
|
10
|
-
|
|
11
|
-
from_digit = <
|
|
12
|
-
[0, ()] EQ _, -- {_: {}}
|
|
13
|
-
[1, ()] EQ n1, -- {"1": {_: {}}}
|
|
14
|
-
[2, ()] EQ _ _0 _1, -- {"1": {"0": {_: {}}}}
|
|
15
|
-
[3, ()] EQ _ _1 _1, -- {"1": {"1": {_: {}}}}
|
|
16
|
-
[4, ()] EQ _ _0 _0 _1, -- {"1": {"0": {"0": {_: {}}}}}
|
|
17
|
-
[5, ()] EQ _ _1 _0 _1,
|
|
18
|
-
[6, ()] EQ _ _0 _1 _1,
|
|
19
|
-
[7, ()] EQ _ _1 _1 _1,
|
|
20
|
-
[8, ()] EQ _ _0 _0 _0 _1,
|
|
21
|
-
[9, ()] EQ _ _1 _0 _0 _1,
|
|
22
|
-
[10, ()] EQ _ _0 _1 _0 _1
|
|
23
|
-
>;
|
|
24
|
-
|
|
25
|
-
to_digit = <
|
|
26
|
-
._ 0,
|
|
27
|
-
."1"._ 1,
|
|
28
|
-
."1"."0"._ 2,
|
|
29
|
-
."1"."1"._ 3,
|
|
30
|
-
."1"."0"."0"._ 4,
|
|
31
|
-
."1"."0"."1"._ 5,
|
|
32
|
-
."1"."1"."0"._ 6,
|
|
33
|
-
."1"."1"."1"._ 7,
|
|
34
|
-
."1"."0"."0"."0"._ 8,
|
|
35
|
-
."1"."0"."0"."1"._ 9,
|
|
36
|
-
."1"."0"."1"."0"._ 10,
|
|
37
|
-
."1"."0"."1"."1"._ 11,
|
|
38
|
-
."1"."1"."0"."0"._ 12,
|
|
39
|
-
."1"."1"."0"."1"._ 13,
|
|
40
|
-
."1"."1"."1"."0"._ 14,
|
|
41
|
-
."1"."1"."1"."1"._ 15
|
|
42
|
-
> ;
|
|
43
|
-
|
|
44
|
-
remove_leading_zeros =
|
|
45
|
-
< ."0" remove_leading_zeros, () >;
|
|
46
|
-
|
|
47
|
-
_append_zero =
|
|
48
|
-
< ._ _ _0, -- ( _ -> 0 _)
|
|
49
|
-
."1" _append_zero _1, -- ('1 ... -> '1 [$.1 | _append_zero]),
|
|
50
|
-
."0" _append_zero _0 -- ('0 ... -> '0 [$.0 | _append_zero]),
|
|
51
|
-
>
|
|
52
|
-
;
|
|
53
|
-
|
|
54
|
-
_append_one =
|
|
55
|
-
<
|
|
56
|
-
._ _ _1,
|
|
57
|
-
."1" _append_one _1,
|
|
58
|
-
."0" _append_one _0
|
|
59
|
-
>
|
|
60
|
-
;
|
|
61
|
-
|
|
62
|
-
reverse_bnat = {() x, _ y} _reverse_bnat;
|
|
63
|
-
|
|
64
|
-
_reverse_bnat =
|
|
65
|
-
<
|
|
66
|
-
{.x._ x, .y y} .y,
|
|
67
|
-
{.x."0" x, .y _0 y} _reverse_bnat,
|
|
68
|
-
{.x."1" x, .y _1 y} _reverse_bnat
|
|
69
|
-
>;
|
|
70
|
-
|
|
71
|
-
_add2wc =
|
|
72
|
-
-- least significant bit first addition with carry
|
|
73
|
-
--- :{ :bnat x, :bnat y, :[0,1] c}
|
|
74
|
-
-- _log!
|
|
75
|
-
<
|
|
76
|
-
{.x."0" x, .y."0" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _0,
|
|
77
|
-
{.x."0" x, .y."0" y, .c."1" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
78
|
-
|
|
79
|
-
{.x."0" x, .y."1" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
80
|
-
{.x."0" x, .y."1" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
81
|
-
|
|
82
|
-
{.x."1" x, .y."0" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
83
|
-
{.x."1" x, .y."0" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
84
|
-
|
|
85
|
-
{.x."1" x, .y."1" y, .c."0" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
86
|
-
{.x."1" x, .y."1" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _1,
|
|
87
|
-
{.x._ x, .c."0" c, .y y} .y,
|
|
88
|
-
{.y._ y, .c."0" c, .x x} .x,
|
|
89
|
-
{.x._ x, .c."1" c, .y._ y} n1,
|
|
90
|
-
{.x._ x, .y."0" y, .c."1" c} .y _1,
|
|
91
|
-
{.x."0" x, .y._ y, .c."1" c} .x _1,
|
|
92
|
-
{.x._ x, .y."1" y, .c."1" c} {_ x, .y y, {} _1 c} _add2wc _0,
|
|
93
|
-
{.x."1" x, .y._ y, .c."1" c} {.x x, _ y, {} _1 c} _add2wc _0
|
|
94
|
-
>;
|
|
95
|
-
|
|
96
|
-
plus = <
|
|
97
|
-
SNOC {
|
|
98
|
-
x: .0 from_digit remove_leading_zeros reverse_bnat,
|
|
99
|
-
y: .1 plus remove_leading_zeros reverse_bnat,
|
|
100
|
-
c: {} _0 } _add2wc reverse_bnat,
|
|
101
|
-
.0 from_digit
|
|
102
|
-
>;
|
|
103
|
-
|
|
104
|
-
to_string = <
|
|
105
|
-
._ "0",
|
|
106
|
-
toString
|
|
107
|
-
>;
|
|
108
|
-
|
|
109
|
-
toString = <
|
|
110
|
-
._ "",
|
|
111
|
-
["0", ."0" toString] CONCAT,
|
|
112
|
-
["1", ."1" toString] CONCAT
|
|
113
|
-
>;
|
|
114
|
-
|
|
115
|
-
[(), plus]
|
|
116
|
-
{
|
|
117
|
-
input: .0,
|
|
118
|
-
sum: {raw: .1, digits: .1 <to_digit,"...too big...">, binary: .1 to_string}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
-- example: > echo '[9,9,9,8]' | ./k.sh -k ./bnat.k
|
package/Examples/code.k
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
inc = [(),1] PLUS;
|
|
2
|
-
max = <GT .0, .1>; -- select max from vector of two
|
|
3
|
-
-- Finding a max in any vector would be:
|
|
4
|
-
-- max = < SNOC [.0, .1 max] <GT .0, .1>, .0 >;
|
|
5
|
-
$tree = < string leaf, {tree left, tree right} tree>;
|
|
6
|
-
height = $tree <
|
|
7
|
-
.leaf 0,
|
|
8
|
-
.tree [.left height, .right height] max inc
|
|
9
|
-
> $int;
|
|
10
|
-
|
|
11
|
-
<$tree,
|
|
12
|
-
{tree:
|
|
13
|
-
{ left: { leaf: "a"},
|
|
14
|
-
right: { tree:
|
|
15
|
-
{ left: {leaf: "b"},
|
|
16
|
-
right: {leaf: "c"}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
> _log! height
|
package/Examples/error.k
DELETED
package/Examples/factorial.k
DELETED
package/Examples/fib.k
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
pos? = [(), 0] GT .0;
|
|
2
|
-
dec = [(), -1] PLUS;
|
|
3
|
-
fib_aux =
|
|
4
|
-
<
|
|
5
|
-
{ .n pos? dec n,
|
|
6
|
-
."fib(n),fib(n-1),..." [[.0,.1] PLUS, ()] CONS "fib(n),fib(n-1),..."
|
|
7
|
-
} fib_aux,
|
|
8
|
-
."fib(n),fib(n-1),..."
|
|
9
|
-
>
|
|
10
|
-
;
|
|
11
|
-
|
|
12
|
-
fib = {() n, [1,1] "fib(n),fib(n-1),..."}
|
|
13
|
-
fib_aux _log! .0;
|
|
14
|
-
|
|
15
|
-
----------------------
|
|
16
|
-
|
|
17
|
-
fib
|
package/Examples/pow.k
DELETED
package/Examples/rec-code.k
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
$ t = < { t l, int v, t r } t, {} _ >;
|
|
2
|
-
|
|
3
|
-
t =
|
|
4
|
-
$ {t l, int v, t r}
|
|
5
|
-
{() t}
|
|
6
|
-
$ t
|
|
7
|
-
;
|
|
8
|
-
|
|
9
|
-
empty = {{} _} $t;
|
|
10
|
-
|
|
11
|
-
leave =
|
|
12
|
-
$ int
|
|
13
|
-
{ empty l,
|
|
14
|
-
() v,
|
|
15
|
-
empty r
|
|
16
|
-
} t
|
|
17
|
-
$ t
|
|
18
|
-
;
|
|
19
|
-
|
|
20
|
-
sort =
|
|
21
|
-
$ [ int ]
|
|
22
|
-
<
|
|
23
|
-
SNOC { .0 x, .1 sort t } insert,
|
|
24
|
-
{ empty l, .0 v, empty r } t,
|
|
25
|
-
empty
|
|
26
|
-
>
|
|
27
|
-
$ t
|
|
28
|
-
;
|
|
29
|
-
|
|
30
|
-
insert =
|
|
31
|
-
$ { int x, t t }
|
|
32
|
-
<
|
|
33
|
-
[.t._ , .x leave ] .1,
|
|
34
|
-
[[.x, .t.t.v] GT .0, .t.t]
|
|
35
|
-
{
|
|
36
|
-
.1.l l,
|
|
37
|
-
.1.v v,
|
|
38
|
-
{.0 x, .1.r t} insert r
|
|
39
|
-
} t,
|
|
40
|
-
{
|
|
41
|
-
{.x x, .t.t.l t} insert l,
|
|
42
|
-
.t.t.v v,
|
|
43
|
-
.t.t.r r
|
|
44
|
-
} t
|
|
45
|
-
>
|
|
46
|
-
$ t
|
|
47
|
-
;
|
|
48
|
-
|
|
49
|
-
inc = $int [(),1] PLUS $int;
|
|
50
|
-
len =
|
|
51
|
-
< SNOC .1 len inc,
|
|
52
|
-
.0 1,
|
|
53
|
-
0
|
|
54
|
-
>
|
|
55
|
-
;
|
|
56
|
-
|
|
57
|
-
concat =
|
|
58
|
-
$ [ [ int ] ]
|
|
59
|
-
<
|
|
60
|
-
SNOC
|
|
61
|
-
< [.0 SNOC, .1] [.0.0, [.0.1, .1] CONS concat ] CONS,
|
|
62
|
-
[.0.0, .1 concat] CONS,
|
|
63
|
-
.1 concat
|
|
64
|
-
>,
|
|
65
|
-
.0,
|
|
66
|
-
[]
|
|
67
|
-
>
|
|
68
|
-
$ [ int ]
|
|
69
|
-
;
|
|
70
|
-
|
|
71
|
-
toVector =
|
|
72
|
-
$ t
|
|
73
|
-
< ._ [],
|
|
74
|
-
.t [.l toVector, [.v], .r toVector] concat
|
|
75
|
-
>
|
|
76
|
-
$ [ int ]
|
|
77
|
-
;
|
|
78
|
-
|
|
79
|
-
[len, sort toVector]
|
|
80
|
-
|
package/Examples/test.k
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
--------- comments start by #, --, or // ----------------------------------
|
|
2
|
-
< -- merge of 4 partial functions...
|
|
3
|
-
["x=", .x, " & y=", .y], -- produces a vector of 4 values, if fields 'x' and 'y' are present
|
|
4
|
-
["only x=", .x], -- produces a pair '["only x=", "value-of-x"]', for input like {"x":"value-of-x"}
|
|
5
|
-
-- it is defined only if field 'x' is present
|
|
6
|
-
["only y=", .y],
|
|
7
|
-
["no x nor y"] -- defined for all input, returns always the same one element vector
|
|
8
|
-
>
|
|
9
|
-
-- one of the string vectors is passed to the following partial function,
|
|
10
|
-
-- which produces a record (map) with one field "x&y", whose value is the
|
|
11
|
-
-- result of concatenating elements of the passed in vector
|
|
12
|
-
{ CONCAT "x&y" }
|
|
13
|
-
------------------------------------------------------------------------------
|
package/Examples/typed-fib.k
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
inc = [(),1] PLUS;
|
|
2
|
-
fib2 =
|
|
3
|
-
$ { int target, int x, int "fib(x)", int "fib(x-1)" }
|
|
4
|
-
_log!
|
|
5
|
-
<
|
|
6
|
-
{[.target,.x]GT.0 target ,
|
|
7
|
-
.x inc x ,
|
|
8
|
-
[."fib(x)",."fib(x-1)"]PLUS "fib(x)" ,
|
|
9
|
-
."fib(x)" "fib(x-1)" }
|
|
10
|
-
fib2,
|
|
11
|
-
|
|
12
|
-
{[.target,.x] EQ, ."fib(x)" result}
|
|
13
|
-
.result
|
|
14
|
-
>
|
|
15
|
-
$ int;
|
|
16
|
-
|
|
17
|
-
{() target, 0 x, 1 "fib(x)", 0 "fib(x-1)"} fib2
|
|
18
|
-
|
|
19
|
-
|