functionalscript 0.2.0 → 0.2.2

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.
Files changed (248) hide show
  1. package/dev/module.mjs +1 -1
  2. package/jsr.json +59 -59
  3. package/out/com/cpp/module.f.mjs +123 -0
  4. package/out/com/cpp/test.f.mjs +40 -0
  5. package/out/com/cpp/testlib.f.mjs +7 -0
  6. package/out/com/cs/module.f.mjs +95 -0
  7. package/out/com/cs/test.f.mjs +43 -0
  8. package/out/com/cs/testlib.f.mjs +7 -0
  9. package/out/com/rust/module.f.mjs +213 -0
  10. package/out/com/rust/test.f.mjs +123 -0
  11. package/out/com/rust/testlib.f.mjs +7 -0
  12. package/out/com/test/build.f.mjs +98 -0
  13. package/out/com/test/build.mjs +40 -0
  14. package/out/com/types/module.f.mjs +51 -0
  15. package/out/com/types/testlib.f.mjs +30 -0
  16. package/out/commonjs/build/module.f.mjs +107 -0
  17. package/out/commonjs/build/test.f.mjs +102 -0
  18. package/out/commonjs/module/function/module.f.mjs +15 -0
  19. package/out/commonjs/module/module.f.mjs +48 -0
  20. package/out/commonjs/module.f.mjs +10 -0
  21. package/out/commonjs/module.mjs +26 -0
  22. package/out/commonjs/package/dependencies/module.f.mjs +21 -0
  23. package/out/commonjs/package/dependencies/test.f.mjs +15 -0
  24. package/out/commonjs/package/module.f.mjs +40 -0
  25. package/out/commonjs/package/test.f.mjs +27 -0
  26. package/out/commonjs/path/module.f.mjs +171 -0
  27. package/out/commonjs/path/test.f.mjs +231 -0
  28. package/out/commonjs/test.mjs +87 -0
  29. package/out/dev/index.mjs +2 -0
  30. package/out/dev/module.f.mjs +2 -0
  31. package/out/dev/module.mjs +167 -0
  32. package/out/dev/test/module.f.mjs +134 -0
  33. package/out/dev/test.f.mjs +58 -0
  34. package/out/dev/test.mjs +52 -0
  35. package/out/djs/module.f.mjs +75 -0
  36. package/out/djs/parser/module.f.mjs +432 -0
  37. package/out/djs/parser/test.f.mjs +535 -0
  38. package/out/djs/test.f.mjs +84 -0
  39. package/out/djs/tokenizer/module.f.mjs +87 -0
  40. package/out/djs/tokenizer/test.f.mjs +480 -0
  41. package/out/fsc/module.f.mjs +105 -0
  42. package/out/fsc/test.f.mjs +19 -0
  43. package/out/fsm/module.f.mjs +80 -0
  44. package/out/fsm/test.f.mjs +138 -0
  45. package/out/html/module.f.mjs +94 -0
  46. package/out/html/test.f.mjs +45 -0
  47. package/out/issues/test.f.mjs +66 -0
  48. package/out/js/tokenizer/module.f.mjs +686 -0
  49. package/out/js/tokenizer/test.f.mjs +844 -0
  50. package/out/json/module.f.mjs +89 -0
  51. package/out/json/parser/module.f.mjs +224 -0
  52. package/out/json/parser/test.f.mjs +321 -0
  53. package/out/json/serializer/module.f.mjs +67 -0
  54. package/out/json/serializer/test.f.mjs +87 -0
  55. package/out/json/test.f.mjs +61 -0
  56. package/out/json/tokenizer/module.f.mjs +78 -0
  57. package/out/json/tokenizer/test.f.mjs +420 -0
  58. package/out/nanvm-lib/tests/test.f.mjs +87 -0
  59. package/out/nodejs/version/main.mjs +3 -0
  60. package/out/nodejs/version/module.f.mjs +34 -0
  61. package/out/nodejs/version/test.f.mjs +97 -0
  62. package/out/prime_field/module.f.mjs +87 -0
  63. package/out/prime_field/test.f.mjs +145 -0
  64. package/out/secp/module.f.mjs +113 -0
  65. package/out/secp/test.f.mjs +63 -0
  66. package/out/sha2/module.f.mjs +172 -0
  67. package/out/sha2/test.f.mjs +86 -0
  68. package/out/text/ascii/module.f.mjs +154 -0
  69. package/out/text/ascii/test.f.mjs +14 -0
  70. package/out/text/module.f.mjs +19 -0
  71. package/out/text/sgr/module.f.mjs +17 -0
  72. package/out/text/test.f.mjs +19 -0
  73. package/out/text/utf16/module.f.mjs +86 -0
  74. package/out/text/utf16/test.f.mjs +145 -0
  75. package/out/text/utf8/module.f.mjs +126 -0
  76. package/out/text/utf8/test.f.mjs +175 -0
  77. package/out/types/array/module.f.mjs +95 -0
  78. package/out/types/array/test.f.mjs +116 -0
  79. package/out/types/bigfloat/module.f.mjs +77 -0
  80. package/out/types/bigfloat/test.f.mjs +349 -0
  81. package/out/types/bigint/module.f.mjs +114 -0
  82. package/out/types/bigint/test.f.mjs +192 -0
  83. package/out/types/btree/find/module.f.mjs +137 -0
  84. package/out/types/btree/find/test.f.mjs +156 -0
  85. package/out/types/btree/module.f.mjs +34 -0
  86. package/out/types/btree/remove/module.f.mjs +209 -0
  87. package/out/types/btree/remove/test.f.mjs +638 -0
  88. package/out/types/btree/set/module.f.mjs +114 -0
  89. package/out/types/btree/set/test.f.mjs +390 -0
  90. package/out/types/btree/test.f.mjs +83 -0
  91. package/out/types/btree/types/module.f.mjs +50 -0
  92. package/out/types/byte_set/module.f.mjs +42 -0
  93. package/out/types/byte_set/test.f.mjs +123 -0
  94. package/out/types/function/compare/module.f.mjs +22 -0
  95. package/out/types/function/compare/test.f.mjs +8 -0
  96. package/out/types/function/module.f.mjs +44 -0
  97. package/out/types/function/operator/module.f.mjs +60 -0
  98. package/out/types/function/test.f.mjs +15 -0
  99. package/out/types/list/module.f.mjs +269 -0
  100. package/out/types/list/test.f.mjs +401 -0
  101. package/out/types/map/module.f.mjs +54 -0
  102. package/out/types/map/test.f.mjs +115 -0
  103. package/out/types/nibble_set/module.f.mjs +19 -0
  104. package/out/types/nibble_set/test.f.mjs +90 -0
  105. package/out/types/nullable/module.f.mjs +9 -0
  106. package/out/types/nullable/test.f.mjs +12 -0
  107. package/out/types/number/module.f.mjs +12 -0
  108. package/out/types/number/test.f.mjs +126 -0
  109. package/out/types/object/module.f.mjs +27 -0
  110. package/out/types/object/test.f.mjs +17 -0
  111. package/out/types/range/module.f.mjs +6 -0
  112. package/out/types/range/test.f.mjs +18 -0
  113. package/out/types/range_map/module.f.mjs +84 -0
  114. package/out/types/range_map/test.f.mjs +201 -0
  115. package/out/types/result/module.f.mjs +25 -0
  116. package/out/types/result/module.mjs +16 -0
  117. package/out/types/sorted_list/module.f.mjs +102 -0
  118. package/out/types/sorted_list/test.f.mjs +66 -0
  119. package/out/types/sorted_set/module.f.mjs +29 -0
  120. package/out/types/sorted_set/test.f.mjs +80 -0
  121. package/out/types/string/module.f.mjs +17 -0
  122. package/out/types/string/test.f.mjs +58 -0
  123. package/out/types/string_set/module.f.mjs +29 -0
  124. package/out/types/string_set/test.f.mjs +65 -0
  125. package/package.json +5 -4
  126. package/tsconfig.json +2 -2
  127. /package/{com → out/com}/cpp/module.f.d.mts +0 -0
  128. /package/{com → out/com}/cpp/test.f.d.mts +0 -0
  129. /package/{com → out/com}/cpp/testlib.f.d.mts +0 -0
  130. /package/{com → out/com}/cs/module.f.d.mts +0 -0
  131. /package/{com → out/com}/cs/test.f.d.mts +0 -0
  132. /package/{com → out/com}/cs/testlib.f.d.mts +0 -0
  133. /package/{com → out/com}/rust/module.f.d.mts +0 -0
  134. /package/{com → out/com}/rust/test.f.d.mts +0 -0
  135. /package/{com → out/com}/rust/testlib.f.d.mts +0 -0
  136. /package/{com → out/com}/test/build.d.mts +0 -0
  137. /package/{com → out/com}/test/build.f.d.mts +0 -0
  138. /package/{com → out/com}/types/module.f.d.mts +0 -0
  139. /package/{com → out/com}/types/testlib.f.d.mts +0 -0
  140. /package/{commonjs → out/commonjs}/build/module.f.d.mts +0 -0
  141. /package/{commonjs → out/commonjs}/build/test.f.d.mts +0 -0
  142. /package/{commonjs → out/commonjs}/module/function/module.f.d.mts +0 -0
  143. /package/{commonjs → out/commonjs}/module/module.f.d.mts +0 -0
  144. /package/{commonjs → out/commonjs}/module.d.mts +0 -0
  145. /package/{commonjs → out/commonjs}/module.f.d.mts +0 -0
  146. /package/{commonjs → out/commonjs}/package/dependencies/module.f.d.mts +0 -0
  147. /package/{commonjs → out/commonjs}/package/dependencies/test.f.d.mts +0 -0
  148. /package/{commonjs → out/commonjs}/package/module.f.d.mts +0 -0
  149. /package/{commonjs → out/commonjs}/package/test.f.d.mts +0 -0
  150. /package/{commonjs → out/commonjs}/path/module.f.d.mts +0 -0
  151. /package/{commonjs → out/commonjs}/path/test.f.d.mts +0 -0
  152. /package/{commonjs → out/commonjs}/test.d.mts +0 -0
  153. /package/{dev → out/dev}/index.d.mts +0 -0
  154. /package/{dev → out/dev}/module.d.mts +0 -0
  155. /package/{dev → out/dev}/module.f.d.mts +0 -0
  156. /package/{dev → out/dev}/test/module.f.d.mts +0 -0
  157. /package/{dev → out/dev}/test.d.mts +0 -0
  158. /package/{dev → out/dev}/test.f.d.mts +0 -0
  159. /package/{djs → out/djs}/module.f.d.mts +0 -0
  160. /package/{djs → out/djs}/parser/module.f.d.mts +0 -0
  161. /package/{djs → out/djs}/parser/test.f.d.mts +0 -0
  162. /package/{djs → out/djs}/test.f.d.mts +0 -0
  163. /package/{djs → out/djs}/tokenizer/module.f.d.mts +0 -0
  164. /package/{djs → out/djs}/tokenizer/test.f.d.mts +0 -0
  165. /package/{fsc → out/fsc}/module.f.d.mts +0 -0
  166. /package/{fsc → out/fsc}/test.f.d.mts +0 -0
  167. /package/{fsm → out/fsm}/module.f.d.mts +0 -0
  168. /package/{fsm → out/fsm}/test.f.d.mts +0 -0
  169. /package/{html → out/html}/module.f.d.mts +0 -0
  170. /package/{html → out/html}/test.f.d.mts +0 -0
  171. /package/{issues → out/issues}/test.f.d.mts +0 -0
  172. /package/{js → out/js}/tokenizer/module.f.d.mts +0 -0
  173. /package/{js → out/js}/tokenizer/test.f.d.mts +0 -0
  174. /package/{json → out/json}/module.f.d.mts +0 -0
  175. /package/{json → out/json}/parser/module.f.d.mts +0 -0
  176. /package/{json → out/json}/parser/test.f.d.mts +0 -0
  177. /package/{json → out/json}/serializer/module.f.d.mts +0 -0
  178. /package/{json → out/json}/serializer/test.f.d.mts +0 -0
  179. /package/{json → out/json}/test.f.d.mts +0 -0
  180. /package/{json → out/json}/tokenizer/module.f.d.mts +0 -0
  181. /package/{json → out/json}/tokenizer/test.f.d.mts +0 -0
  182. /package/{nanvm-lib → out/nanvm-lib}/tests/test.f.d.mts +0 -0
  183. /package/{nodejs → out/nodejs}/version/main.d.mts +0 -0
  184. /package/{nodejs → out/nodejs}/version/module.f.d.mts +0 -0
  185. /package/{nodejs → out/nodejs}/version/test.f.d.mts +0 -0
  186. /package/{prime_field → out/prime_field}/module.f.d.mts +0 -0
  187. /package/{prime_field → out/prime_field}/test.f.d.mts +0 -0
  188. /package/{secp → out/secp}/module.f.d.mts +0 -0
  189. /package/{secp → out/secp}/test.f.d.mts +0 -0
  190. /package/{sha2 → out/sha2}/module.f.d.mts +0 -0
  191. /package/{sha2 → out/sha2}/test.f.d.mts +0 -0
  192. /package/{text → out/text}/ascii/module.f.d.mts +0 -0
  193. /package/{text → out/text}/ascii/test.f.d.mts +0 -0
  194. /package/{text → out/text}/module.f.d.mts +0 -0
  195. /package/{text → out/text}/sgr/module.f.d.mts +0 -0
  196. /package/{text → out/text}/test.f.d.mts +0 -0
  197. /package/{text → out/text}/utf16/module.f.d.mts +0 -0
  198. /package/{text → out/text}/utf16/test.f.d.mts +0 -0
  199. /package/{text → out/text}/utf8/module.f.d.mts +0 -0
  200. /package/{text → out/text}/utf8/test.f.d.mts +0 -0
  201. /package/{types → out/types}/array/module.f.d.mts +0 -0
  202. /package/{types → out/types}/array/test.f.d.mts +0 -0
  203. /package/{types → out/types}/bigfloat/module.f.d.mts +0 -0
  204. /package/{types → out/types}/bigfloat/test.f.d.mts +0 -0
  205. /package/{types → out/types}/bigint/module.f.d.mts +0 -0
  206. /package/{types → out/types}/bigint/test.f.d.mts +0 -0
  207. /package/{types → out/types}/btree/find/module.f.d.mts +0 -0
  208. /package/{types → out/types}/btree/find/test.f.d.mts +0 -0
  209. /package/{types → out/types}/btree/module.f.d.mts +0 -0
  210. /package/{types → out/types}/btree/remove/module.f.d.mts +0 -0
  211. /package/{types → out/types}/btree/remove/test.f.d.mts +0 -0
  212. /package/{types → out/types}/btree/set/module.f.d.mts +0 -0
  213. /package/{types → out/types}/btree/set/test.f.d.mts +0 -0
  214. /package/{types → out/types}/btree/test.f.d.mts +0 -0
  215. /package/{types → out/types}/btree/types/module.f.d.mts +0 -0
  216. /package/{types → out/types}/byte_set/module.f.d.mts +0 -0
  217. /package/{types → out/types}/byte_set/test.f.d.mts +0 -0
  218. /package/{types → out/types}/function/compare/module.f.d.mts +0 -0
  219. /package/{types → out/types}/function/compare/test.f.d.mts +0 -0
  220. /package/{types → out/types}/function/module.f.d.mts +0 -0
  221. /package/{types → out/types}/function/operator/module.f.d.mts +0 -0
  222. /package/{types → out/types}/function/test.f.d.mts +0 -0
  223. /package/{types → out/types}/list/module.f.d.mts +0 -0
  224. /package/{types → out/types}/list/test.f.d.mts +0 -0
  225. /package/{types → out/types}/map/module.f.d.mts +0 -0
  226. /package/{types → out/types}/map/test.f.d.mts +0 -0
  227. /package/{types → out/types}/nibble_set/module.f.d.mts +0 -0
  228. /package/{types → out/types}/nibble_set/test.f.d.mts +0 -0
  229. /package/{types → out/types}/nullable/module.f.d.mts +0 -0
  230. /package/{types → out/types}/nullable/test.f.d.mts +0 -0
  231. /package/{types → out/types}/number/module.f.d.mts +0 -0
  232. /package/{types → out/types}/number/test.f.d.mts +0 -0
  233. /package/{types → out/types}/object/module.f.d.mts +0 -0
  234. /package/{types → out/types}/object/test.f.d.mts +0 -0
  235. /package/{types → out/types}/range/module.f.d.mts +0 -0
  236. /package/{types → out/types}/range/test.f.d.mts +0 -0
  237. /package/{types → out/types}/range_map/module.f.d.mts +0 -0
  238. /package/{types → out/types}/range_map/test.f.d.mts +0 -0
  239. /package/{types → out/types}/result/module.d.mts +0 -0
  240. /package/{types → out/types}/result/module.f.d.mts +0 -0
  241. /package/{types → out/types}/sorted_list/module.f.d.mts +0 -0
  242. /package/{types → out/types}/sorted_list/test.f.d.mts +0 -0
  243. /package/{types → out/types}/sorted_set/module.f.d.mts +0 -0
  244. /package/{types → out/types}/sorted_set/test.f.d.mts +0 -0
  245. /package/{types → out/types}/string/module.f.d.mts +0 -0
  246. /package/{types → out/types}/string/test.f.d.mts +0 -0
  247. /package/{types → out/types}/string_set/module.f.d.mts +0 -0
  248. /package/{types → out/types}/string_set/test.f.d.mts +0 -0
@@ -0,0 +1,123 @@
1
+ import * as _ from './module.f.mjs';
2
+ import * as list from '../list/module.f.mjs';
3
+ const { every, countdown, map, toArray } = list;
4
+ import * as json from '../../json/module.f.mjs';
5
+ import * as o from '../object/module.f.mjs';
6
+ const { sort } = o;
7
+ /** @type {(a: readonly json.Unknown[]) => string} */
8
+ const stringify = json.stringify(sort);
9
+ export default {
10
+ has: [
11
+ () => {
12
+ if (_.has(0)(_.empty)) {
13
+ throw _.empty;
14
+ }
15
+ if (_.has(1)(_.empty)) {
16
+ throw _.empty;
17
+ }
18
+ if (_.has(33)(_.empty)) {
19
+ throw _.empty;
20
+ }
21
+ },
22
+ () => {
23
+ const s = _.set(0)(_.empty);
24
+ if (s !== 1n) {
25
+ throw s;
26
+ }
27
+ if (!_.has(0)(s)) {
28
+ throw s;
29
+ }
30
+ if (_.has(1)(s)) {
31
+ throw s;
32
+ }
33
+ if (_.has(33)(s)) {
34
+ throw s;
35
+ }
36
+ },
37
+ () => {
38
+ const s = _.set(33)(_.empty);
39
+ if (s !== 8589934592n) {
40
+ throw s;
41
+ }
42
+ if (_.has(0)(s)) {
43
+ throw s;
44
+ }
45
+ if (_.has(1)(s)) {
46
+ throw s;
47
+ }
48
+ if (!_.has(33)(s)) {
49
+ throw s;
50
+ }
51
+ }
52
+ ],
53
+ setRange: () => {
54
+ const result = _.setRange([2, 5])(_.empty);
55
+ if (result !== 60n) {
56
+ throw result;
57
+ }
58
+ },
59
+ unset: [
60
+ () => {
61
+ const a = _.set(0)(_.empty);
62
+ const result = _.unset(0)(a);
63
+ if (result !== 0n) {
64
+ throw result;
65
+ }
66
+ },
67
+ () => {
68
+ const a = _.set(255)(_.empty);
69
+ const result = _.unset(255)(a);
70
+ if (result !== 0n) {
71
+ throw result;
72
+ }
73
+ }
74
+ ],
75
+ universe: () => {
76
+ const x = every(map(v => _.has(v)(_.universe))(countdown(256)));
77
+ if (!x) {
78
+ throw x;
79
+ }
80
+ },
81
+ compliment: {
82
+ empty: () => {
83
+ const r = _.complement(_.empty);
84
+ if (r !== _.universe) {
85
+ throw r;
86
+ }
87
+ },
88
+ universe: () => {
89
+ const r = _.complement(_.universe);
90
+ if (r !== _.empty) {
91
+ throw r;
92
+ }
93
+ },
94
+ },
95
+ toRangeMap: [
96
+ () => {
97
+ const result = stringify(toArray(_.toRangeMap(_.empty)('a')));
98
+ if (result !== '[]') {
99
+ throw result;
100
+ }
101
+ },
102
+ () => {
103
+ const s = _.set(0)(_.empty);
104
+ const result = stringify(toArray(_.toRangeMap(s)('a')));
105
+ if (result !== '[[["a"],0]]') {
106
+ throw result;
107
+ }
108
+ },
109
+ () => {
110
+ const s = _.setRange([1, 2])(_.empty);
111
+ const result = stringify(toArray(_.toRangeMap(s)('a')));
112
+ if (result !== '[[[],0],[["a"],2]]') {
113
+ throw result;
114
+ }
115
+ },
116
+ () => {
117
+ const result = stringify(toArray(_.toRangeMap(_.universe)('a')));
118
+ if (result !== '[[["a"],255]]') {
119
+ throw result;
120
+ }
121
+ },
122
+ ]
123
+ };
@@ -0,0 +1,22 @@
1
+ // @ts-self-types="./module.f.d.mts"
2
+ import * as Array from '../../array/module.f.mjs';
3
+ /** @typedef {Array.Index3} Index3 */
4
+ /** @typedef {Array.Index5} Index5 */
5
+ /**
6
+ * @template T
7
+ * @typedef {Array.Array2<T>} Array2
8
+ */
9
+ /** @typedef {-1|0|1} Sign */
10
+ /**
11
+ * @template T
12
+ * @typedef {(_: T) => Sign} Compare
13
+ */
14
+ /** @type {<T>(cmp: Compare<T>) => (value: T) => Index3} */
15
+ export const index3 = cmp => value => /** @type {Index3} */ (cmp(value) + 1);
16
+ /** @type {<T>(cmp: Compare<T>) => (v2: Array2<T>) => Index5} */
17
+ export const index5 = cmp => ([v0, v1]) => {
18
+ const _0 = cmp(v0);
19
+ return /** @type {Index5} */ (_0 <= 0 ? _0 + 1 : cmp(v1) + 3);
20
+ };
21
+ /** @type {<T>(a: T) => (b: T) => Sign} */
22
+ export const unsafeCmp = a => b => a < b ? -1 : a > b ? 1 : 0;
@@ -0,0 +1,8 @@
1
+ import * as _ from './module.f.mjs';
2
+ const { unsafeCmp } = _;
3
+ export default () => {
4
+ const result = unsafeCmp(true)(false);
5
+ if (result !== 1) {
6
+ throw result;
7
+ }
8
+ };
@@ -0,0 +1,44 @@
1
+ // @ts-self-types="./module.f.d.mts"
2
+ /**
3
+ * A generic function type.
4
+ *
5
+ * @template I
6
+ * @template O
7
+ * @typedef {(_: I) => O} Func
8
+ */
9
+ /**
10
+ * A postfix compose function.
11
+ *
12
+ * @type {<I, X>(g: Func<I, X>) => <O>(f: Func<X, O>) => Func<I, O>}
13
+ */
14
+ export const compose = g => f => x => f(g(x));
15
+ /**
16
+ * A generic identity function.
17
+ *
18
+ * @type {<T>(value: T) => T}
19
+ */
20
+ export const identity = value => value;
21
+ /**
22
+ * Flips the arguments of a curried function.
23
+ *
24
+ * @type {<A, B, C>(f: (a: A) => (b: B) => C) => (b: B) => (a: A) => C}
25
+ */
26
+ export const flip = f => b => a => f(a)(b);
27
+ /**
28
+ * A functional utility type that enables seamless chaining of transformations.
29
+ *
30
+ * @template I,O
31
+ * @typedef {{
32
+ * readonly result: Func<I, O>
33
+ * readonly then: <T>(g: Func<O, T>) => Fn<I, T>
34
+ * }} Fn
35
+ */
36
+ /**
37
+ * Creates an `Fn` instance from a function, enabling chaining of transformations.
38
+ *
39
+ * @type {<I, O>(f: (i: I) => O) => Fn<I, O>}
40
+ */
41
+ export const fn = result => ({
42
+ result,
43
+ then: g => fn(compose(result)(g))
44
+ });
@@ -0,0 +1,60 @@
1
+ // @ts-self-types="./module.f.d.mts"
2
+ /**
3
+ * @template A
4
+ * @template B
5
+ * @template R
6
+ * @typedef {(a: A) => (b: B) => R} Binary
7
+ */
8
+ /**
9
+ * @template I,O
10
+ * @typedef {Binary<I, O, O>} Fold
11
+ */
12
+ /** @type {(separator: string) => Reduce<string>} */
13
+ export const join = separator => value => prior => `${prior}${separator}${value}`;
14
+ /** @type {Reduce<string>} */
15
+ export const concat = i => acc => `${acc}${i}`;
16
+ /**
17
+ * @template T
18
+ * @template R
19
+ * @typedef {(value: T) => R} Unary
20
+ */
21
+ /** @type {Unary<boolean, boolean>} */
22
+ export const logicalNot = v => !v;
23
+ /**
24
+ * @template T
25
+ * @typedef {Binary<T, T, boolean>} Equal
26
+ */
27
+ /** @type {<T>(a: T) => (b: T) => boolean} */
28
+ export const strictEqual = a => b => a === b;
29
+ /**
30
+ * @template I,O
31
+ * @typedef {(input: I) => readonly[O, Scan<I,O>]} Scan
32
+ */
33
+ /**
34
+ * @template I,S,O
35
+ * @typedef {(prior: S) => (input: I) => readonly[O, S]} StateScan
36
+ */
37
+ /** @type {<I, S, O>(op: StateScan<I, S, O>) => (prior: S) => Scan<I, O>} */
38
+ export const stateScanToScan = op => prior => i => {
39
+ const [o, s] = op(prior)(i);
40
+ return [o, stateScanToScan(op)(s)];
41
+ };
42
+ /** @type {<I, O>(fold: Fold<I, O>) => (prior: O) => Scan<I, O>} */
43
+ export const foldToScan = fold => prior => i => {
44
+ const result = fold(i)(prior);
45
+ return [result, foldToScan(fold)(result)];
46
+ };
47
+ /**
48
+ * @template T
49
+ * @typedef {Fold<T, T>} Reduce
50
+ */
51
+ /** @type {<T>(fold: Reduce<T>) => Scan<T, T>} */
52
+ export const reduceToScan = op => init => [init, foldToScan(op)(init)];
53
+ /** @type {Reduce<number>} */
54
+ export const addition = a => b => a + b;
55
+ /** @type {Reduce<number>} */
56
+ export const min = a => b => a < b ? a : b;
57
+ /** @type {Reduce<number>} */
58
+ export const max = a => b => a > b ? a : b;
59
+ export const increment = addition(1);
60
+ export const counter = () => increment;
@@ -0,0 +1,15 @@
1
+ import * as _ from './module.f.mjs';
2
+ const { fn } = _;
3
+ export default () => {
4
+ /** @type {(x: string) => readonly[string]} */
5
+ const f = x => [x];
6
+ /** @type {(x: readonly[string]) => readonly[number]} */
7
+ const g = ([x]) => [x.length];
8
+ /** @type {(x: readonly[number]) => number} */
9
+ const w = ([x]) => x;
10
+ const r = fn(f).then(g).then(w).result;
11
+ const result = r('hello');
12
+ if (result !== 5) {
13
+ throw r;
14
+ }
15
+ };
@@ -0,0 +1,269 @@
1
+ // @ts-self-types="./module.f.d.mts"
2
+ import * as function_ from '../function/module.f.mjs';
3
+ const { identity, fn, compose } = function_;
4
+ import * as operator from '../function/operator/module.f.mjs';
5
+ const { addition, logicalNot, strictEqual, stateScanToScan, foldToScan, reduceToScan } = operator;
6
+ /**
7
+ * @template T
8
+ * @typedef {NotLazy<T> | Thunk<T>} List
9
+ */
10
+ /**
11
+ * @template T
12
+ * @typedef {|
13
+ * Result<T> |
14
+ * Concat<T> |
15
+ * readonly T[]
16
+ * } NotLazy
17
+ */
18
+ /** @typedef {null} Empty */
19
+ /**
20
+ * @template T
21
+ * @typedef {Empty | NonEmpty<T>} Result
22
+ */
23
+ /**
24
+ * @template T
25
+ * @typedef {() => List<T>} Thunk
26
+ */
27
+ /**
28
+ * @template T
29
+ * @typedef {{
30
+ * readonly first: T
31
+ * readonly tail: List<T>
32
+ * }} NonEmpty
33
+ */
34
+ /**
35
+ * @template T
36
+ * @typedef {{
37
+ * readonly head: List<T>
38
+ * readonly tail: List<T>
39
+ * }} Concat
40
+ */
41
+ /** @type {<T>(array: readonly T[]) => Result<T>} */
42
+ const fromArray = array => {
43
+ /** @typedef {typeof array extends readonly (infer T)[] ? T : never} T */
44
+ /** @type {(i: number) => Result<T>} */
45
+ const at = i => i < array.length ? { first: array[i], tail: () => at(i + 1) } : null;
46
+ return at(0);
47
+ };
48
+ /** @type {<T>(head: List<T>) => (tail: List<T>) => List<T>} */
49
+ export const concat = head => tail => tail === null ? head : ({ head, tail });
50
+ /** @type {<T>(list: List<T>) => NotLazy<T> } */
51
+ const trampoline = list => {
52
+ while (typeof list === 'function') {
53
+ list = list();
54
+ }
55
+ return list;
56
+ };
57
+ /** @type {<T>(list: List<T>) => Result<T>} */
58
+ export const next = head => {
59
+ /** @type {typeof head} */
60
+ let tail = null;
61
+ while (true) {
62
+ head = trampoline(head);
63
+ if (head instanceof Array) {
64
+ head = fromArray(head);
65
+ }
66
+ else if (head !== null && 'head' in head) {
67
+ [head, tail] = [head.head, concat(head.tail)(tail)];
68
+ continue;
69
+ }
70
+ if (head !== null) {
71
+ return { first: head.first, tail: concat(head.tail)(tail) };
72
+ }
73
+ if (tail === null) {
74
+ return null;
75
+ }
76
+ [head, tail] = [tail, null];
77
+ }
78
+ };
79
+ /** @type {<T>(list: List<T>) => Iterable<T>} */
80
+ export const iterable = list => ({
81
+ *[Symbol.iterator]() {
82
+ let i = list;
83
+ while (true) {
84
+ const r = next(i);
85
+ if (r === null) {
86
+ return;
87
+ }
88
+ yield r.first;
89
+ i = r.tail;
90
+ }
91
+ }
92
+ });
93
+ const { from } = Array;
94
+ /** @type {<T>(list: List<T>) => readonly T[]} */
95
+ export const toArray = list => {
96
+ const u = trampoline(list);
97
+ return u instanceof Array ? u : from(iterable(u));
98
+ };
99
+ /** @type {<I, O>(step: (n: NonEmpty<I>) => List<O>) => (input: List<I>) => Thunk<O>} */
100
+ const apply = f => input => () => {
101
+ const n = next(input);
102
+ if (n === null) {
103
+ return null;
104
+ }
105
+ return f(n);
106
+ };
107
+ /** @type {<T>(n: NonEmpty<List<T>>) => List<T>} */
108
+ const flatStep = ({ first, tail }) => concat(first)(flat(tail));
109
+ /** @type {<T>(list: List<List<T>>) => Thunk<T>} */
110
+ export const flat = apply(flatStep);
111
+ /** @type {<I, O>(f: (value: I) => O) => (n: NonEmpty<I>) => List<O>} */
112
+ const mapStep = f => ({ first, tail }) => ({ first: f(first), tail: map(f)(tail) });
113
+ /** @type {<I, O>(f: (value: I) => O) => (input: List<I>) => Thunk<O>} */
114
+ export const map = f => apply(mapStep(f));
115
+ /** @type {<I, O>(f: (value: I) => List<O>) => (input: List<I>) => Thunk<O>} */
116
+ export const flatMap = f => compose(map(f))(flat);
117
+ /** @type {<T>(f: (value: T) => boolean) => (n: NonEmpty<T>) => List<T>} */
118
+ const filterStep = f => ({ first, tail }) => {
119
+ const newTail = filter(f)(tail);
120
+ return f(first) ? { first, tail: newTail } : newTail;
121
+ };
122
+ /** @type {<T>(f: (value: T) => boolean) => (input: List<T>) => Thunk<T>} */
123
+ export const filter = f => apply(filterStep(f));
124
+ /** @type {<I, O>(f: (value: I) => O|null) => (n: NonEmpty<I>) => List<O>} */
125
+ const filterMapStep = f => n => {
126
+ const [first, tail] = [f(n.first), filterMap(f)(n.tail)];
127
+ return first === null ? tail : { first, tail };
128
+ };
129
+ /** @type {<I, O>(f: (value: I) => O|null) => (input: List<I>) => Thunk<O>} */
130
+ export const filterMap = f => apply(filterMapStep(f));
131
+ /** @type {<T>(f: (value: T) => boolean) => (n: NonEmpty<T>) => List<T>} */
132
+ const takeWhileStep = f => ({ first, tail }) => f(first) ? { first, tail: takeWhile(f)(tail) } : null;
133
+ /** @type {<T>(f: (value: T) => boolean) => (input: List<T>) => Thunk<T>} */
134
+ export const takeWhile = f => apply(takeWhileStep(f));
135
+ /** @type {(n: number) => <T>(result: NonEmpty<T>) => List<T>} */
136
+ const takeStep = n => ({ first, tail }) => 0 < n ? { first: first, tail: take(n - 1)(tail) } : null;
137
+ /** @type {(n: number) => <T>(input: List<T>) => Thunk<T>} */
138
+ export const take = n => apply(takeStep(n));
139
+ /** @type {<T>(f: (value: T) => boolean) => (ne: NonEmpty<T>) => List<T>} */
140
+ const dropWhileStep = f => ne => f(ne.first) ? dropWhile(f)(ne.tail) : ne;
141
+ /** @type {<T>(f: (value: T) => boolean) => (input: List<T>) => Thunk<T>} */
142
+ export const dropWhile = f => apply(dropWhileStep(f));
143
+ /** @type {(n: number) => <T>(ne: NonEmpty<T>) => List<T>} */
144
+ const dropStep = n => ne => 0 < n ? drop(n - 1)(ne.tail) : ne;
145
+ /** @type {(n: number) => <T>(input: List<T>) => Thunk<T>} */
146
+ export const drop = n => apply(dropStep(n));
147
+ /** @type {<D>(def: D) => <T>(input: List<T>) => D|T} */
148
+ export const first = def => input => {
149
+ const ne = next(input);
150
+ return ne === null ? def : ne.first;
151
+ };
152
+ /** @type {<D>(first: D) => <T>(tail: List<T>) => D|T} */
153
+ export const last = first => tail => {
154
+ /** @typedef {typeof tail extends List<infer T> ? T : never} T */
155
+ /** @type {NonEmpty<typeof first|T>} */
156
+ let i = { first, tail };
157
+ while (true) {
158
+ const result = next(i.tail);
159
+ if (result === null) {
160
+ return i.first;
161
+ }
162
+ i = result;
163
+ }
164
+ };
165
+ /** @type {<D>(def: D) => <T>(f: (value: T) => boolean) => (input: List<T>) => D|T} */
166
+ export const find = def => f => compose(filter(f))(first(def));
167
+ /** @type {(input: List<boolean>) => boolean} */
168
+ export const some = find(false)(identity);
169
+ /** @type {<T>(input: List<T>) => boolean} */
170
+ export const isEmpty = fn(map(() => true))
171
+ .then(some)
172
+ .then(logicalNot)
173
+ .result;
174
+ export const every = fn(map(logicalNot))
175
+ .then(some)
176
+ .then(logicalNot)
177
+ .result;
178
+ /** @type {<T>(value: T) => (sequence: List<T>) => boolean} */
179
+ export const includes = value => compose(map(strictEqual(value)))(some);
180
+ /** @type {(count: number) => Thunk<number>} */
181
+ export const countdown = count => () => {
182
+ if (count <= 0) {
183
+ return null;
184
+ }
185
+ const first = count - 1;
186
+ return { first, tail: countdown(first) };
187
+ };
188
+ /** @type {<T>(v: T) => (c: number) => Thunk<T>} */
189
+ export const repeat = v => compose(countdown)(map(() => v));
190
+ /** @type {<T>(list: List<T>) => List<T>} */
191
+ export const cycle = list => () => {
192
+ const i = next(list);
193
+ return i === null ? null : { first: i.first, tail: concat(i.tail)(cycle(list)) };
194
+ };
195
+ /** @type {<I, O>(op: operator.Scan<I, O>) => (ne: NonEmpty<I>) => List<O>} */
196
+ const scanStep = op => ne => {
197
+ const [first, newOp] = op(ne.first);
198
+ return { first, tail: scan(newOp)(ne.tail) };
199
+ };
200
+ /** @type {<I, O>(op: operator.Scan<I, O>) => (input: List<I>) => Thunk<O>} */
201
+ export const scan = op => apply(scanStep(op));
202
+ /** @type {<I, S, O>(op: operator.StateScan<I, S, O>) => (init: S) => (input: List<I>) => Thunk<O>} */
203
+ export const stateScan = op => compose(stateScanToScan(op))(scan);
204
+ /** @type {<I,O>(op: operator.Fold<I, O>) => (init: O) => (input: List<I>) => Thunk<O>} */
205
+ export const foldScan = op => compose(foldToScan(op))(scan);
206
+ /** @type {<I,O>(op: operator.Fold<I, O>) => (init: O) => (input: List<I>) => O} */
207
+ export const fold = op => init => compose(foldScan(op)(init))(last(init));
208
+ /** @type {<T>(op: operator.Reduce<T>) => <D>(def: D) => (input: List<T>) => D|T} */
209
+ export const reduce = op => def => compose(scan(reduceToScan(op)))(last(def));
210
+ /** @type {<T>(list: List<T>) => Thunk<number>} */
211
+ const lengthList = list => () => {
212
+ const notLazy = trampoline(list);
213
+ if (notLazy === null) {
214
+ return null;
215
+ }
216
+ if (notLazy instanceof Array) {
217
+ return [notLazy.length];
218
+ }
219
+ const tail = lengthList(notLazy.tail);
220
+ if ("first" in notLazy) {
221
+ return { first: 1, tail };
222
+ }
223
+ return { head: lengthList(notLazy.head), tail };
224
+ };
225
+ const sum = reduce(addition)(0);
226
+ /** @type {<T>(input: List<T>) => number} */
227
+ export const length = compose(lengthList)(sum);
228
+ /**
229
+ * @template T
230
+ * @typedef {readonly[number, T]} Entry
231
+ */
232
+ /** @type {(index: number) => <T>(value: T) => readonly[Entry<T>, number]} */
233
+ const entryOperator = index => value => [[index, value], index + 1];
234
+ /** @type {<T>(input: List<T>) => Thunk<Entry<T>>} */
235
+ export const entries = input => {
236
+ /** @typedef {typeof input extends List<infer T> ? T : never} T */
237
+ /** @type {operator.StateScan<T, Number, Entry<T>>} */
238
+ const o = entryOperator;
239
+ return stateScan(o)(0)(input);
240
+ };
241
+ /** @type {<T>(value: T) => (prior: List<T>) => List<T>} */
242
+ const reverseOperator = first => tail => ({ first, tail });
243
+ /** @type {<T>(input: List<T>) => List<T>} */
244
+ export const reverse = fold(reverseOperator)(null);
245
+ /** @type {<A>(a: List<A>) => <B>(b: List<B>) => Thunk<readonly[A, B]>} */
246
+ export const zip = a => b => () => {
247
+ const aResult = next(a);
248
+ if (aResult === null) {
249
+ return null;
250
+ }
251
+ const bResult = next(b);
252
+ if (bResult === null) {
253
+ return null;
254
+ }
255
+ return { first: [aResult.first, bResult.first], tail: zip(aResult.tail)(bResult.tail) };
256
+ };
257
+ /** @type {<T>(e: operator.Equal<T>) => (a: List<T>) => (b: List<T>) => boolean} */
258
+ export const equal = e => {
259
+ /** @typedef {typeof e extends operator.Equal<infer T> ? T : never} T */
260
+ /** @type {(a: List<T>) => (b: List<T>) => List<boolean>} */
261
+ const f = a => b => () => {
262
+ const [aResult, bResult] = [next(a), next(b)];
263
+ return aResult === null || bResult === null
264
+ ? { first: aResult === bResult, tail: null }
265
+ : { first: e(aResult.first)(bResult.first), tail: f(aResult.tail)(bResult.tail) };
266
+ };
267
+ return a => b => every(f(a)(b));
268
+ };
269
+ export const empty = null;