@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/cond.js CHANGED
@@ -2,82 +2,46 @@ import { illegalState } from "@thi.ng/errors/illegal-state";
2
2
  import { $ } from "./safe.js";
3
3
  import { nop } from "./stack.js";
4
4
  import { $stackFn } from "./word.js";
5
- //////////////////// Conditionals ////////////////////
6
- /**
7
- * Higher order word. Takes two stack programs: truthy and falsey
8
- * branches, respectively. When executed, pops TOS and runs only one of
9
- * the branches depending if TOS was truthy or not.
10
- *
11
- * Note: Unlike JS `if() {...} else {...}` constructs, the actual
12
- * conditional is NOT part of this word.
13
- *
14
- * ( bool -- ? )
15
- *
16
- * @param _then -
17
- * @param _else -
18
- */
19
- export const defCond = (_then, _else = nop) => (ctx) => {
20
- $(ctx[0], 1);
21
- return $stackFn(ctx[0].pop() ? _then : _else)(ctx);
5
+ const defCond = (_then, _else = nop) => (ctx) => {
6
+ $(ctx[0], 1);
7
+ return $stackFn(ctx[0].pop() ? _then : _else)(ctx);
22
8
  };
23
- /**
24
- * Non-HOF version of {@link cond}, expects `test` result and both branches on
25
- * d-stack. Executes `thenq` word/quotation if `test` is truthy, else
26
- * runs `elseq`.
27
- *
28
- * ( test thenq elseq -- ? )
29
- *
30
- * @param ctx -
31
- */
32
- export const condq = (ctx) => {
33
- const stack = ctx[0];
34
- $(stack, 3);
35
- const _else = stack.pop();
36
- const _then = stack.pop();
37
- return $stackFn(stack.pop() ? _then : _else)(ctx);
9
+ const condq = (ctx) => {
10
+ const stack = ctx[0];
11
+ $(stack, 3);
12
+ const _else = stack.pop();
13
+ const _then = stack.pop();
14
+ return $stackFn(stack.pop() ? _then : _else)(ctx);
38
15
  };
39
- /**
40
- * Similar to {@link condq}, but only expects `test` result and truthy branch
41
- * d-stack. Executes word/quotation if `test` is truthy, else does nothing.
42
- *
43
- * ( test whenq -- ? )
44
- *
45
- * @param ctx -
46
- */
47
- export const whenq = (ctx) => {
48
- const stack = ctx[0];
49
- $(stack, 2);
50
- const _then = stack.pop();
51
- return stack.pop() ? $stackFn(_then)(ctx) : ctx;
16
+ const whenq = (ctx) => {
17
+ const stack = ctx[0];
18
+ $(stack, 2);
19
+ const _then = stack.pop();
20
+ return stack.pop() ? $stackFn(_then)(ctx) : ctx;
52
21
  };
53
- /**
54
- * Higher order word. Takes an object of stack programs with keys in the
55
- * object being used to check for equality with TOS. If a match is
56
- * found, executes corresponding stack program. If a `default` key is
57
- * specified and no other cases matched, run `default` program. In all
58
- * other cases throws an error.
59
- *
60
- * Important: The default case has the original TOS re-added to the
61
- * d-stack before execution.
62
- *
63
- * @param cases -
64
- */
65
- export const defCases = (cases) => (ctx) => {
66
- $(ctx[0], 1);
67
- const stack = ctx[0];
68
- const tos = stack.pop();
69
- const cas = cases[tos];
70
- if (cas !== undefined) {
71
- return $stackFn(cas)(ctx);
72
- }
73
- if (cases.default) {
74
- stack.push(tos);
75
- return $stackFn(cases.default)(ctx);
76
- }
77
- return illegalState(`no matching case for: ${tos}`);
22
+ const defCases = (cases) => (ctx) => {
23
+ $(ctx[0], 1);
24
+ const stack = ctx[0];
25
+ const tos = stack.pop();
26
+ const cas = cases[tos];
27
+ if (cas !== void 0) {
28
+ return $stackFn(cas)(ctx);
29
+ }
30
+ if (cases.default) {
31
+ stack.push(tos);
32
+ return $stackFn(cases.default)(ctx);
33
+ }
34
+ return illegalState(`no matching case for: ${tos}`);
78
35
  };
79
- export const casesq = (ctx) => {
80
- const stack = ctx[0];
81
- $(stack, 2);
82
- return defCases(stack.pop())(ctx);
36
+ const casesq = (ctx) => {
37
+ const stack = ctx[0];
38
+ $(stack, 2);
39
+ return defCases(stack.pop())(ctx);
40
+ };
41
+ export {
42
+ casesq,
43
+ condq,
44
+ defCases,
45
+ defCond,
46
+ whenq
83
47
  };
package/context.js CHANGED
@@ -1,12 +1,8 @@
1
- /**
2
- * Creates a new StackContext tuple from given d-stack and/or
3
- * environment only (the r-stack is always initialized empty).
4
- *
5
- * @param stack - initial d-stack
6
- * @param env - initial environment
7
- */
8
- export const ctx = (stack = [], env = {}) => [
9
- stack,
10
- [],
11
- env,
1
+ const ctx = (stack = [], env = {}) => [
2
+ stack,
3
+ [],
4
+ env
12
5
  ];
6
+ export {
7
+ ctx
8
+ };
package/dataflow.js CHANGED
@@ -2,173 +2,59 @@ import { and, or } from "./logic.js";
2
2
  import { $ } from "./safe.js";
3
3
  import { dup, dup2, dup3, over, swap } from "./stack.js";
4
4
  import { $stackFn, defWord, exec } from "./word.js";
5
- //////////////////// Dataflow combinators ////////////////////
6
- // these combinators have been ported from Factor:
7
- // http://docs.factorcode.org:8080/content/article-dataflow-combinators.html
8
- /**
9
- * Removes `x` from d-stack, calls `q` and restores `x` to the top of
10
- * the d-stack after quotation is finished.
11
- *
12
- * ( x q -- x )
13
- *
14
- * Same behavior as: `[swap, movdr, exec, movrd]`, only the current
15
- * implementation doesn't use r-stack and stashes `x` off stack.
16
- *
17
- * @param ctx -
18
- */
19
- export const dip = (ctx) => {
20
- const stack = ctx[0];
21
- $(stack, 2);
22
- const q = stack.pop();
23
- const x = stack.pop();
24
- ctx = $stackFn(q)(ctx);
25
- ctx[0].push(x);
26
- return ctx;
5
+ const dip = (ctx) => {
6
+ const stack = ctx[0];
7
+ $(stack, 2);
8
+ const q = stack.pop();
9
+ const x = stack.pop();
10
+ ctx = $stackFn(q)(ctx);
11
+ ctx[0].push(x);
12
+ return ctx;
13
+ };
14
+ const dip2 = defWord([swap, [dip], dip]);
15
+ const dip3 = defWord([swap, [dip2], dip]);
16
+ const dip4 = defWord([swap, [dip3], dip]);
17
+ const keep = defWord([over, [exec], dip]);
18
+ const keep2 = defWord([[dup2], dip, dip2]);
19
+ const keep3 = defWord([[dup3], dip, dip3]);
20
+ const bi = defWord([[keep], dip, exec]);
21
+ const bi2 = defWord([[keep2], dip, exec]);
22
+ const bi3 = defWord([[keep3], dip, exec]);
23
+ const tri = defWord([[[keep], dip, keep], dip, exec]);
24
+ const tri2 = defWord([[[keep2], dip, keep2], dip, exec]);
25
+ const tri3 = defWord([[[keep3], dip, keep3], dip, exec]);
26
+ const bis = defWord([[dip], dip, exec]);
27
+ const bis2 = defWord([[dip2], dip, exec]);
28
+ const tris = defWord([[[dip2], dip, dip], dip, exec]);
29
+ const tris2 = defWord([[dip4], dip2, bis2]);
30
+ const bia = defWord([dup, bis]);
31
+ const bia2 = defWord([dup, bis2]);
32
+ const tria = defWord([dup, dup, tris]);
33
+ const tria2 = defWord([dup, dup, tris2]);
34
+ const both = defWord([bia, and]);
35
+ const either = defWord([bia, or]);
36
+ export {
37
+ bi,
38
+ bi2,
39
+ bi3,
40
+ bia,
41
+ bia2,
42
+ bis,
43
+ bis2,
44
+ both,
45
+ dip,
46
+ dip2,
47
+ dip3,
48
+ dip4,
49
+ either,
50
+ keep,
51
+ keep2,
52
+ keep3,
53
+ tri,
54
+ tri2,
55
+ tri3,
56
+ tria,
57
+ tria2,
58
+ tris,
59
+ tris2
27
60
  };
28
- /**
29
- * Removes `x y` from d-stack, calls `q` and restores removed vals
30
- * to the top of the d-stack after quotation is finished.
31
- *
32
- * ( x y q -- x y )
33
- */
34
- export const dip2 = defWord([swap, [dip], dip]);
35
- /**
36
- * Removes `x y z` from d-stack, calls `q` and restores removed
37
- * vals to the top of the d-stack after quotation is finished.
38
- *
39
- * ( x y z q -- x y z )
40
- */
41
- export const dip3 = defWord([swap, [dip2], dip]);
42
- /**
43
- * Removes `x y z w` from d-stack, calls `q` and restores removed
44
- * vals to the top of the d-stack after quotation is finished.
45
- *
46
- * ( x y z w q -- x y z w )
47
- */
48
- export const dip4 = defWord([swap, [dip3], dip]);
49
- /**
50
- * Calls a quotation with a value on the d-stack, restoring the value
51
- * after quotation finished.
52
- *
53
- * ( x q -- .. x )
54
- */
55
- export const keep = defWord([over, [exec], dip]);
56
- /**
57
- * Call a quotation with two values on the stack, restoring the values
58
- * after quotation finished.
59
- *
60
- * ( x y q -- .. x y )
61
- */
62
- export const keep2 = defWord([[dup2], dip, dip2]);
63
- /**
64
- * Call a quotation with three values on the stack, restoring the values
65
- * after quotation finished.
66
- *
67
- * ( x y z q -- .. x y z )
68
- */
69
- export const keep3 = defWord([[dup3], dip, dip3]);
70
- /**
71
- * First applies `p` to the value `x`, then applies `q` to the same
72
- * value.
73
- *
74
- * ( x p q -- px qx )
75
- */
76
- export const bi = defWord([[keep], dip, exec]);
77
- /**
78
- * First applies `p` to the two input values `x y`, then applies `q` to
79
- * the same values.
80
- *
81
- * ( x y p q -- pxy qxy )
82
- */
83
- export const bi2 = defWord([[keep2], dip, exec]);
84
- /**
85
- * First applies `p` to the three input values `x y z`, then applies `q`
86
- * to the same values.
87
- *
88
- * ( x y z p q -- pxyz qxyz )
89
- */
90
- export const bi3 = defWord([[keep3], dip, exec]);
91
- /**
92
- * Applies `p` to `x`, then `q` to `x`, and finally `r` to `x`
93
- *
94
- * ( x p q r -- px qx rx )
95
- */
96
- export const tri = defWord([[[keep], dip, keep], dip, exec]);
97
- /**
98
- * Applies `p` to the two input values `x y`, then same with `q`, and
99
- * finally with `r`.
100
- *
101
- * ( x y p q r -- pxy qxy rxy )
102
- */
103
- export const tri2 = defWord([[[keep2], dip, keep2], dip, exec]);
104
- /**
105
- * Applies `p` to the three input values `x y z`, then same with `q`,
106
- * and finally with `r`.
107
- *
108
- * ( x y z p q r -- pxyz qxyz rxyz )
109
- */
110
- export const tri3 = defWord([[[keep3], dip, keep3], dip, exec]);
111
- /**
112
- * Applies `p` to `x`, then applies `q` to `y`.
113
- *
114
- * ( x y p q -- px qy )
115
- */
116
- export const bis = defWord([[dip], dip, exec]);
117
- /**
118
- * Applies `p` to `a b`, then applies `q` to `c d`.
119
- *
120
- * ( a b c d p q -- pab qcd )
121
- */
122
- export const bis2 = defWord([[dip2], dip, exec]);
123
- /**
124
- * Applies `p` to `x`, then `q` to `y`, and finally `r` to `z`.
125
- *
126
- * ( x y z p q r -- )
127
- */
128
- export const tris = defWord([[[dip2], dip, dip], dip, exec]);
129
- /**
130
- * Applies `p` to `u v`, then `q` to `w x`, and finally `r` to `y z`.
131
- *
132
- * ( u v w x y z p q r -- puv qwx ryz )
133
- */
134
- export const tris2 = defWord([[dip4], dip2, bis2]);
135
- /**
136
- * Applies the quotation `q` to `x`, then to `y`.
137
- *
138
- * ( x y q -- qx qy )
139
- */
140
- export const bia = defWord([dup, bis]);
141
- /**
142
- * Applies the quotation `q` to `x y`, then to `z w`.
143
- *
144
- * ( x y z w q -- qxy qzw )
145
- */
146
- export const bia2 = defWord([dup, bis2]);
147
- /**
148
- * Applies the `q` to `x`, then to `y`, and finally to `z`.
149
- *
150
- * ( x y z q -- qx qy qz )
151
- */
152
- export const tria = defWord([dup, dup, tris]);
153
- /**
154
- * Applies the quotation to `u v`, then to `w x`, and then to `y z`.
155
- *
156
- * ( u v w x y z q -- quv qwx qyz )
157
- */
158
- export const tria2 = defWord([dup, dup, tris2]);
159
- /**
160
- * Applies `q` individually to both input vals `x y` and combines
161
- * results with `and`. The final result will be true if both interim
162
- * results were truthy.
163
- *
164
- * ( x y q -- qx && qy )
165
- */
166
- export const both = defWord([bia, and]);
167
- /**
168
- * Applies `q` individually to both input vals `x y` and combines results with `or`.
169
- * The final result will be true if at least one of the interim results
170
- * was truthy.
171
- *
172
- * ( x y q -- qx || qy )
173
- */
174
- export const either = defWord([bia, or]);
package/env.js CHANGED
@@ -1,68 +1,29 @@
1
1
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
2
2
  import { $ } from "./safe.js";
3
- //////////////////// Environment ////////////////////
4
- /**
5
- * Pushes current env onto d-stack.
6
- *
7
- * ( -- env )
8
- *
9
- * @param ctx -
10
- * @param env -
11
- */
12
- export const pushenv = (ctx) => (ctx[0].push(ctx[2]), ctx);
13
- /**
14
- * Loads value for `key` from current env and pushes it on d-stack.
15
- * Throws error if var doesn't exist.
16
- *
17
- * ( key -- env[key] )
18
- *
19
- * @param ctx -
20
- * @param env -
21
- */
22
- export const load = (ctx) => {
23
- const stack = ctx[0];
24
- $(stack, 1);
25
- const id = stack.pop();
26
- !ctx[2].hasOwnProperty(id) && illegalArgs(`unknown var: ${id}`);
27
- stack.push(ctx[2][id]);
28
- return ctx;
3
+ const pushenv = (ctx) => (ctx[0].push(ctx[2]), ctx);
4
+ const load = (ctx) => {
5
+ const stack = ctx[0];
6
+ $(stack, 1);
7
+ const id = stack.pop();
8
+ !ctx[2].hasOwnProperty(id) && illegalArgs(`unknown var: ${id}`);
9
+ stack.push(ctx[2][id]);
10
+ return ctx;
29
11
  };
30
- /**
31
- * Stores `val` under `key` in env.
32
- *
33
- * ( val key -- )
34
- *
35
- * @param ctx -
36
- * @param env -
37
- */
38
- export const store = (ctx) => ($(ctx[0], 2), (ctx[2][ctx[0].pop()] = ctx[0].pop()), ctx);
39
- /**
40
- * Higher order word. Similar to {@link load}, but always uses given
41
- * preconfigured `key` instead of reading it from d-stack at runtime
42
- * (also slightly faster). Throws error if var doesn't exist.
43
- *
44
- * ( -- env[key] )
45
- * @param ctx -
46
- * @param env -
47
- */
48
- export const defLoadKey = (key) => (ctx) => {
49
- !ctx[2].hasOwnProperty(key) &&
50
- illegalArgs(`unknown var: ${key.toString()}`);
51
- ctx[0].push(ctx[2][key]);
52
- return ctx;
12
+ const store = (ctx) => ($(ctx[0], 2), ctx[2][ctx[0].pop()] = ctx[0].pop(), ctx);
13
+ const defLoadKey = (key) => (ctx) => {
14
+ !ctx[2].hasOwnProperty(key) && illegalArgs(`unknown var: ${key.toString()}`);
15
+ ctx[0].push(ctx[2][key]);
16
+ return ctx;
53
17
  };
54
- /**
55
- * Higher order word. Similar to {@link store}, but always uses given
56
- * preconfigure `key` instead of reading it from d-stack at runtime
57
- * (also slightly faster).
58
- *
59
- * ( val -- )
60
- *
61
- * @param ctx -
62
- * @param env -
63
- */
64
- export const defStoreKey = (key) => (ctx) => {
65
- $(ctx[0], 1);
66
- ctx[2][key] = ctx[0].pop();
67
- return ctx;
18
+ const defStoreKey = (key) => (ctx) => {
19
+ $(ctx[0], 1);
20
+ ctx[2][key] = ctx[0].pop();
21
+ return ctx;
22
+ };
23
+ export {
24
+ defLoadKey,
25
+ defStoreKey,
26
+ load,
27
+ pushenv,
28
+ store
68
29
  };
package/index.js CHANGED
@@ -14,5 +14,12 @@ export * from "./safe.js";
14
14
  export * from "./stack.js";
15
15
  export * from "./string.js";
16
16
  export * from "./word.js";
17
- export { $ as ensureStack, $n as ensureStackN } from "./safe.js";
18
- export { defOp1 as maptos, defOp2 as map2, defOp2v as op2v } from "./ops.js";
17
+ import { $, $n } from "./safe.js";
18
+ import { defOp1, defOp2, defOp2v } from "./ops.js";
19
+ export {
20
+ $ as ensureStack,
21
+ $n as ensureStackN,
22
+ defOp2 as map2,
23
+ defOp1 as maptos,
24
+ defOp2v as op2v
25
+ };
package/io.js CHANGED
@@ -1,12 +1,9 @@
1
1
  import { $ } from "./safe.js";
2
- //////////////////// I/O ////////////////////
3
- /**
4
- * Prints TOS to console
5
- *
6
- * ( x -- )
7
- *
8
- * @param ctx -
9
- */
10
- export const print = (ctx) => ($(ctx[0], 1), console.log(ctx[0].pop()), ctx);
11
- export const printds = (ctx) => (console.log(ctx[0]), ctx);
12
- export const printrs = (ctx) => (console.log(ctx[1]), ctx);
2
+ const print = (ctx) => ($(ctx[0], 1), console.log(ctx[0].pop()), ctx);
3
+ const printds = (ctx) => (console.log(ctx[0]), ctx);
4
+ const printrs = (ctx) => (console.log(ctx[1]), ctx);
5
+ export {
6
+ print,
7
+ printds,
8
+ printrs
9
+ };
package/logic.js CHANGED
@@ -1,87 +1,32 @@
1
1
  import { equiv as _equiv } from "@thi.ng/equiv";
2
2
  import { defOp1, defOp2 } from "./ops.js";
3
- //////////////////// Logic ops ////////////////////
4
- /**
5
- * ( x y -- x===y )
6
- *
7
- * @param ctx -
8
- */
9
- export const eq = defOp2((b, a) => a === b);
10
- /**
11
- * ( x y -- equiv(x,y) )
12
- *
13
- * @param ctx -
14
- */
15
- export const equiv = defOp2(_equiv);
16
- /**
17
- * ( x y -- x!==y )
18
- *
19
- * @param ctx -
20
- */
21
- export const neq = defOp2((b, a) => a !== b);
22
- /**
23
- * ( x y -- x&&y )
24
- *
25
- * @param ctx -
26
- */
27
- export const and = defOp2((b, a) => !!a && !!b);
28
- /**
29
- * ( x y -- x||y )
30
- *
31
- * @param ctx -
32
- */
33
- export const or = defOp2((b, a) => !!a || !!b);
34
- /**
35
- * ( x -- !x )
36
- *
37
- * @param ctx -
38
- */
39
- export const not = defOp1((x) => !x);
40
- /**
41
- * ( x y -- x<y )
42
- *
43
- * @param ctx -
44
- */
45
- export const lt = defOp2((b, a) => a < b);
46
- /**
47
- * ( x y -- x>y )
48
- *
49
- * @param ctx -
50
- */
51
- export const gt = defOp2((b, a) => a > b);
52
- /**
53
- * ( x y -- x<=y )
54
- *
55
- * @param ctx -
56
- */
57
- export const lteq = defOp2((b, a) => a <= b);
58
- /**
59
- * ( x y -- x>=y )
60
- *
61
- * @param ctx -
62
- */
63
- export const gteq = defOp2((b, a) => a >= b);
64
- /**
65
- * ( x -- x===0 )
66
- *
67
- * @param ctx -
68
- */
69
- export const iszero = defOp1((x) => x === 0);
70
- /**
71
- * ( x -- x>0 )
72
- *
73
- * @param ctx -
74
- */
75
- export const ispos = defOp1((x) => x > 0);
76
- /**
77
- * ( x -- x<0 )
78
- *
79
- * @param ctx -
80
- */
81
- export const isneg = defOp1((x) => x < 0);
82
- /**
83
- * ( x -- x==null )
84
- *
85
- * @param ctx -
86
- */
87
- export const isnull = defOp1((x) => x == null);
3
+ const eq = defOp2((b, a) => a === b);
4
+ const equiv = defOp2(_equiv);
5
+ const neq = defOp2((b, a) => a !== b);
6
+ const and = defOp2((b, a) => !!a && !!b);
7
+ const or = defOp2((b, a) => !!a || !!b);
8
+ const not = defOp1((x) => !x);
9
+ const lt = defOp2((b, a) => a < b);
10
+ const gt = defOp2((b, a) => a > b);
11
+ const lteq = defOp2((b, a) => a <= b);
12
+ const gteq = defOp2((b, a) => a >= b);
13
+ const iszero = defOp1((x) => x === 0);
14
+ const ispos = defOp1((x) => x > 0);
15
+ const isneg = defOp1((x) => x < 0);
16
+ const isnull = defOp1((x) => x == null);
17
+ export {
18
+ and,
19
+ eq,
20
+ equiv,
21
+ gt,
22
+ gteq,
23
+ isneg,
24
+ isnull,
25
+ ispos,
26
+ iszero,
27
+ lt,
28
+ lteq,
29
+ neq,
30
+ not,
31
+ or
32
+ };