functionalscript 0.0.444 → 0.0.446

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.444",
3
+ "version": "0.0.446",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -0,0 +1,3 @@
1
+ # SGR (Select Graphic Rendition) parameters
2
+
3
+ https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
@@ -0,0 +1,60 @@
1
+ const sortedList = require("../sorted_list/module.f.cjs")
2
+ const { genericMerge } = sortedList
3
+ const list = require("../list/module.f.cjs")
4
+ const option = require("../option/module.f.cjs")
5
+ const { cmp } = require('../number/module.f.cjs')
6
+ const operator = require("../function/operator/module.f.cjs")
7
+
8
+ /**
9
+ * @template T
10
+ * @typedef {[T, number]} Entry
11
+ */
12
+
13
+ /**
14
+ * @template T
15
+ * @typedef {sortedList.SortedList<Entry<T>>} RangeMap
16
+ */
17
+
18
+ /**
19
+ * @template T
20
+ * @typedef {{
21
+ * readonly union: operator.Reduce<T>
22
+ * readonly equal: operator.Equal<T>
23
+ * }} Operators
24
+ */
25
+
26
+ /**
27
+ * @template T
28
+ * @typedef {option.Option<Entry<T>>} RangeState
29
+ */
30
+
31
+ /**
32
+ * @template T
33
+ * @typedef {(a: RangeMap<T>) => (b: RangeMap<T>) => RangeMap<T>} RangeMerge
34
+ */
35
+
36
+ /** @type {<T>(union: operator.Reduce<T>) => (equal: operator.Equal<T>) => sortedList.ReduceOp<Entry<T>, RangeState<T>>} */
37
+ const reduceOp = union => equal => state => ([aItem, aMax]) => ([bItem, bMax]) => {
38
+ const sign = cmp(aMax)(bMax)
39
+ const min = sign === 1 ? bMax : aMax
40
+ const u = union(aItem)(bItem)
41
+ const newState = state !== undefined && equal(state[0])(u) ? undefined : state
42
+ return [newState, sign, [u, min]]
43
+ }
44
+
45
+ /** @type {<T>(equal: operator.Equal<T>) => sortedList.TailReduce<Entry<T>, RangeState<T>>} */
46
+ const tailReduce = equal => state => tail => {
47
+ if (state === undefined) { return tail }
48
+ const tailResult = list.next(tail)
49
+ if (tailResult === undefined) { return [state] }
50
+ if (equal(state[0])(tailResult.first[0])) { return tailResult }
51
+ return { first: state, tail: tailResult }
52
+ }
53
+
54
+ /** @type {<T>(op: Operators<T>) => RangeMerge<T>} */
55
+ const merge = ({union, equal}) => genericMerge({reduceOp: reduceOp(union)(equal), tailReduce: tailReduce(equal)})(undefined)
56
+
57
+ module.exports = {
58
+ /** @readonly */
59
+ merge
60
+ }
@@ -0,0 +1,81 @@
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 sortedSet = require('../sorted_set/module.f.cjs')
6
+ const { list } = require('../module.f.cjs')
7
+ const operator = require("../function/operator/module.f.cjs")
8
+
9
+ /** @type {(a: readonly json.Unknown[]) => string} */
10
+ const stringify = a => json.stringify(sort)(a)
11
+
12
+ /** @type {_.Operators<sortedSet.SortedSet<string>>} */
13
+ const op = { union: sortedSet.union(unsafeCmp), equal: list.equal(operator.strictEqual) }
14
+
15
+ module.exports = {
16
+ merge: [
17
+ () => {
18
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
19
+ const a = [[['a'], 1], [['b'], 2]]
20
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
21
+ const b = undefined
22
+ const merged = _.merge(op)(a)(b)
23
+ const result = stringify(list.toArray(merged))
24
+ if (result !== '[[["a"],1],[["b"],2]]') { throw result }
25
+ },
26
+ () => {
27
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
28
+ const a = undefined
29
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
30
+ const b = [[['a'], 1], [['b'], 2]]
31
+ const merged = _.merge(op)(a)(b)
32
+ const result = stringify(list.toArray(merged))
33
+ if (result !== '[[["a"],1],[["b"],2]]') { throw result }
34
+ },
35
+ () => {
36
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
37
+ const a = [[['a'], 1], [['b'], 2]]
38
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
39
+ const b = [[['a'], 1], [['b'], 2]]
40
+ const merged = _.merge(op)(a)(b)
41
+ const result = stringify(list.toArray(merged))
42
+ if (result !== '[[["a"],1],[["b"],2]]') { throw result }
43
+ },
44
+ () => {
45
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
46
+ const a = [[['a'], 1], [['c'], 3]]
47
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
48
+ const b = [[['b'], 2], [['d'], 4]]
49
+ const merged = _.merge(op)(a)(b)
50
+ const result = stringify(list.toArray(merged))
51
+ if (result !== '[[["a","b"],1],[["b","c"],2],[["c","d"],3],[["d"],4]]') { throw result }
52
+ },
53
+ () => {
54
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
55
+ const a = [[['a'], 1], [['d'], 4]]
56
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
57
+ const b = [[['b'], 2], [['c'], 3]]
58
+ const merged = _.merge(op)(a)(b)
59
+ const result = stringify(list.toArray(merged))
60
+ if (result !== '[[["a","b"],1],[["b","d"],2],[["c","d"],3],[["d"],4]]') { throw result }
61
+ },
62
+ () => {
63
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
64
+ const a = [[['a'], 1], [['b'], 2]]
65
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
66
+ const b = [[['b'], 1], [['a'], 2]]
67
+ const merged = _.merge(op)(a)(b)
68
+ const result = stringify(list.toArray(merged))
69
+ if (result !== '[[["a","b"],2]]') { throw result }
70
+ },
71
+ () => {
72
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
73
+ const a = [[['a'], 1], [['b'], 2], [['a'], 3]]
74
+ /** @type {_.RangeMap<sortedSet.SortedSet<string>>} */
75
+ const b = [[['a'], 5]]
76
+ const merged = _.merge(op)(a)(b)
77
+ const result = stringify(list.toArray(merged))
78
+ if (result !== '[[["a"],1],[["a","b"],2],[["a"],5]]') { throw result }
79
+ }
80
+ ]
81
+ }
@@ -14,11 +14,6 @@ const { identity } = require('../function/module.f.cjs')
14
14
  * @typedef {(a: T) => (b: T) => compare.Sign} Cmp
15
15
  */
16
16
 
17
- /**
18
- * @template T
19
- * @typedef {SortedList<[T, number]>} RangeMap
20
- */
21
-
22
17
  /**
23
18
  * @template T
24
19
  * @template S
@@ -51,7 +46,7 @@ const genericMerge = reduce => {
51
46
  const aResult = next(a)
52
47
  if (aResult === undefined) { return tailReduce(state)(b) }
53
48
  const bResult = next(b)
54
- if (bResult === undefined) { return tailReduce(state)(a) }
49
+ if (bResult === undefined) { return tailReduce(state)(aResult) }
55
50
  const [first, sign, stateNext] = reduceOp(state)(aResult.first)(bResult.first)
56
51
  const aNext = sign === 1 ? a : aResult.tail
57
52
  const bNext = sign === -1 ? b : bResult.tail