functionalscript 0.4.2 → 0.4.4

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 (76) hide show
  1. package/README.md +1 -1
  2. package/bnf/data/module.f.d.ts +12 -0
  3. package/bnf/data/module.f.js +85 -0
  4. package/bnf/data/test.f.d.ts +4 -0
  5. package/bnf/data/test.f.js +8 -0
  6. package/bnf/module.f.d.ts +55 -0
  7. package/bnf/module.f.js +98 -0
  8. package/bnf/test.f.d.ts +4 -0
  9. package/bnf/test.f.js +7 -0
  10. package/bnf/testlib.f.d.ts +3 -0
  11. package/bnf/{tag/test.f.js → testlib.f.js} +48 -44
  12. package/crypto/hmac/module.f.d.ts +28 -0
  13. package/crypto/hmac/module.f.js +60 -0
  14. package/crypto/hmac/test.f.d.ts +6 -0
  15. package/crypto/hmac/test.f.js +24 -0
  16. package/crypto/secp/module.f.d.ts +8 -0
  17. package/crypto/secp/module.f.js +30 -0
  18. package/crypto/secp/test.f.d.ts +3 -0
  19. package/crypto/secp/test.f.js +67 -3
  20. package/crypto/sha2/module.f.d.ts +10 -0
  21. package/crypto/sha2/module.f.js +11 -2
  22. package/crypto/sha2/test.f.js +28 -21
  23. package/djs/ast/module.f.d.ts +10 -0
  24. package/djs/ast/module.f.js +52 -0
  25. package/djs/ast/test.f.d.ts +8 -0
  26. package/djs/ast/test.f.js +41 -0
  27. package/djs/module.f.d.ts +5 -15
  28. package/djs/module.f.js +1 -62
  29. package/djs/parser/module.f.d.ts +9 -9
  30. package/djs/parser/module.f.js +7 -8
  31. package/djs/parser/test.f.js +97 -97
  32. package/djs/serializer/module.f.d.ts +8 -4
  33. package/djs/serializer/module.f.js +9 -27
  34. package/djs/{test.f.d.ts → serializer/test.f.d.ts} +3 -3
  35. package/djs/{test.f.js → serializer/test.f.js} +13 -13
  36. package/djs/tokenizer/test.f.js +2 -2
  37. package/djs/transpiler/module.f.d.ts +15 -0
  38. package/djs/transpiler/module.f.js +57 -0
  39. package/djs/transpiler/test.f.d.ts +8 -0
  40. package/djs/transpiler/test.f.js +74 -0
  41. package/io/module.f.d.ts +12 -0
  42. package/io/virtual-io.f.d.ts +3 -0
  43. package/io/virtual-io.f.js +10 -0
  44. package/js/tokenizer/test.f.js +2 -2
  45. package/json/module.f.d.ts +2 -1
  46. package/json/tokenizer/test.f.js +2 -2
  47. package/nanvm-lib/tests/test.f.js +4 -4
  48. package/package.json +2 -2
  49. package/path/module.f.d.ts +2 -0
  50. package/path/module.f.js +34 -0
  51. package/path/test.f.d.ts +5 -0
  52. package/path/test.f.js +49 -0
  53. package/text/sgr/module.f.d.ts +19 -1
  54. package/text/sgr/module.f.js +26 -1
  55. package/text/sgr/test.f.d.ts +2 -0
  56. package/text/sgr/test.f.js +8 -0
  57. package/text/utf16/module.f.d.ts +116 -0
  58. package/text/utf16/module.f.js +285 -0
  59. package/text/utf16/test.f.d.ts +4 -0
  60. package/text/utf16/test.f.js +28 -0
  61. package/types/bigint/module.f.d.ts +83 -2
  62. package/types/bigint/module.f.js +98 -2
  63. package/types/bigint/test.f.d.ts +4 -0
  64. package/types/bigint/test.f.js +66 -6
  65. package/types/bit_vec/module.f.d.ts +1 -1
  66. package/types/monoid/module.f.d.ts +3 -1
  67. package/types/monoid/module.f.js +2 -0
  68. package/types/number/module.f.d.ts +4 -0
  69. package/types/number/module.f.js +16 -0
  70. package/types/number/test.f.d.ts +1 -0
  71. package/types/number/test.f.js +19 -3
  72. package/types/object/module.f.d.ts +18 -0
  73. package/types/object/module.f.js +1 -1
  74. package/bnf/tag/module.f.d.ts +0 -30
  75. package/bnf/tag/module.f.js +0 -37
  76. /package/{bnf/tag/test.f.d.ts → io/module.f.js} +0 -0
@@ -1,5 +1,5 @@
1
1
  /**
2
- * A collection of utility functions for working with `bigint` values.
2
+ * Utility functions for working with `bigint` values.
3
3
  *
4
4
  * @module
5
5
  *
@@ -14,14 +14,58 @@
14
14
  * const bitCount = bitLength(255n) // 8n
15
15
  * const bitmask = mask(5n) // 31n
16
16
  * const m = min(3n)(13n) // 3n
17
+ * const c = combination([3n, 2n, 1n]) // 60n
17
18
  * ```
18
19
  */
19
20
  import { unsafeCmp } from "../function/compare/module.f.js";
20
21
  import { reduce } from "../list/module.f.js";
22
+ /**
23
+ * Adds two `bigint` values.
24
+ *
25
+ * @param a - The first bigint value.
26
+ * @returns A function that takes the second bigint value and returns the sum.
27
+ */
21
28
  export const addition = a => b => a + b;
29
+ /**
30
+ * Calculates the sum of a list of `bigint` values.
31
+ *
32
+ * @param input - A list of bigint values.
33
+ * @returns The sum of all values in the list.
34
+ */
22
35
  export const sum = reduce(addition)(0n);
36
+ /**
37
+ * Multiplies two `bigint` values.
38
+ *
39
+ * @param a - The first bigint value.
40
+ * @returns A function that takes the second bigint value and returns the product.
41
+ */
42
+ export const multiple = a => b => a * b;
43
+ /**
44
+ * Calculates the product of a list of `bigint` values.
45
+ *
46
+ * @param input - A list of bigint values.
47
+ * @returns The product of all values in the list.
48
+ */
49
+ export const product = reduce(multiple)(1n);
50
+ /**
51
+ * Calculates the absolute value of a `bigint`.
52
+ *
53
+ * @param a - The bigint value.
54
+ * @returns The absolute value of the input bigint.
55
+ */
23
56
  export const abs = a => a >= 0 ? a : -a;
57
+ /**
58
+ * Determines the sign of a `bigint`.
59
+ * @param a - The bigint value.
60
+ * @returns `1` if positive, `-1` if negative, and `0` if zero.
61
+ */
24
62
  export const sign = (a) => unsafeCmp(a)(0n);
63
+ /**
64
+ * Serializes a `bigint` to a string representation.
65
+ *
66
+ * @param a - The bigint value.
67
+ * @returns A string representation of the bigint (e.g., '123n').
68
+ */
25
69
  export const serialize = (a) => `${a}n`;
26
70
  const { isFinite } = Number;
27
71
  const { log2: mathLog2 } = Math;
@@ -139,6 +183,58 @@ export const bitLength = (v) => {
139
183
  */
140
184
  export const mask = (len) => (1n << len) - 1n;
141
185
  /**
142
- * A minimal value.
186
+ * Returns the smaller of two `bigint` values.
187
+ *
188
+ * @param a - The first bigint.
189
+ * @returns A function that takes the second bigint and returns the smaller value.
143
190
  */
144
191
  export const min = (a) => (b) => a < b ? a : b;
192
+ /**
193
+ * Returns the larger of two `bigint` values.
194
+ *
195
+ * @param a - The first bigint.
196
+ * @returns A function that takes the second bigint and returns the larger value.
197
+ */
198
+ export const max = (a) => (b) => a < b ? b : a;
199
+ /**
200
+ * Calculates the partial factorial `b!/a!`.
201
+ *
202
+ * @param a - The starting bigint value.
203
+ * @returns A function that takes `b` and computes `b!/a!`.
204
+ */
205
+ export const partialFactorial = (a) => (b) => {
206
+ let result = b;
207
+ while (true) {
208
+ --b;
209
+ if (b <= a) {
210
+ return result;
211
+ }
212
+ result *= b;
213
+ }
214
+ };
215
+ /**
216
+ * Calculates the factorial of a `bigint`.
217
+ *
218
+ * @param b - The bigint value.
219
+ * @returns The factorial of the input.
220
+ */
221
+ export const factorial = partialFactorial(1n);
222
+ /**
223
+ * Calculates the number of combinations for a list of `bigint` values.
224
+ *
225
+ * @param k - A list of bigint values.
226
+ * @returns The number of combinations.
227
+ */
228
+ export const combination = (...k) => {
229
+ let s = 0n;
230
+ let m = 1n;
231
+ let p = 1n;
232
+ for (let i of k) {
233
+ s += i;
234
+ if (i >= m) {
235
+ [i, m] = [m, i];
236
+ }
237
+ p *= factorial(i);
238
+ }
239
+ return partialFactorial(m)(s) / p;
240
+ };
@@ -23,5 +23,9 @@ declare const _default: {
23
23
  big: () => void;
24
24
  neg: (() => void)[];
25
25
  };
26
+ factorial: () => void;
27
+ combination: () => void;
28
+ combination50x50: () => void;
29
+ combination3: () => void;
26
30
  };
27
31
  export default _default;
@@ -1,4 +1,4 @@
1
- import { sum, abs, serialize, log2, bitLength, mask, min } from "./module.f.js";
1
+ import { sum, abs, serialize, log2, bitLength, mask, min, combination, factorial } from "./module.f.js";
2
2
  const oldLog2 = (v) => {
3
3
  if (v <= 0n) {
4
4
  return -1n;
@@ -174,15 +174,19 @@ export default {
174
174
  if (m !== 3n) {
175
175
  throw m;
176
176
  }
177
+ const c = combination(3n, 2n, 1n); // 60n
178
+ if (c !== 60n) {
179
+ throw c;
180
+ }
177
181
  },
178
182
  benchmark: () => {
179
183
  const list = {
180
- strBinLog2,
181
- strHexLog2,
184
+ // strBinLog2,
185
+ // strHexLog2,
182
186
  str32Log2,
183
- oldLog2,
184
- clz32Log2,
185
- m1023log2,
187
+ // oldLog2,
188
+ // clz32Log2,
189
+ // m1023log2,
186
190
  log2,
187
191
  };
188
192
  const transform = (b) => Object.fromEntries(Object.entries(list).map(([k, f]) => [k, b(f)]));
@@ -391,5 +395,61 @@ export default {
391
395
  }
392
396
  },
393
397
  ]
398
+ },
399
+ factorial: () => {
400
+ {
401
+ const r = factorial(3n);
402
+ if (r !== 6n) {
403
+ throw r;
404
+ }
405
+ }
406
+ {
407
+ const r = factorial(5n);
408
+ if (r !== 120n) {
409
+ throw r;
410
+ }
411
+ }
412
+ },
413
+ combination: () => {
414
+ const r = combination(2n, 3n);
415
+ if (r != 120n / (2n * 6n)) {
416
+ throw r;
417
+ }
418
+ },
419
+ combination50x50: () => {
420
+ {
421
+ const r = combination(1n, 1n);
422
+ if (r !== 2n) {
423
+ throw r;
424
+ }
425
+ }
426
+ {
427
+ const r = combination(2n, 2n);
428
+ if (r !== 6n) {
429
+ throw r;
430
+ }
431
+ }
432
+ {
433
+ const r = combination(3n, 3n);
434
+ if (r !== 20n) {
435
+ throw r;
436
+ }
437
+ }
438
+ {
439
+ const r = combination(4n, 4n);
440
+ if (r !== 70n) {
441
+ throw r;
442
+ }
443
+ }
444
+ },
445
+ combination3: () => {
446
+ const r = combination(2n, 3n, 4n, 2n);
447
+ // 2! * 3! * 4! * 2! : 2! * 2! * 3!
448
+ // 2+3+4+2 = 5*6*7*8*9*10*11
449
+ // e = 5 * 6 * 7 * 8 * 9 * 10 * 11 / (2n * 2n * 6n) =
450
+ // e = 5 * 7 * 2 * 9 * 10 * 11 = 69300
451
+ if (r !== 69300n) {
452
+ throw r;
453
+ }
394
454
  }
395
455
  };
@@ -6,7 +6,7 @@ export type Vec = bigint;
6
6
  /**
7
7
  * An empty vector of bits.
8
8
  */
9
- export declare const empty = 1n;
9
+ export declare const empty: Vec;
10
10
  /**
11
11
  * Calculates the length of the given vector of bits.
12
12
  */
@@ -1,4 +1,4 @@
1
- import type { Reduce } from "../function/operator/module.f.ts";
1
+ import type { Reduce } from '../function/operator/module.f.ts';
2
2
  /**
3
3
  * Represents a monoid, an algebraic structure with a binary operation
4
4
  * and an identity (neutral) element.
@@ -52,6 +52,8 @@ export type Monoid<T> = {
52
52
  * @returns A function that takes an element `a` and a repetition count `n`,
53
53
  * and returns the result of applying the operation `n` times.
54
54
  *
55
+ * See also {@link https://en.wikipedia.org/wiki/Exponentiation_by_squaring}.
56
+ *
55
57
  * @example
56
58
  *
57
59
  * ```ts
@@ -7,6 +7,8 @@
7
7
  * @returns A function that takes an element `a` and a repetition count `n`,
8
8
  * and returns the result of applying the operation `n` times.
9
9
  *
10
+ * See also {@link https://en.wikipedia.org/wiki/Exponentiation_by_squaring}.
11
+ *
10
12
  * @example
11
13
  *
12
14
  * ```ts
@@ -4,3 +4,7 @@ export declare const sum: (input: List<number>) => number;
4
4
  export declare const min: (input: List<number>) => number | null;
5
5
  export declare const max: (input: List<number>) => number | null;
6
6
  export declare const cmp: (a: number) => (b: number) => Sign;
7
+ /**
8
+ * Count a number of ones in 32 bit number
9
+ */
10
+ export declare const countOnes: (n: number) => number;
@@ -5,3 +5,19 @@ export const sum = reduce(addition)(0);
5
5
  export const min = reduce(minOp)(null);
6
6
  export const max = reduce(maxOp)(null);
7
7
  export const cmp = unsafeCmp;
8
+ const mo = [
9
+ [0x5555_5555, 1],
10
+ [0x3333_3333, 2],
11
+ [0x0F0F_0F0F, 4],
12
+ [0x00FF_00FF, 8],
13
+ [0x0000_FFFF, 16],
14
+ ];
15
+ /**
16
+ * Count a number of ones in 32 bit number
17
+ */
18
+ export const countOnes = (n) => {
19
+ for (const [mask, offset] of mo) {
20
+ n = (n & mask) + ((n >> offset) & mask);
21
+ }
22
+ return n;
23
+ };
@@ -7,5 +7,6 @@ declare const _default: {
7
7
  max: () => void;
8
8
  cmp: () => void;
9
9
  standard: () => void;
10
+ countOnes: (() => void)[];
10
11
  };
11
12
  export default _default;
@@ -1,4 +1,4 @@
1
- import { sum, min, max, cmp } from "./module.f.js";
1
+ import { sum, min, max, cmp, countOnes } from "./module.f.js";
2
2
  export default {
3
3
  sum: () => {
4
4
  const result = sum([2, 3, 4, 5]);
@@ -34,7 +34,7 @@ export default {
34
34
  },
35
35
  standard: () => {
36
36
  const check = a => b => {
37
- if (BigInt(Number(a)) != b) {
37
+ if (BigInt(Number(a)) !== b) {
38
38
  throw [a, b];
39
39
  }
40
40
  };
@@ -119,5 +119,21 @@ export default {
119
119
  // 4_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_1234
120
120
  check(104002482718319358n)(104002482718319360n);
121
121
  check(142693895881191444n)(142693895881191440n);
122
- }
122
+ },
123
+ countOnes: [
124
+ () => {
125
+ // 1121 2231 = 5 + 8 = 13
126
+ const r = countOnes(0x1234_5678);
127
+ if (r !== 13) {
128
+ throw r;
129
+ }
130
+ },
131
+ () => {
132
+ // 2232 3340 = 9 + 10 = 19
133
+ const r = countOnes(0x9ABC_DEF0);
134
+ if (r !== 19) {
135
+ throw r;
136
+ }
137
+ }
138
+ ]
123
139
  };
@@ -8,3 +8,21 @@ export declare const at: (name: string) => <T>(object: Map<T>) => T | null;
8
8
  export declare const sort: <T>(e: List<Entry<T>>) => List<Entry<T>>;
9
9
  export declare const fromEntries: <T>(e: List<Entry<T>>) => Map<T>;
10
10
  export declare const fromMap: <T>(m: BtMap<T>) => Map<T>;
11
+ /**
12
+ * A set of objects with a single key.
13
+ *
14
+ * See also
15
+ * https://stackoverflow.com/questions/57571664/typescript-type-for-an-object-with-only-one-key-no-union-type-allowed-as-a-key
16
+ */
17
+ export type OneKey<K extends string, V> = {
18
+ [P in K]: (Record<P, V> & Partial<Record<Exclude<K, P>, never>>) extends infer O ? {
19
+ [Q in keyof O]: O[Q];
20
+ } : never;
21
+ }[K];
22
+ /**
23
+ * https://stackoverflow.com/questions/61112584/typing-a-single-record-entry
24
+ */
25
+ export type NotUnion<T, U = T> = T extends unknown ? [
26
+ U
27
+ ] extends [T] ? T : never : never;
28
+ export type SingleProperty<T extends Record<string, never>> = keyof T extends NotUnion<keyof T> ? T : never;
@@ -3,7 +3,7 @@ import { entries as mapEntries, fromEntries as mapFromEntries } from "../map/mod
3
3
  const { getOwnPropertyDescriptor, fromEntries: objectFromEntries } = Object;
4
4
  export const at = name => object => {
5
5
  const r = getOwnPropertyDescriptor(object, name);
6
- return r === void 0 ? null : r.value;
6
+ return r === undefined ? null : r.value;
7
7
  };
8
8
  export const sort = e => mapEntries(mapFromEntries(e));
9
9
  export const fromEntries = e => objectFromEntries(iterable(e));
@@ -1,30 +0,0 @@
1
- import { type Array2 } from '../../types/array/module.f.ts';
2
- export type Range = bigint;
3
- export type Sequence = readonly Rule[];
4
- export type Or = {
5
- readonly [k in string]: Rule;
6
- };
7
- export type DataRule = Or | Sequence | Range | string;
8
- export type LazyRule = () => DataRule;
9
- export type Rule = DataRule | LazyRule;
10
- export declare const rangeEncode: (a: number, b: number) => Range;
11
- export declare const oneEncode: (a: number) => Range;
12
- export declare const rangeDecode: (r: bigint) => Array2<number>;
13
- export declare const str: (s: string) => readonly Range[] | Range;
14
- export declare const set: (s: string) => Or;
15
- export declare const range: (ab: string) => Range;
16
- export type None = readonly [];
17
- export declare const none: None;
18
- export type Option<S> = {
19
- none: None;
20
- some: S;
21
- };
22
- export declare const option: <S extends Rule>(some: S) => Option<S>;
23
- export type Repeat0<T> = () => Option<readonly [T, Repeat0<T>]>;
24
- export declare const repeat0: <T extends Rule>(some: T) => Repeat0<T>;
25
- export type Repeat1<T> = readonly [T, Repeat0<T>];
26
- export declare const repeat1: <T extends Rule>(some: T) => Repeat1<T>;
27
- export type Join1<T, S> = readonly [T, Repeat0<readonly [S, T]>];
28
- export declare const join1: <T extends Rule, S extends Rule>(some: T, separator: S) => Join1<T, S>;
29
- export type Join0<T, S> = Option<readonly [T, Repeat0<readonly [S, T]>]>;
30
- export declare const join0: <T extends Rule, S extends Rule>(some: T, separator: S) => Rule;
@@ -1,37 +0,0 @@
1
- import { stringToCodePointList } from "../../text/utf16/module.f.js";
2
- import { isArray2 } from "../../types/array/module.f.js";
3
- import { map, toArray } from "../../types/list/module.f.js";
4
- //
5
- const { fromEntries } = Object;
6
- const { fromCodePoint } = String;
7
- const offset = 24n;
8
- const mask = (1n << offset) - 1n;
9
- export const rangeEncode = (a, b) => (BigInt(a) << offset) | BigInt(b);
10
- export const oneEncode = (a) => rangeEncode(a, a);
11
- export const rangeDecode = (r) => [Number(r >> offset), Number(r & mask)];
12
- const mapOneEncode = map(oneEncode);
13
- export const str = (s) => {
14
- const x = toArray(mapOneEncode(stringToCodePointList(s)));
15
- return x.length === 1 ? x[0] : x;
16
- };
17
- const mapEntry = map((v) => [fromCodePoint(v), oneEncode(v)]);
18
- export const set = (s) => fromEntries(toArray(mapEntry(stringToCodePointList(s))));
19
- export const range = (ab) => {
20
- const a = toArray(stringToCodePointList(ab));
21
- if (!isArray2(a)) {
22
- throw `Invalid range ${ab}.`;
23
- }
24
- return rangeEncode(...a);
25
- };
26
- export const none = [];
27
- export const option = (some) => ({
28
- none,
29
- some,
30
- });
31
- export const repeat0 = (some) => {
32
- const f = () => option([some, f]);
33
- return f;
34
- };
35
- export const repeat1 = (some) => [some, repeat0(some)];
36
- export const join1 = (some, separator) => [some, repeat0([separator, some])];
37
- export const join0 = (some, separator) => option(join1(some, separator));
File without changes