@thi.ng/pointfree 3.1.54 → 3.1.55
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/api.js +0 -1
- package/array.js +150 -358
- package/binary.js +16 -43
- package/cond.js +38 -74
- package/context.js +7 -11
- package/dataflow.js +55 -169
- package/env.js +24 -63
- package/index.js +9 -2
- package/io.js +8 -11
- package/logic.js +30 -85
- package/loop.js +32 -81
- package/math.js +50 -145
- package/ops.js +42 -71
- package/package.json +11 -8
- package/run.js +18 -37
- package/safe.js +14 -11
- package/stack.js +163 -339
- package/string.js +22 -35
- package/word.js +40 -107
package/word.js
CHANGED
|
@@ -3,114 +3,47 @@ import { isFunction } from "@thi.ng/checks/is-function";
|
|
|
3
3
|
import { compL } from "@thi.ng/compose/comp";
|
|
4
4
|
import { $ } from "./safe.js";
|
|
5
5
|
import { tos } from "./stack.js";
|
|
6
|
-
|
|
7
|
-
const compile = (prog) => prog.length > 0
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* @param result -
|
|
18
|
-
* @param n -
|
|
19
|
-
*/
|
|
20
|
-
export const unwrap = ([stack], n = 1) => n === 1 ? tos(stack) : stack.slice(Math.max(0, stack.length - n));
|
|
21
|
-
//////////////////// Dynamic words & quotations ////////////////////
|
|
22
|
-
/**
|
|
23
|
-
* Higher order word. Takes a StackProgram and returns it as StackFn to
|
|
24
|
-
* be used like any word. Unknown stack effect.
|
|
25
|
-
*
|
|
26
|
-
* If the optional `env` is given, uses a shallow copy of that
|
|
27
|
-
* environment (one per invocation) instead of the current one passed by
|
|
28
|
-
* {@link run} at runtime. If `mergeEnv` is true (default), the user
|
|
29
|
-
* provided env will be merged with the current env (also shallow
|
|
30
|
-
* copies). This is useful in conjunction with {@link pushenv} and {@link store}
|
|
31
|
-
* or `storekey()` to save results of sub procedures in the main env.
|
|
32
|
-
*
|
|
33
|
-
* Note: The provided (or merged) env is only active within the
|
|
34
|
-
* execution scope of the word.
|
|
35
|
-
*
|
|
36
|
-
* ( ? -- ? )
|
|
37
|
-
*
|
|
38
|
-
* @param prog -
|
|
39
|
-
* @param env -
|
|
40
|
-
* @param mergeEnv -
|
|
41
|
-
*/
|
|
42
|
-
export const defWord = (prog, env, mergeEnv = true) => {
|
|
43
|
-
const w = compile(prog);
|
|
44
|
-
return env
|
|
45
|
-
? mergeEnv
|
|
46
|
-
? (ctx) => (w([ctx[0], ctx[1], { ...ctx[2], ...env }]), ctx)
|
|
47
|
-
: (ctx) => (w([ctx[0], ctx[1], { ...env }]), ctx)
|
|
48
|
-
: w;
|
|
6
|
+
const $stackFn = (f) => isArray(f) ? defWord(f) : f;
|
|
7
|
+
const compile = (prog) => prog.length > 0 ? compL.apply(
|
|
8
|
+
null,
|
|
9
|
+
prog.map(
|
|
10
|
+
(w) => !isFunction(w) ? (ctx) => (ctx[0].push(w), ctx) : w
|
|
11
|
+
)
|
|
12
|
+
) : (ctx) => ctx;
|
|
13
|
+
const unwrap = ([stack], n = 1) => n === 1 ? tos(stack) : stack.slice(Math.max(0, stack.length - n));
|
|
14
|
+
const defWord = (prog, env, mergeEnv = true) => {
|
|
15
|
+
const w = compile(prog);
|
|
16
|
+
return env ? mergeEnv ? (ctx) => (w([ctx[0], ctx[1], { ...ctx[2], ...env }]), ctx) : (ctx) => (w([ctx[0], ctx[1], { ...env }]), ctx) : w;
|
|
49
17
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
* **Importatant:** Words defined with this function CANNOT be used as
|
|
55
|
-
* part of a larger stack program, only for standalone use.
|
|
56
|
-
*
|
|
57
|
-
* @param prog -
|
|
58
|
-
* @param n -
|
|
59
|
-
* @param env -
|
|
60
|
-
* @param mergeEnv -
|
|
61
|
-
*/
|
|
62
|
-
export const defWordU = (prog, n = 1, env, mergeEnv = true) => {
|
|
63
|
-
const w = compile(prog);
|
|
64
|
-
return env
|
|
65
|
-
? mergeEnv
|
|
66
|
-
? (ctx) => unwrap(w([ctx[0], ctx[1], { ...ctx[2], ...env }]), n)
|
|
67
|
-
: (ctx) => unwrap(w([ctx[0], ctx[1], { ...env }]), n)
|
|
68
|
-
: (ctx) => unwrap(w(ctx), n);
|
|
18
|
+
const defWordU = (prog, n = 1, env, mergeEnv = true) => {
|
|
19
|
+
const w = compile(prog);
|
|
20
|
+
return env ? mergeEnv ? (ctx) => unwrap(w([ctx[0], ctx[1], { ...ctx[2], ...env }]), n) : (ctx) => unwrap(w([ctx[0], ctx[1], { ...env }]), n) : (ctx) => unwrap(w(ctx), n);
|
|
69
21
|
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
* within an implicit `try .. catch` and if an error was thrown pushes
|
|
82
|
-
* it on stack and executes error quotation.
|
|
83
|
-
*
|
|
84
|
-
* ( body catch -- ? )
|
|
85
|
-
*
|
|
86
|
-
* @param ctx -
|
|
87
|
-
*/
|
|
88
|
-
export const $try = (ctx) => {
|
|
89
|
-
const stack = ctx[0];
|
|
90
|
-
$(stack, 2);
|
|
91
|
-
const err = stack.pop();
|
|
92
|
-
try {
|
|
93
|
-
return exec(ctx);
|
|
94
|
-
}
|
|
95
|
-
catch (e) {
|
|
96
|
-
stack.push(e, err);
|
|
97
|
-
return exec(ctx);
|
|
98
|
-
}
|
|
22
|
+
const exec = (ctx) => ($(ctx[0], 1), $stackFn(ctx[0].pop())(ctx));
|
|
23
|
+
const $try = (ctx) => {
|
|
24
|
+
const stack = ctx[0];
|
|
25
|
+
$(stack, 2);
|
|
26
|
+
const err = stack.pop();
|
|
27
|
+
try {
|
|
28
|
+
return exec(ctx);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
stack.push(e, err);
|
|
31
|
+
return exec(ctx);
|
|
32
|
+
}
|
|
99
33
|
};
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return ctx;
|
|
34
|
+
const execjs = (ctx) => {
|
|
35
|
+
const stack = ctx[0];
|
|
36
|
+
$(stack, 1);
|
|
37
|
+
const [fn, ...args] = stack.pop();
|
|
38
|
+
stack.push(fn(...args));
|
|
39
|
+
return ctx;
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
$stackFn,
|
|
43
|
+
$try,
|
|
44
|
+
defWord,
|
|
45
|
+
defWordU,
|
|
46
|
+
exec,
|
|
47
|
+
execjs,
|
|
48
|
+
unwrap
|
|
116
49
|
};
|