functionalscript 0.0.568 → 0.0.570

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.
@@ -6,7 +6,7 @@ const { cpp, cs, rust } = build
6
6
  const { join } = require('../../types/string/module.f.cjs')
7
7
  const { log, error } = console
8
8
  const { bold, reset } = require('../../text/sgr/module.f.cjs')
9
- const { list } = require('../../types/module.f.cjs')
9
+ const list = require('../../types/list/module.f.cjs')
10
10
 
11
11
  const nodeJs = {
12
12
  dirname: __dirname,
@@ -0,0 +1,30 @@
1
+ # Issues
2
+
3
+ ## Allow Debugging During Test Run
4
+
5
+ Currently, we read files as strings and then parse them as functions. See [dev/test.mjs](dev/test.mjs). In this case, debugger doesn't know about source code and can't debug the functions. The main reason for loading modules as functions was that Deno v1 didn't support `.cjs` files. However, Deno v2 support them.
6
+
7
+ We can fix the issue by changing our test runner. The test runner will scan all directories and find all `test.f.cjs` files and then load them using `require`.
8
+
9
+ **Note:** we will drop support for Deno v1.
10
+
11
+ ## Creating `./_module.f.cjs`
12
+
13
+ We can write a script which will generate `./_module.f.cjs` before packaging. Note: the script should be added into [prepack](https://docs.npmjs.com/cli/v8/using-npm/scripts#pre--post-scripts). The module should never be used by other internal modules. It's only for internal consumptions. The structure will be like this
14
+
15
+ ```js
16
+ module.exports = {
17
+ types: {
18
+ list: require('./types/list/module.f.cjs'),
19
+ // ...
20
+ },
21
+ // ...
22
+ }
23
+ ```
24
+
25
+ Then, users can use it like this:
26
+
27
+ ```js
28
+ const { types: { list } } = require('functionalscript)
29
+ //...
30
+ ```
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.568",
3
+ "version": "0.0.570",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
- "main": "module.f.cjs",
6
5
  "scripts": {
7
6
  "tsc": "tsc",
8
7
  "test": "tsc && npm run test-only",
package/text/test.f.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  const _ = require('./module.f.cjs')
2
- const { string: { join } } = require('../types/module.f.cjs')
2
+ const { join } = require('../types/string/module.f.cjs')
3
3
 
4
4
  module.exports = () => {
5
5
  /** @type {_.Block} */
@@ -1,7 +1,7 @@
1
1
  const encoding = require('./module.f.cjs')
2
2
  const json = require('../../json/module.f.cjs')
3
3
  const { sort } = require('../../types/object/module.f.cjs')
4
- const { list } = require('../../types/module.f.cjs')
4
+ const list = require('../../types/list/module.f.cjs')
5
5
 
6
6
  /** @type {(a: readonly json.Unknown[]) => string} */
7
7
  const stringify = a => json.stringify(sort)(a)
@@ -1,7 +1,7 @@
1
1
  const encoding = require('./module.f.cjs')
2
2
  const json = require('../../json/module.f.cjs')
3
3
  const { sort } = require('../../types/object/module.f.cjs')
4
- const { list } = require('../../types/module.f.cjs')
4
+ const list = require('../../types/list/module.f.cjs')
5
5
 
6
6
  const stringify = json.stringify(sort)
7
7
 
@@ -44,17 +44,33 @@ const scalar_mul = ({ 0: _0, add }) => a => n => {
44
44
  }
45
45
  }
46
46
 
47
- /** @type {(a: bigint) => bigint} */
48
- const log2 = a => {
49
- // Possible optimization: use a binary search in 32 bit value
50
- let i = -1n
51
- while (a > 0n) {
52
- ++i
53
- a >>= 1n
47
+ const bit_len = (/** @type {bigint} */v) => {
48
+ if (v < 0n) { v = -v }
49
+ if (v === 0n) { return 0n }
50
+ let result = 1n
51
+ let i = 1n
52
+ while (true) {
53
+ const n = v >> i
54
+ if (n === 0n) {
55
+ break
56
+ }
57
+ v = n
58
+ result += i
59
+ i <<= 1n
54
60
  }
55
- return i
61
+ while (i !== 0n) {
62
+ i >>= 1n
63
+ const n = v >> i
64
+ if (n !== 0n) {
65
+ result += i
66
+ v = n
67
+ }
68
+ }
69
+ return result
56
70
  }
57
71
 
72
+ const log2 = (/** @type {bigint} */v) => v <= 0n ? -1n : bit_len(v) - 1n
73
+
58
74
  module.exports = {
59
75
  /** @readonly */
60
76
  addition,
@@ -70,4 +86,6 @@ module.exports = {
70
86
  scalar_mul,
71
87
  /** @readonly */
72
88
  log2,
89
+ /** @readonly */
90
+ bit_len,
73
91
  }
@@ -1,4 +1,4 @@
1
- const { sum, abs, serialize, log2 } = require('./module.f.cjs')
1
+ const { sum, abs, serialize, log2, bit_len } = require('./module.f.cjs')
2
2
 
3
3
  module.exports = {
4
4
  sum: () => {
@@ -69,6 +69,48 @@ module.exports = {
69
69
  () => {
70
70
  const result = log2(16n)
71
71
  if (result !== 4n) { throw result }
72
+ },
73
+ () => {
74
+ // max for Bun (131_072 Bytes)
75
+ const v = 1_048_575n
76
+ const result = log2(1n << v)
77
+ if (result !== v) { throw result }
72
78
  }
73
- ]
79
+ ],
80
+ bit_len: {
81
+ 0: () => {
82
+ const s = bit_len(0n)
83
+ if (s !== 0n) { throw s }
84
+ },
85
+ m: () => {
86
+ let i = 0n
87
+ while (i < 10_000n) {
88
+ const s = bit_len(1n << i)
89
+ if (s !== i + 1n) { throw [s, i] }
90
+ i += 1n
91
+ }
92
+ },
93
+ big: () => {
94
+ const s = bit_len(1n << 1_000_000n)
95
+ if (s !== 1_000_001n) { throw s }
96
+ },
97
+ neg: [
98
+ () => {
99
+ const s = bit_len(-1n)
100
+ if (s !== 1n) { throw s }
101
+ },
102
+ () => {
103
+ const s = bit_len(-2n)
104
+ if (s !== 2n) { throw s }
105
+ },
106
+ () => {
107
+ const s = bit_len(-3n)
108
+ if (s !== 2n) { throw s }
109
+ },
110
+ () => {
111
+ const s = bit_len(-4n)
112
+ if (s !== 3n) { throw s }
113
+ },
114
+ ]
115
+ }
74
116
  }
@@ -52,8 +52,7 @@ const counter = reverse(countdown(256))
52
52
  const toRangeMapOp = n => s => i => {
53
53
  const current = has(i + 1)(n)
54
54
  const prev = has(i)(n)
55
- if (current === prev) { return null }
56
- return [[prev ? [s] : [], i]]
55
+ return current === prev ? null : [[prev ? [s] : [], i]]
57
56
  }
58
57
 
59
58
  /** @type {(n: ByteSet) => (s: string) => rangeMap.RangeMap<sortedSet.SortedSet<string>>} */
@@ -27,7 +27,7 @@ const _range = require('../range/module.f.cjs')
27
27
  * @typedef {{
28
28
  * readonly union: operator.Reduce<T>
29
29
  * readonly equal: operator.Equal<T>
30
- * }} Operators
30
+ * }} Operators
31
31
  */
32
32
 
33
33
  /**
@@ -41,41 +41,41 @@ const _range = require('../range/module.f.cjs')
41
41
  */
42
42
 
43
43
  /** @type {<T>(union: operator.Reduce<T>) => (equal: operator.Equal<T>) => sortedList.ReduceOp<Entry<T>, RangeState<T>>} */
44
- const reduceOp = union => equal => state => ([aItem, aMax]) => ([bItem, bMax]) => {
45
- const sign = cmp(aMax)(bMax)
46
- const min = sign === 1 ? bMax : aMax
47
- const u = union(aItem)(bItem)
48
- const newState = state !== null && equal(state[0])(u) ? null : state
49
- return [newState, sign, [u, min]]
44
+ const reduceOp = union => equal => state => ([aItem, aMax]) => ([bItem, bMax]) => {
45
+ const sign = cmp(aMax)(bMax)
46
+ const min = sign === 1 ? bMax : aMax
47
+ const u = union(aItem)(bItem)
48
+ const newState = state !== null && equal(state[0])(u) ? null : state
49
+ return [newState, sign, [u, min]]
50
50
  }
51
51
 
52
52
  /** @type {<T>(equal: operator.Equal<T>) => sortedList.TailReduce<Entry<T>, RangeState<T>>} */
53
53
  const tailReduce = equal => state => tail => {
54
- if (state === null) { return tail }
55
- const tailResult = next(tail)
56
- if (tailResult === null) { return [state] }
57
- if (equal(state[0])(tailResult.first[0])) { return tailResult }
58
- return { first: state, tail: tailResult }
54
+ if (state === null) { return tail }
55
+ const tailResult = next(tail)
56
+ if (tailResult === null) { return [state] }
57
+ if (equal(state[0])(tailResult.first[0])) { return tailResult }
58
+ return { first: state, tail: tailResult }
59
59
  }
60
60
 
61
- /** @type {<T>(op: Operators<T>) => RangeMerge<T>} */
62
- const merge = ({union, equal}) => genericMerge({reduceOp: reduceOp(union)(equal), tailReduce: tailReduce(equal)})(null)
61
+ /** @type {<T>(op: Operators<T>) => RangeMerge<T>} */
62
+ const merge = ({ union, equal }) => genericMerge({ reduceOp: reduceOp(union)(equal), tailReduce: tailReduce(equal) })(null)
63
63
 
64
64
  /** @type {<T>(def: T) => (value: number) => (rm: RangeMapArray<T>) => T} */
65
65
  const get = def => value => rm => {
66
- const len = rm.length
67
- let b = 0
68
- let e = len - 1
69
- while (true) {
70
- if (b >= len) { return def }
71
- if (e - b < 0) { return rm[b][0] }
72
- const mid = b + (e - b >> 1)
73
- if (value <= rm[mid][1]) {
74
- e = mid - 1
75
- } else {
76
- b = mid + 1
66
+ const len = rm.length
67
+ let b = 0
68
+ let e = len - 1
69
+ while (true) {
70
+ if (b >= len) { return def }
71
+ if (e - b < 0) { return rm[b][0] }
72
+ const mid = b + (e - b >> 1)
73
+ if (value <= rm[mid][1]) {
74
+ e = mid - 1
75
+ } else {
76
+ b = mid + 1
77
+ }
77
78
  }
78
- }
79
79
  }
80
80
 
81
81
  /** @type {<T>(def: T) => (r: _range.Range) => (value: T) => RangeMapArray<T>} */
@@ -3,7 +3,7 @@ const { unsafeCmp } = require('../function/compare/module.f.cjs')
3
3
  const json = require('../../json/module.f.cjs')
4
4
  const { sort } = require('../../types/object/module.f.cjs')
5
5
  const sortedSet = require('../sorted_set/module.f.cjs')
6
- const { list } = require('../module.f.cjs')
6
+ const list = require('../list/module.f.cjs')
7
7
  const operator = require("../function/operator/module.f.cjs")
8
8
 
9
9
  /** @type {(a: readonly json.Unknown[]) => string} */
package/module.f.cjs DELETED
@@ -1,22 +0,0 @@
1
- module.exports = {
2
- /** @readonly */
3
- com: require('./com/module.f.cjs'),
4
- /** @readonly */
5
- commonjs: require('./commonjs/module.f.cjs'),
6
- /** @readonly */
7
- dev: require('./dev/module.f.cjs'),
8
- /** @readonly */
9
- fsm: require('./fsm/module.f.cjs'),
10
- /** @readonly */
11
- html: require('./html/module.f.cjs'),
12
- /** @readonly */
13
- json: require('./json/module.f.cjs'),
14
- /** @readonly */
15
- nodejs: require('./nodejs/module.f.cjs'),
16
- /** @readonly */
17
- sha2: require('./sha2/module.f.cjs'),
18
- /** @readonly */
19
- text: require('./text/module.f.cjs'),
20
- /** @readonly */
21
- types: require('./types/module.f.cjs'),
22
- }
@@ -1,28 +0,0 @@
1
- module.exports = {
2
- /** @readonly */
3
- array: require('./array/module.f.cjs'),
4
- /** @readonly */
5
- btree: require('./btree/module.f.cjs'),
6
- /** @readonly */
7
- function: require('./function/module.f.cjs'),
8
- /** @readonly */
9
- list: require('./list/module.f.cjs'),
10
- /** @readonly */
11
- map: require('./map/module.f.cjs'),
12
- /** @readonly */
13
- nibbleSet: require('./nibble_set/module.f.cjs'),
14
- /** @readonly */
15
- object: require('./object/module.f.cjs'),
16
- /** @readonly */
17
- range: require('./range/module.f.cjs'),
18
- /** @readonly */
19
- result: require('./result/module.f.cjs'),
20
- /** @readonly */
21
- stringSet: require('./string_set/module.f.cjs'),
22
- /** @readonly */
23
- bigint: require('./bigint/module.f.cjs'),
24
- /** @readonly */
25
- number: require('./number/module.f.cjs'),
26
- /** @readonly */
27
- string: require('./string/module.f.cjs'),
28
- }