functionalscript 0.0.436 → 0.0.438

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.436",
3
+ "version": "0.0.438",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "homepage": "https://github.com/functionalscript/functionalscript#readme",
31
31
  "devDependencies": {
32
- "@types/node": "^18.8.3",
32
+ "@types/node": "^18.8.4",
33
33
  "typescript": "^4.8.4"
34
34
  }
35
35
  }
@@ -10,6 +10,9 @@ const has = n => s => ((s >> BigInt(n)) & 1n) === 1n
10
10
 
11
11
  const empty = 0n
12
12
 
13
+ // 0 1 2 3 4 5 6 7 8 9 A B C D E F
14
+ const universe = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFFn
15
+
13
16
  /** @type {(n: Byte) => ByteSet} */
14
17
  const one = n => 1n << BigInt(n)
15
18
 
@@ -28,7 +31,7 @@ const intersect = a => b => a & b
28
31
  const difference = a => b => intersect(a)(complement(b))
29
32
 
30
33
  /** @type {(n: ByteSet) => ByteSet} */
31
- const complement = n => ~n
34
+ const complement = n => universe ^ n
32
35
 
33
36
  // additional operations
34
37
 
@@ -43,6 +46,8 @@ module.exports = {
43
46
  /** @readonly */
44
47
  empty,
45
48
  /** @readonly */
49
+ universe,
50
+ /** @readonly */
46
51
  has,
47
52
  /** @readonly */
48
53
  set,
@@ -54,4 +59,6 @@ module.exports = {
54
59
  setRange,
55
60
  /** @readonly */
56
61
  range,
62
+ /** @readonly */
63
+ complement,
57
64
  }
@@ -1,4 +1,5 @@
1
1
  const _ = require('./module.f.cjs')
2
+ const { every, countdown, map } = require('../list/module.f.cjs')
2
3
 
3
4
  module.exports = {
4
5
  has: [
@@ -37,5 +38,19 @@ module.exports = {
37
38
  const result = _.unset(255)(a)
38
39
  if (result !== 0n) { throw result }
39
40
  }
40
- ]
41
+ ],
42
+ universe: () => {
43
+ const x = every(map(v => _.has(v)(_.universe))(countdown(256)))
44
+ if (!x) { throw x }
45
+ },
46
+ compliment: {
47
+ empty: () => {
48
+ const r = _.complement(_.empty)
49
+ if (r !== _.universe) { throw r }
50
+ },
51
+ universe: () => {
52
+ const r = _.complement(_.universe)
53
+ if (r !== _.empty) { throw r }
54
+ },
55
+ }
41
56
  }
@@ -1,32 +1,44 @@
1
- /** @typedef {number} nibbleSet */
2
- /** @typedef {number} nibble */
1
+ /** @typedef {number} NibbleSet */
2
+ /** @typedef {number} Nibble */
3
3
 
4
4
  const empty = 0
5
5
 
6
- /** @type {(n: nibble) => (s: nibbleSet) => boolean} */
6
+ const universe = 0xFFFF
7
+
8
+ /** @type {(n: Nibble) => NibbleSet} */
9
+ const one = n => 1 << n
10
+
11
+ /** @type {(n: Nibble) => (s: NibbleSet) => boolean} */
7
12
  const has = n => s => ((s >> n) & 1) === 1
8
13
 
9
- /** @type {(n: nibble) => (s: nibbleSet) => nibbleSet} */
10
- const set = n => s => s | (1 << n)
14
+ /** @type {(n: Nibble) => (s: NibbleSet) => NibbleSet} */
15
+ const set = n => s => s | one(n)
16
+
17
+ /** @type {(n: NibbleSet) => NibbleSet} */
18
+ const complement = s => universe ^ s
11
19
 
12
- /** @type {(n: nibble) => (s: nibbleSet) => nibbleSet} */
13
- const unset = n => s => s & ~(1 << n)
20
+ /** @type {(n: Nibble) => (s: NibbleSet) => NibbleSet} */
21
+ const unset = n => s => s & complement(one(n))
14
22
 
15
- /** @type {(r: readonly[number, number]) => (s: nibbleSet) => nibbleSet} */
16
- const setRange = r => s => s | (((1 << (r[1] - r[0] + 1)) - 1 << r[0]))
23
+ /** @type {(r: readonly[number, number]) => NibbleSet} */
24
+ const range = ([a, b]) => one(b - a + 1) - 1 << a
17
25
 
18
- // how to define FA???
19
- // const stateA = [init, set] ????
26
+ /** @type {(r: readonly[number, number]) => (s: NibbleSet) => NibbleSet} */
27
+ const setRange = r => s => s | range(r)
20
28
 
21
29
  module.exports = {
22
30
  /** @readonly */
23
31
  empty,
24
32
  /** @readonly */
33
+ universe,
34
+ /** @readonly */
25
35
  has,
26
36
  /** @readonly */
37
+ complement,
38
+ /** @readonly */
27
39
  set,
28
40
  /** @readonly */
29
41
  unset,
30
42
  /** @readonly */
31
- setRange
43
+ setRange,
32
44
  }
@@ -1,3 +1,4 @@
1
+ const { every, map, countdown } = require('../list/module.f.cjs')
1
2
  const _ = require('./module.f.cjs')
2
3
 
3
4
  module.exports = {
@@ -16,7 +17,7 @@ module.exports = {
16
17
  },
17
18
  () => {
18
19
  const s = _.set(15)(_.empty)
19
- if (s !== 32768) { throw s }
20
+ if (s !== 0x8000) { throw s }
20
21
  if (_.has(0)(s)) { throw s }
21
22
  if (_.has(1)(s)) { throw s }
22
23
  if (!_.has(15)(s)) { throw s }
@@ -37,5 +38,19 @@ module.exports = {
37
38
  setRange: () => {
38
39
  const result = _.setRange([2, 5])(_.empty)
39
40
  if (result !== 60) { throw result }
41
+ },
42
+ universe: () => {
43
+ const x = every(map(v => _.has(v)(_.universe))(countdown(16)))
44
+ if (!x) { throw x }
45
+ },
46
+ compliment: {
47
+ empty: () => {
48
+ const r = _.complement(_.empty)
49
+ if (r !== _.universe) { throw r }
50
+ },
51
+ universe: () => {
52
+ const r = _.complement(_.universe)
53
+ if (r !== _.empty) { throw r }
54
+ },
40
55
  }
41
56
  }
@@ -1,6 +1,7 @@
1
1
  const compare = require("../function/compare/module.f.cjs")
2
2
  const list = require("../list/module.f.cjs")
3
- const { next, toArray } = list
3
+ const option = require("../option/module.f.cjs")
4
+ const { next } = list
4
5
 
5
6
  /**
6
7
  * @template T
@@ -19,20 +20,52 @@ const { next, toArray } = list
19
20
  * @typedef {SortedList<[Byte, readonly string[]]>} RangeMap
20
21
  */
21
22
 
23
+ /**
24
+ * @template S
25
+ * @template T
26
+ * @typedef {(state: S) => (a: T) => (b: T) => readonly[option.Option<T>, compare.Sign, S]} ReduceOp
27
+ */
28
+
29
+ /**
30
+ * @template S
31
+ * @template T
32
+ * @typedef {(state: S) => (tail: list.List<T>) => list.List<T>} TailReduce
33
+ */
34
+
35
+ /**
36
+ * @template S
37
+ * @template T
38
+ * @typedef {(init: S) => (reduce: ReduceOp<S,T>) => (tailReduce: TailReduce<S, T>) => (a: list.List<T>) => (b: list.List<T>) => list.List<T>} GenericMerge
39
+ */
40
+
22
41
  /** @type {<T>(cmp: Cmp<T>) => (a: SortedList<T>) => (b: SortedList<T>) => SortedList<T>} */
23
- const merge = cmp => a => b => () => {
42
+ const merge = cmp => genericMerge(undefined)(cmpReduce(cmp))(mergeTail)
43
+
44
+ /** @type {<S,T>(cmp: Cmp<T>) => ReduceOp<S, T>} */
45
+ const cmpReduce = cmp => state => a => b => {
46
+ const sign = cmp(a)(b)
47
+ return [sign === 1 ? b : a, sign, state]
48
+ }
49
+
50
+ /** @type {<S,T>(state: S) => (tail: list.List<T>) => list.List<T>} */
51
+ const mergeTail = s => input => input
52
+
53
+ /** @type {<S,T>(init: S) => (reduce: ReduceOp<S,T>) => (tailReduce: TailReduce<S, T>) => (a: list.List<T>) => (b: list.List<T>) => list.List<T>} */
54
+ const genericMerge = init => reduce => tailReduce => a => b => () => {
24
55
  const aResult = next(a)
25
- if (aResult === undefined) { return b }
56
+ if (aResult === undefined) { return tailReduce(init)(b) }
26
57
  const bResult = next(b)
27
- if (bResult === undefined) { return a }
28
- switch (cmp(aResult.first)(bResult.first)) {
29
- case -1: return { first: aResult.first, tail: merge(cmp)(aResult.tail)(b) }
30
- case 0: return { first: aResult.first, tail: merge(cmp)(aResult.tail)(bResult.tail) }
31
- case 1: return { first: bResult.first, tail: merge(cmp)(a)(bResult.tail) }
32
- }
58
+ if (bResult === undefined) { return tailReduce(init)(a) }
59
+ const [result, sign, state] = reduce(init)(aResult.first)(bResult.first)
60
+ const aNext = sign === 1 ? a : aResult.tail
61
+ const bNext = sign === -1 ? b : bResult.tail
62
+ const mergeNext = genericMerge(state)(reduce)(tailReduce)(aNext)(bNext)
63
+ return result === undefined ? mergeNext : { first: result, tail: mergeNext }
33
64
  }
34
65
 
35
66
  module.exports = {
36
67
  /** @readonly */
37
- merge
68
+ merge,
69
+ /** @readonly */
70
+ genericMerge
38
71
  }
@@ -0,0 +1,43 @@
1
+ const compare = require("../function/compare/module.f.cjs")
2
+ const { toArray } = require("../list/module.f.cjs")
3
+ const sortedList = require("../sorted_list/module.f.cjs")
4
+ const { merge, genericMerge } = sortedList
5
+ const list = require("../list/module.f.cjs")
6
+ const option = require("../option/module.f.cjs")
7
+
8
+ /**
9
+ * @template T
10
+ * @typedef {readonly T[]} SortedSet
11
+ */
12
+
13
+ /**
14
+ * @template T
15
+ * @typedef {(a: T) => (b: T) => compare.Sign} Cmp
16
+ */
17
+
18
+ /** @typedef {number} Byte */
19
+
20
+ /** @type {<T>(cmp: Cmp<T>) => (a: SortedSet<T>) => (b: SortedSet<T>) => SortedSet<T>} */
21
+ const union = cmp => a => b => toArray(merge(cmp)(a)(b))
22
+
23
+ /** @type {<T>(cmp: Cmp<T>) => (a: SortedSet<T>) => (b: SortedSet<T>) => SortedSet<T>} */
24
+ const intersect = cmp => a => b => toArray(intersectMerge(cmp)(a)(b))
25
+
26
+ /** @type {<T>(cmp: Cmp<T>) => (a: sortedList.SortedList<T>) => (b: sortedList.SortedList<T>) => sortedList.SortedList<T>} */
27
+ const intersectMerge = cmp => genericMerge(undefined)(intersectReduce(cmp))(intersectTail)
28
+
29
+ /** @type {<S,T>(cmp: Cmp<T>) => sortedList.ReduceOp<S, T>} */
30
+ const intersectReduce = cmp => state => a => b => {
31
+ const sign = cmp(a)(b)
32
+ return [sign === 0 ? a : undefined, sign, state]
33
+ }
34
+
35
+ /** @type {<S,T>(state: S) => (tail: list.List<T>) => list.List<T>} */
36
+ const intersectTail = s => input => undefined
37
+
38
+ module.exports = {
39
+ /** @readonly */
40
+ union,
41
+ /** @readonly */
42
+ intersect
43
+ }
@@ -0,0 +1,44 @@
1
+ const _ = require('./module.f.cjs')
2
+ const { unsafeCmp } = require('../function/compare/module.f.cjs')
3
+ const json = require('../../json/module.f.cjs')
4
+ const { sort } = require('../../types/object/module.f.cjs')
5
+ const { toArray, countdown, length } = require('../list/module.f.cjs')
6
+ const map = require('../map/module.f.cjs')
7
+ const { flip } = require('../function/module.f.cjs')
8
+
9
+ /** @type {(a: readonly json.Unknown[]) => string} */
10
+ const stringify = a => json.stringify(sort)(a)
11
+
12
+ /** @type {<T>(a: T) => (b: T) => map.Sign} */
13
+ const reverseCmp = flip(unsafeCmp)
14
+
15
+ module.exports = {
16
+ union: [
17
+ () => {
18
+ const result = stringify(toArray(_.union(unsafeCmp)([2, 3, 4])([1, 3, 5])))
19
+ if (result !== '[1,2,3,4,5]') { throw result }
20
+ },
21
+ () => {
22
+ const result = stringify(toArray(_.union(unsafeCmp)([1, 2, 3])([])))
23
+ if (result !== '[1,2,3]') { throw result }
24
+ },
25
+ () => {
26
+ const n = 10_000
27
+ const sortedSet = toArray(countdown(n))
28
+ const result = _.union(reverseCmp)(sortedSet)(sortedSet)
29
+ const len = length(result)
30
+ if (len != n) { throw result }
31
+ }
32
+ ],
33
+ intersect: [
34
+ () => {
35
+ const result = stringify(toArray(_.intersect(unsafeCmp)([2, 3, 4])([1, 3, 5])))
36
+ console.log(result)
37
+ if (result !== '[3]') { throw result }
38
+ },
39
+ () => {
40
+ const result = stringify(toArray(_.intersect(unsafeCmp)([1, 2, 3])([])))
41
+ if (result !== '[]') { throw result }
42
+ }
43
+ ]
44
+ }