@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/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
- export const $stackFn = (f) => (isArray(f) ? defWord(f) : f);
7
- const compile = (prog) => prog.length > 0
8
- ? compL.apply(null, (prog.map((w) => !isFunction(w)
9
- ? (ctx) => (ctx[0].push(w), ctx)
10
- : w)))
11
- : (ctx) => ctx;
12
- /**
13
- * Takes a result tuple returned by {@link run} and unwraps one or more
14
- * items from result stack. If no `n` is given, defaults to single value
15
- * (TOS) and returns it as is. Returns an array for all other `n`.
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
- * Like {@link word}, but automatically calls {@link unwrap} on result context
52
- * to produced unwrapped value/tuple.
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
- * Executes TOS as stack function and places result back on d-stack. TOS
72
- * MUST be a valid word or quotation.
73
- *
74
- * ( x -- x() )
75
- *
76
- * @param ctx -
77
- */
78
- export const exec = (ctx) => ($(ctx[0], 1), $stackFn(ctx[0].pop())(ctx));
79
- /**
80
- * Expects a body and error handler quotation on stack. Executes body
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
- //////////////////// JS host calls ////////////////////
101
- /**
102
- * Expects TOS to be a quotation with a vanilla JS function as first
103
- * element. Calls fn with all remaining items in quot as arguments and
104
- * pushes result back on d-stack (even if fn returned `undefined`).
105
- *
106
- * ( [f ...] -- f(...) )
107
- *
108
- * @param ctx -
109
- */
110
- export const execjs = (ctx) => {
111
- const stack = ctx[0];
112
- $(stack, 1);
113
- const [fn, ...args] = stack.pop();
114
- stack.push(fn(...args));
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
  };