functionalscript 0.0.284 → 0.0.285

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.284",
3
+ "version": "0.0.285",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -5,37 +5,27 @@ const cmp = require('../../function/compare')
5
5
 
6
6
  /**
7
7
  * @template T
8
- * @typedef {readonly[1, _.Leaf1<T>|_.Branch3<T>]} Found1
8
+ * @typedef {readonly[cmp.Index3, _.Leaf1<T>|_.Branch3<T>]} FirstLeaf1
9
9
  */
10
10
 
11
11
  /**
12
12
  * @template T
13
- * @typedef {readonly[1|3, _.Leaf2<T> | _.Branch5<T>]} Found2
13
+ * @typedef {readonly[1, _.Branch3<T>]} FirstBranch3
14
14
  */
15
15
 
16
16
  /**
17
17
  * @template T
18
- * @typedef {readonly[0|2, _.Leaf1<T>]} NotFound1
18
+ * @typedef {readonly[cmp.Index5, _.Leaf2<T>]} FirstLeaf2
19
19
  */
20
20
 
21
21
  /**
22
22
  * @template T
23
- * @typedef {readonly[0|2|4, _.Leaf2<T>]} NotFound2
23
+ * @typedef {readonly[1|3, _.Branch5<T>]} FirstBranch5
24
24
  */
25
25
 
26
26
  /**
27
27
  * @template T
28
- * @typedef {Found1<T> | Found2<T>} Found
29
- */
30
-
31
- /**
32
- * @template T
33
- * @typedef {NotFound1<T> | NotFound2<T>} NotFound
34
- */
35
-
36
- /**
37
- * @template T
38
- * @typedef {Found<T> | NotFound<T>} First
28
+ * @typedef {FirstLeaf1<T> | FirstBranch3<T> | FirstLeaf2<T> | FirstBranch5<T>} First
39
29
  */
40
30
 
41
31
  /**
@@ -53,6 +43,12 @@ const cmp = require('../../function/compare')
53
43
  * @typedef {PathItem3<T> | PathItem5<T>} PathItem
54
44
  */
55
45
 
46
+ /** @type {<T>(item: PathItem<T>) => _.Node<T>} */
47
+ const child = item => {
48
+ /** @typedef {typeof item extends PathItem<infer T> ? T : never} T */
49
+ return /** @type {_.Node<T>} */(item[1][item[0]])
50
+ }
51
+
56
52
  /**
57
53
  * @template T
58
54
  * @typedef {list.List<PathItem<T>>} Path
@@ -64,4 +60,37 @@ const cmp = require('../../function/compare')
64
60
  */
65
61
 
66
62
  /** @type {<T>(c: cmp.Compare<T>) => (node: _.Node<T>) => Result<T>} */
67
- const find = c => node => todo()
63
+ const find = c => {
64
+ const i3 = cmp.index3(c)
65
+ const i5 = cmp.index5(c)
66
+ /** @typedef {typeof c extends cmp.Compare<infer T> ? T : never} T */
67
+ /** @type {(prior: Path<T>) => (node: _.Node<T>) => Result<T>} */
68
+ const f = tail => node => {
69
+ /** @type {(first: PathItem<T>) => Result<T>} */
70
+ const append = first => f({ first, tail })(child(first))
71
+ switch (node.length) {
72
+ case 1: { return [[i3(node[0]), node], tail] }
73
+ case 2: { return [[i5(node), node], tail] }
74
+ case 3: {
75
+ const i = i3(node[1])
76
+ switch (i) {
77
+ case 0: case 2: { return append([i, node]) }
78
+ case 1: { return [[i, node], tail] }
79
+ }
80
+ }
81
+ case 5: {
82
+ const i = i5([node[1], node[3]])
83
+ switch (i) {
84
+ case 0: case 2: case 4: { return append([i, node]) }
85
+ case 1: case 3: { return [[i, node], tail]}
86
+ }
87
+ }
88
+ }
89
+ }
90
+ return f(undefined)
91
+ }
92
+
93
+ module.exports = {
94
+ /** @readonly */
95
+ find,
96
+ }
@@ -0,0 +1,122 @@
1
+ const _ = require('.')
2
+ const list = require('../../list')
3
+ const json = require('../../../json')
4
+ const { sort } = require('../../object')
5
+ const btree = require('..')
6
+ const { stringCmp } = require('../../function/compare')
7
+
8
+ const jsonStr = json.stringify(sort)
9
+
10
+ // /** @type {(sequence: list.List<json.Unknown>) => string} */
11
+ // const arrayStr = sequence => jsonStr(list.toArray(sequence))
12
+
13
+ /** @type {(node: btree.Node<string>) => (value: string) => btree.Node<string>} */
14
+ const set = node => value => {
15
+ const result = btree.setVisitor(stringCmp(value))(node)(() => value)
16
+ switch (result[0]) {
17
+ case 'replace': case 'overflow': { return result[1] }
18
+ default: { return node }
19
+ }
20
+ }
21
+
22
+ /** @type {(r: _.Result<json.Unknown>) => string} */
23
+ const str = ([r, path]) => jsonStr([r[0], list.toArray(list.map(x => x[0])(path))])
24
+
25
+ /** @type {(i: string) => (m: btree.Node<string>) => string} */
26
+ const find = i => m => str(_.find(stringCmp(i))(m))
27
+
28
+ {
29
+ /** @type {btree.Node<string>} */
30
+ let _map = ['1']
31
+ for (let i = 2; i <= 10; i++)
32
+ _map = set(_map)((i * i).toString())
33
+ {
34
+ const s = jsonStr(_map)
35
+ if (s !== '[[["1","100"],"16",["25","36"]],"4",[["49"],"64",["81","9"]]]') { throw s }
36
+ }
37
+ //
38
+ {
39
+ const r = find("0")(_map)
40
+ if (r !== '[0,[0,0]]') { throw r }
41
+ }
42
+ {
43
+ const r = find("1")(_map)
44
+ if (r !== '[1,[0,0]]') { throw r }
45
+ }
46
+ {
47
+ const r = find("10")(_map)
48
+ if (r !== '[2,[0,0]]') { throw r }
49
+ }
50
+ {
51
+ const r = find("100")(_map)
52
+ if (r !== '[3,[0,0]]') { throw r }
53
+ }
54
+ {
55
+ const r = find("12")(_map)
56
+ if (r !== '[4,[0,0]]') { throw r }
57
+ }
58
+ {
59
+ const r = find("16")(_map)
60
+ if (r !== '[1,[0]]') { throw r }
61
+ }
62
+ {
63
+ const r = find("17")(_map)
64
+ if (r !== '[0,[2,0]]') { throw r }
65
+ }
66
+ {
67
+ const r = find("25")(_map)
68
+ if (r !== '[1,[2,0]]') { throw r }
69
+ }
70
+ {
71
+ const r = find("26")(_map)
72
+ if (r !== '[2,[2,0]]') { throw r }
73
+ }
74
+ {
75
+ const r = find("36")(_map)
76
+ if (r !== '[3,[2,0]]') { throw r }
77
+ }
78
+ {
79
+ const r = find("37")(_map)
80
+ if (r !== '[4,[2,0]]') { throw r }
81
+ }
82
+ {
83
+ const r = find("4")(_map)
84
+ if (r !== '[1,[]]') { throw r }
85
+ }
86
+ {
87
+ const r = find("41")(_map)
88
+ if (r !== '[0,[0,2]]') { throw r }
89
+ }
90
+ {
91
+ const r = find("49")(_map)
92
+ if (r !== '[1,[0,2]]') { throw r }
93
+ }
94
+ {
95
+ const r = find("5")(_map)
96
+ if (r !== '[2,[0,2]]') { throw r }
97
+ }
98
+ {
99
+ const r = find("64")(_map)
100
+ if (r !== '[1,[2]]') { throw r }
101
+ }
102
+ {
103
+ const r = find("65")(_map)
104
+ if (r !== '[0,[2,2]]') { throw r }
105
+ }
106
+ {
107
+ const r = find("81")(_map)
108
+ if (r !== '[1,[2,2]]') { throw r }
109
+ }
110
+ {
111
+ const r = find("85")(_map)
112
+ if (r !== '[2,[2,2]]') { throw r }
113
+ }
114
+ {
115
+ const r = find("9")(_map)
116
+ if (r !== '[3,[2,2]]') { throw r }
117
+ }
118
+ {
119
+ const r = find("91")(_map)
120
+ if (r !== '[4,[2,2]]') { throw r }
121
+ }
122
+ }
@@ -5,6 +5,8 @@ const { sort } = require('../object')
5
5
  const { stringCmp } = require('../function/compare')
6
6
  const list = require('../list')
7
7
 
8
+ require('./find/test')
9
+
8
10
  const jsonStr = json.stringify(sort)
9
11
 
10
12
  /** @type {(sequence: list.List<json.Unknown>) => string} */