functionalscript 0.0.438 → 0.0.439

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.438",
3
+ "version": "0.0.439",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -294,16 +294,18 @@ const zip = a => b => () => {
294
294
  return { first: [aResult.first, bResult.first], tail: zip(aResult.tail)(bResult.tail) }
295
295
  }
296
296
 
297
- /** @type {<T>(e: operator.Equal<T>) => (a: List<T>) => (b: List<T>) => List<boolean>} */
298
- const equalZip = e => a => b => () => {
299
- const [aResult, bResult] = [next(a), next(b)]
300
- return aResult === undefined || bResult === undefined
301
- ? { first: aResult === bResult, tail: undefined }
302
- : { first: e(aResult.first)(bResult.first), tail: equalZip(e)(aResult.tail)(bResult.tail) }
303
- }
304
-
305
297
  /** @type {<T>(e: operator.Equal<T>) => (a: List<T>) => (b: List<T>) => boolean} */
306
- const equal = e => a => b => every(equalZip(e)(a)(b))
298
+ const equal = e => {
299
+ /** @typedef {typeof e extends operator.Equal<infer T> ? T : never} T */
300
+ /** @type {(a: List<T>) => (b: List<T>) => List<boolean>} */
301
+ const f = a => b => () => {
302
+ const [aResult, bResult] = [next(a), next(b)]
303
+ return aResult === undefined || bResult === undefined
304
+ ? { first: aResult === bResult, tail: undefined }
305
+ : { first: e(aResult.first)(bResult.first), tail: f(aResult.tail)(bResult.tail) }
306
+ }
307
+ return a => b => every(f(a)(b))
308
+ }
307
309
 
308
310
  module.exports = {
309
311
  /** @readonly */
@@ -1,7 +1,8 @@
1
- const compare = require("../function/compare/module.f.cjs")
2
- const list = require("../list/module.f.cjs")
3
- const option = require("../option/module.f.cjs")
1
+ const compare = require('../function/compare/module.f.cjs')
2
+ const list = require('../list/module.f.cjs')
3
+ const option = require('../option/module.f.cjs')
4
4
  const { next } = list
5
+ const { identity } = require('../function/module.f.cjs')
5
6
 
6
7
  /**
7
8
  * @template T
@@ -13,55 +14,74 @@ const { next } = list
13
14
  * @typedef {(a: T) => (b: T) => compare.Sign} Cmp
14
15
  */
15
16
 
16
- /** @typedef {number} Byte */
17
-
18
17
  /**
19
18
  * @template T
20
- * @typedef {SortedList<[Byte, readonly string[]]>} RangeMap
19
+ * @typedef {SortedList<[T, number]>} RangeMap
21
20
  */
22
21
 
23
22
  /**
24
- * @template S
25
23
  * @template T
24
+ * @template S
26
25
  * @typedef {(state: S) => (a: T) => (b: T) => readonly[option.Option<T>, compare.Sign, S]} ReduceOp
27
26
  */
28
27
 
29
28
  /**
30
- * @template S
31
29
  * @template T
30
+ * @template S
32
31
  * @typedef {(state: S) => (tail: list.List<T>) => list.List<T>} TailReduce
33
32
  */
34
33
 
35
34
  /**
35
+ * @template T
36
36
  * @template S
37
+ * @typedef {{
38
+ * readonly reduceOp: ReduceOp<T,S>
39
+ * readonly tailReduce: TailReduce<T,S>
40
+ * }} MergeReduce
41
+ */
42
+
43
+ /** @type {<T,S>(reduce: MergeReduce<T,S>) => (state: S) => (a: list.List<T>) => (b: list.List<T>) => list.List<T>} */
44
+ const genericMerge = reduce => {
45
+ const { reduceOp, tailReduce } = reduce
46
+ /** @typedef {typeof reduce extends MergeReduce<infer T, infer S> ? [T, S] : never} TS */
47
+ /** @typedef {TS[0]} T */
48
+ /** @typedef {TS[1]} S */
49
+ /** @type {(state: S) => (a: list.List<T>) => (b: list.List<T>) => list.List<T>} */
50
+ const f = state => a => b => () => {
51
+ const aResult = next(a)
52
+ if (aResult === undefined) { return tailReduce(state)(b) }
53
+ const bResult = next(b)
54
+ if (bResult === undefined) { return tailReduce(state)(a) }
55
+ const [first, sign, stateNext] = reduceOp(state)(aResult.first)(bResult.first)
56
+ const aNext = sign === 1 ? a : aResult.tail
57
+ const bNext = sign === -1 ? b : bResult.tail
58
+ const tail = f(stateNext)(aNext)(bNext)
59
+ return first === undefined ? tail : { first, tail }
60
+ }
61
+ return f
62
+ }
63
+
64
+ /**
37
65
  * @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
66
+ * @typedef {ReduceOp<T, undefined>} CmpReduceOp
39
67
  */
40
68
 
41
69
  /** @type {<T>(cmp: Cmp<T>) => (a: SortedList<T>) => (b: SortedList<T>) => SortedList<T>} */
42
- const merge = cmp => genericMerge(undefined)(cmpReduce(cmp))(mergeTail)
70
+ const merge = cmp => {
71
+ /** @typedef {typeof cmp extends Cmp<infer T> ? T : never} T*/
72
+ /** @type {TailReduce<T, undefined>} */
73
+ const tailReduce = mergeTail
74
+ return genericMerge({ reduceOp: cmpReduce(cmp), tailReduce })(undefined)
75
+ }
43
76
 
44
- /** @type {<S,T>(cmp: Cmp<T>) => ReduceOp<S, T>} */
45
- const cmpReduce = cmp => state => a => b => {
77
+ /** @type {<T>(cmp: Cmp<T>) => CmpReduceOp<T>} */
78
+ const cmpReduce = cmp => () => a => b => {
46
79
  const sign = cmp(a)(b)
47
- return [sign === 1 ? b : a, sign, state]
80
+ return [sign === 1 ? b : a, sign, undefined]
48
81
  }
49
82
 
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 => () => {
55
- const aResult = next(a)
56
- if (aResult === undefined) { return tailReduce(init)(b) }
57
- const bResult = next(b)
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 }
64
- }
83
+ /** @type {() => <T>(tail: list.List<T>) => list.List<T>} */
84
+ const mergeTail = () => identity
65
85
 
66
86
  module.exports = {
67
87
  /** @readonly */
@@ -24,16 +24,15 @@ const union = cmp => a => b => toArray(merge(cmp)(a)(b))
24
24
  const intersect = cmp => a => b => toArray(intersectMerge(cmp)(a)(b))
25
25
 
26
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)
27
+ const intersectMerge = cmp => genericMerge({ reduceOp: intersectReduce(cmp), tailReduce: intersectTail })(undefined)
28
28
 
29
- /** @type {<S,T>(cmp: Cmp<T>) => sortedList.ReduceOp<S, T>} */
29
+ /** @type {<T,S>(cmp: Cmp<T>) => sortedList.ReduceOp<T,S>} */
30
30
  const intersectReduce = cmp => state => a => b => {
31
31
  const sign = cmp(a)(b)
32
32
  return [sign === 0 ? a : undefined, sign, state]
33
33
  }
34
34
 
35
- /** @type {<S,T>(state: S) => (tail: list.List<T>) => list.List<T>} */
36
- const intersectTail = s => input => undefined
35
+ const intersectTail = () => () => undefined
37
36
 
38
37
  module.exports = {
39
38
  /** @readonly */