functionalscript 0.0.436 → 0.0.437
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,7 @@
|
|
|
1
1
|
const compare = require("../function/compare/module.f.cjs")
|
|
2
2
|
const list = require("../list/module.f.cjs")
|
|
3
|
-
const
|
|
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 =>
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
+
}
|