semantic-typescript 0.3.3 → 0.3.8
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/dist/collectable.d.ts +19 -24
- package/dist/collectable.js +373 -175
- package/dist/collector.d.ts +101 -24
- package/dist/collector.js +402 -146
- package/dist/factory.d.ts +38 -8
- package/dist/factory.js +300 -163
- package/dist/guard.d.ts +8 -3
- package/dist/guard.js +29 -5
- package/dist/hook.d.ts +13 -0
- package/dist/hook.js +114 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/optional.js +36 -3
- package/dist/semantic.d.ts +17 -10
- package/dist/semantic.js +355 -195
- package/dist/statistics.d.ts +13 -17
- package/dist/statistics.js +234 -522
- package/dist/utility.d.ts +4 -0
- package/dist/utility.js +2 -1
- package/dist/window.d.ts +12 -0
- package/dist/window.js +70 -0
- package/package.json +1 -1
- package/readme.cn.md +732 -210
- package/readme.md +426 -150
package/dist/collector.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isBigInt, isBoolean, isCollectable, isFunction, isIterable, isNumber, isObject, isSemantic, isString } from "./guard";
|
|
2
|
+
import { useCompare, useToBigInt, useToNumber } from "./hook";
|
|
2
3
|
import { Optional } from "./optional";
|
|
3
4
|
import { CollectableSymbol } from "./symbol";
|
|
4
5
|
import { validate, invalidate } from "./utility";
|
|
@@ -8,12 +9,45 @@ export class Collector {
|
|
|
8
9
|
accumulator;
|
|
9
10
|
finisher;
|
|
10
11
|
Collector = CollectableSymbol;
|
|
11
|
-
constructor(identity,
|
|
12
|
-
if (isFunction(identity) && isFunction(
|
|
12
|
+
constructor(identity, interrupt, accumulator, finisher) {
|
|
13
|
+
if (isFunction(identity) && isFunction(interrupt) && isFunction(accumulator) && isFunction(finisher)) {
|
|
13
14
|
this.identity = identity;
|
|
14
|
-
this.interrupt =
|
|
15
|
+
this.interrupt = interrupt;
|
|
15
16
|
this.accumulator = accumulator;
|
|
16
17
|
this.finisher = finisher;
|
|
18
|
+
Object.defineProperties(this, {
|
|
19
|
+
"identity": {
|
|
20
|
+
enumerable: false,
|
|
21
|
+
writable: false,
|
|
22
|
+
configurable: false,
|
|
23
|
+
value: identity,
|
|
24
|
+
},
|
|
25
|
+
"interrupt": {
|
|
26
|
+
enumerable: false,
|
|
27
|
+
writable: false,
|
|
28
|
+
configurable: false,
|
|
29
|
+
value: interrupt,
|
|
30
|
+
},
|
|
31
|
+
"accumulator": {
|
|
32
|
+
enumerable: false,
|
|
33
|
+
writable: false,
|
|
34
|
+
configurable: false,
|
|
35
|
+
value: accumulator,
|
|
36
|
+
},
|
|
37
|
+
"finisher": {
|
|
38
|
+
enumerable: false,
|
|
39
|
+
writable: false,
|
|
40
|
+
configurable: false,
|
|
41
|
+
value: finisher,
|
|
42
|
+
},
|
|
43
|
+
"Collector": {
|
|
44
|
+
enumerable: false,
|
|
45
|
+
writable: false,
|
|
46
|
+
configurable: false,
|
|
47
|
+
value: CollectableSymbol,
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
Object.freeze(this);
|
|
17
51
|
}
|
|
18
52
|
else {
|
|
19
53
|
throw new TypeError("Invalid arguments");
|
|
@@ -23,42 +57,20 @@ export class Collector {
|
|
|
23
57
|
let accumulator = this.identity();
|
|
24
58
|
let count = 0n;
|
|
25
59
|
if (isFunction(argument1)) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
accumulator = this.accumulator(accumulator, element, index);
|
|
29
|
-
count++;
|
|
30
|
-
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
31
|
-
}
|
|
32
|
-
else if (isIterable(argument1)) {
|
|
33
|
-
let iterable = argument1;
|
|
34
|
-
let index = 0n;
|
|
35
|
-
for (let element of iterable) {
|
|
36
|
-
if (this.interrupt(element, index, accumulator)) {
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
accumulator = this.accumulator(accumulator, element, count);
|
|
40
|
-
count++;
|
|
41
|
-
index++;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
else if (isSemantic(argument1)) {
|
|
45
|
-
let semantic = argument1;
|
|
46
|
-
let generator = Reflect.get(semantic, "generator");
|
|
47
|
-
if (isFunction(generator)) {
|
|
60
|
+
try {
|
|
61
|
+
let generator = argument1;
|
|
48
62
|
generator((element, index) => {
|
|
49
63
|
accumulator = this.accumulator(accumulator, element, index);
|
|
50
64
|
count++;
|
|
51
65
|
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
52
66
|
}
|
|
53
|
-
|
|
54
|
-
throw new
|
|
67
|
+
catch (error) {
|
|
68
|
+
throw new Error("Uncaught error on collect.");
|
|
55
69
|
}
|
|
56
70
|
}
|
|
57
|
-
else if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (isIterable(source)) {
|
|
61
|
-
let iterable = source;
|
|
71
|
+
else if (isIterable(argument1)) {
|
|
72
|
+
try {
|
|
73
|
+
let iterable = argument1;
|
|
62
74
|
let index = 0n;
|
|
63
75
|
for (let element of iterable) {
|
|
64
76
|
if (this.interrupt(element, index, accumulator)) {
|
|
@@ -69,34 +81,74 @@ export class Collector {
|
|
|
69
81
|
index++;
|
|
70
82
|
}
|
|
71
83
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
84
|
+
catch (error) {
|
|
85
|
+
throw new Error("Uncaught error on collect.");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (isSemantic(argument1)) {
|
|
89
|
+
try {
|
|
90
|
+
let semantic = argument1;
|
|
91
|
+
let generator = Reflect.get(semantic, "generator");
|
|
92
|
+
if (isFunction(generator)) {
|
|
93
|
+
generator((element, index) => {
|
|
94
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
95
|
+
count++;
|
|
96
|
+
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
throw new TypeError("Invalid arguments");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
throw new Error("Uncaught error on collect.");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (isCollectable(argument1)) {
|
|
107
|
+
try {
|
|
108
|
+
let collectable = argument1;
|
|
109
|
+
let source = collectable.source();
|
|
110
|
+
if (isFunction(source)) {
|
|
111
|
+
let generator = source;
|
|
112
|
+
generator((element, index) => {
|
|
113
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
114
|
+
count++;
|
|
115
|
+
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
throw new Error("Uncaught error on collect.");
|
|
78
120
|
}
|
|
79
121
|
}
|
|
80
122
|
else if (isNumber(argument1) && isNumber(argument2)) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
123
|
+
try {
|
|
124
|
+
let start = argument1 < argument2 ? argument1 : argument2;
|
|
125
|
+
let end = argument1 > argument2 ? argument1 : argument2;
|
|
126
|
+
for (let i = start; i < end; i++) {
|
|
127
|
+
if (this.interrupt(i, count, accumulator)) {
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
accumulator = this.accumulator(accumulator, i, count);
|
|
131
|
+
count++;
|
|
86
132
|
}
|
|
87
|
-
|
|
88
|
-
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
throw new Error("Uncaught error on collect.");
|
|
89
136
|
}
|
|
90
137
|
}
|
|
91
138
|
else if (isBigInt(argument1) && isBigInt(argument2)) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
139
|
+
try {
|
|
140
|
+
let start = argument1 < argument2 ? argument1 : argument2;
|
|
141
|
+
let end = argument1 > argument2 ? argument1 : argument2;
|
|
142
|
+
for (let i = start; i < end; i++) {
|
|
143
|
+
if (this.interrupt(i, count, accumulator)) {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
accumulator = this.accumulator(accumulator, i, count);
|
|
147
|
+
count++;
|
|
97
148
|
}
|
|
98
|
-
|
|
99
|
-
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
throw new Error("Uncaught error on collect.");
|
|
100
152
|
}
|
|
101
153
|
}
|
|
102
154
|
return this.finisher(accumulator);
|
|
@@ -109,6 +161,9 @@ export class Collector {
|
|
|
109
161
|
}
|
|
110
162
|
}
|
|
111
163
|
;
|
|
164
|
+
Object.freeze(Collector);
|
|
165
|
+
Object.freeze(Collector.prototype);
|
|
166
|
+
Object.freeze(Object.getPrototypeOf(Collector));
|
|
112
167
|
export let useAnyMatch = (predicate) => {
|
|
113
168
|
if (isFunction(predicate)) {
|
|
114
169
|
return Collector.shortable(() => false, (_element, _index, accumulator) => isBoolean(accumulator) && accumulator, (accumulator, element) => accumulator || predicate(element), (accumulator) => accumulator);
|
|
@@ -143,30 +198,20 @@ export let useCount = () => {
|
|
|
143
198
|
};
|
|
144
199
|
;
|
|
145
200
|
export let useError = (argument1, argument2, argument3) => {
|
|
146
|
-
if (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}, (text) => {
|
|
153
|
-
let result = text.substring(0, text.length - 1) + "]";
|
|
154
|
-
console.error(result);
|
|
155
|
-
return result;
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
159
|
-
let accumulator = argument1;
|
|
160
|
-
return Collector.full(() => "[", accumulator, (text) => {
|
|
161
|
-
let result = text.substring(0, text.length - 1) + "]";
|
|
201
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
202
|
+
let prefix = argument1;
|
|
203
|
+
let accumulator = argument2;
|
|
204
|
+
let suffix = argument3;
|
|
205
|
+
return Collector.full(() => prefix, accumulator, (text) => {
|
|
206
|
+
let result = text + suffix;
|
|
162
207
|
console.error(result);
|
|
163
208
|
return result;
|
|
164
209
|
});
|
|
165
210
|
}
|
|
166
|
-
else if (
|
|
167
|
-
let prefix =
|
|
211
|
+
else if (isFunction(argument1)) {
|
|
212
|
+
let prefix = "[";
|
|
168
213
|
let accumulator = argument2;
|
|
169
|
-
let suffix =
|
|
214
|
+
let suffix = "]";
|
|
170
215
|
return Collector.full(() => prefix, accumulator, (text) => {
|
|
171
216
|
let result = text + suffix;
|
|
172
217
|
console.error(result);
|
|
@@ -174,7 +219,16 @@ export let useError = (argument1, argument2, argument3) => {
|
|
|
174
219
|
});
|
|
175
220
|
}
|
|
176
221
|
else {
|
|
177
|
-
|
|
222
|
+
return Collector.full(() => "[", (accumulator, element) => {
|
|
223
|
+
if (isString(accumulator) && isString(element)) {
|
|
224
|
+
return accumulator + element + ",";
|
|
225
|
+
}
|
|
226
|
+
return String(accumulator) + String(element) + ",";
|
|
227
|
+
}, (text) => {
|
|
228
|
+
let result = text.substring(0, Math.max(1, text.length - 1)) + "]";
|
|
229
|
+
console.error(result);
|
|
230
|
+
return result;
|
|
231
|
+
});
|
|
178
232
|
}
|
|
179
233
|
};
|
|
180
234
|
export let useFindFirst = () => {
|
|
@@ -201,6 +255,22 @@ export let useFindLast = () => {
|
|
|
201
255
|
return accumulator;
|
|
202
256
|
}, (accumulator) => accumulator);
|
|
203
257
|
};
|
|
258
|
+
export let useFindMaximum = (comparator = (useCompare)) => {
|
|
259
|
+
return Collector.full(() => Optional.ofNullable(), (accumulator, element) => {
|
|
260
|
+
if (accumulator.isPresent()) {
|
|
261
|
+
return comparator(accumulator.get(), element) > 0 ? accumulator : Optional.ofNullable(element);
|
|
262
|
+
}
|
|
263
|
+
return Optional.ofNullable(element);
|
|
264
|
+
}, (result) => result);
|
|
265
|
+
};
|
|
266
|
+
export let useFindMinimum = (comparator = (useCompare)) => {
|
|
267
|
+
return Collector.full(() => Optional.ofNullable(), (accumulator, element) => {
|
|
268
|
+
if (accumulator.isPresent()) {
|
|
269
|
+
return comparator(accumulator.get(), element) < 0 ? accumulator : Optional.ofNullable(element);
|
|
270
|
+
}
|
|
271
|
+
return Optional.ofNullable(element);
|
|
272
|
+
}, (result) => result);
|
|
273
|
+
};
|
|
204
274
|
;
|
|
205
275
|
export let useForEach = (action) => {
|
|
206
276
|
if (isFunction(action)) {
|
|
@@ -266,30 +336,20 @@ export let useJoin = (argument1, argument2, argument3) => {
|
|
|
266
336
|
};
|
|
267
337
|
;
|
|
268
338
|
export let useLog = (argument1, argument2, argument3) => {
|
|
269
|
-
if (
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}, (text) => {
|
|
276
|
-
let result = text.substring(0, text.length - 1) + "]";
|
|
277
|
-
console.log(result);
|
|
278
|
-
return result;
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
282
|
-
let accumulator = argument1;
|
|
283
|
-
return Collector.full(() => "[", accumulator, (text) => {
|
|
284
|
-
let result = text.substring(0, text.length - 1) + "]";
|
|
339
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
340
|
+
let prefix = argument1;
|
|
341
|
+
let accumulator = argument2;
|
|
342
|
+
let suffix = argument3;
|
|
343
|
+
return Collector.full(() => prefix, accumulator, (text) => {
|
|
344
|
+
let result = text + suffix;
|
|
285
345
|
console.log(result);
|
|
286
346
|
return result;
|
|
287
347
|
});
|
|
288
348
|
}
|
|
289
|
-
else if (
|
|
290
|
-
let prefix =
|
|
349
|
+
else if (isFunction(argument1)) {
|
|
350
|
+
let prefix = "[";
|
|
291
351
|
let accumulator = argument2;
|
|
292
|
-
let suffix =
|
|
352
|
+
let suffix = "]";
|
|
293
353
|
return Collector.full(() => prefix, accumulator, (text) => {
|
|
294
354
|
let result = text + suffix;
|
|
295
355
|
console.log(result);
|
|
@@ -297,7 +357,17 @@ export let useLog = (argument1, argument2, argument3) => {
|
|
|
297
357
|
});
|
|
298
358
|
}
|
|
299
359
|
else {
|
|
300
|
-
|
|
360
|
+
return Collector.full(() => "[", (accumulator, element) => {
|
|
361
|
+
console.log(element);
|
|
362
|
+
if (isString(accumulator) && isString(element)) {
|
|
363
|
+
return accumulator + element + ",";
|
|
364
|
+
}
|
|
365
|
+
return String(accumulator) + String(element) + ",";
|
|
366
|
+
}, (text) => {
|
|
367
|
+
let result = text.substring(0, Math.max(1, text.length - 1)) + "]";
|
|
368
|
+
console.log(result);
|
|
369
|
+
return result;
|
|
370
|
+
});
|
|
301
371
|
}
|
|
302
372
|
};
|
|
303
373
|
export let usePartition = (count) => {
|
|
@@ -427,63 +497,59 @@ export let useWrite = (argument1, argument2) => {
|
|
|
427
497
|
throw new TypeError("Invalid arguments.");
|
|
428
498
|
};
|
|
429
499
|
;
|
|
430
|
-
export let
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
500
|
+
export let useNumericSummate = (mapper = useToNumber) => {
|
|
501
|
+
return Collector.full(() => 0, (accumulator, element) => {
|
|
502
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
503
|
+
return accumulator + (isNumber(resolved) ? resolved : 0);
|
|
504
|
+
}, (result) => result);
|
|
505
|
+
};
|
|
506
|
+
;
|
|
507
|
+
export let useBigIntSummate = (mapper = useToBigInt) => {
|
|
508
|
+
return Collector.full(() => 0n, (accumulator, element) => {
|
|
509
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
510
|
+
return accumulator + (isBigInt(resolved) ? resolved : 0n);
|
|
511
|
+
}, (result) => result);
|
|
512
|
+
};
|
|
513
|
+
;
|
|
514
|
+
;
|
|
515
|
+
export let useNumericAverage = (mapper = useToNumber) => {
|
|
446
516
|
return Collector.full(() => {
|
|
447
517
|
return {
|
|
448
518
|
summate: 0,
|
|
449
519
|
count: 0
|
|
450
520
|
};
|
|
451
|
-
}, (
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
521
|
+
}, (accumulator, element) => {
|
|
522
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
523
|
+
return {
|
|
524
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
525
|
+
count: accumulator.count + 1
|
|
526
|
+
};
|
|
527
|
+
}, (result) => {
|
|
528
|
+
if (result.count === 0) {
|
|
529
|
+
return 0;
|
|
530
|
+
}
|
|
531
|
+
return result.summate / result.count;
|
|
457
532
|
});
|
|
458
533
|
};
|
|
459
534
|
;
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return Collector.full(() => {
|
|
463
|
-
return {
|
|
464
|
-
summate: 0n,
|
|
465
|
-
count: 0n
|
|
466
|
-
};
|
|
467
|
-
}, (information, element) => {
|
|
468
|
-
let value = mapper(element);
|
|
469
|
-
information.summate += value;
|
|
470
|
-
information.count++;
|
|
471
|
-
return information;
|
|
472
|
-
}, (information) => {
|
|
473
|
-
return information.summate / information.count;
|
|
474
|
-
});
|
|
475
|
-
}
|
|
535
|
+
;
|
|
536
|
+
export let useBigIntAverage = (mapper = useToBigInt) => {
|
|
476
537
|
return Collector.full(() => {
|
|
477
538
|
return {
|
|
478
539
|
summate: 0n,
|
|
479
540
|
count: 0n
|
|
480
541
|
};
|
|
481
|
-
}, (
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
542
|
+
}, (accumulator, element) => {
|
|
543
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
544
|
+
return {
|
|
545
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
546
|
+
count: accumulator.count + 1n
|
|
547
|
+
};
|
|
548
|
+
}, (result) => {
|
|
549
|
+
if (result.count === 0n) {
|
|
550
|
+
return 0n;
|
|
551
|
+
}
|
|
552
|
+
return result.summate / result.count;
|
|
487
553
|
});
|
|
488
554
|
};
|
|
489
555
|
export let useFrequency = () => {
|
|
@@ -494,14 +560,204 @@ export let useFrequency = () => {
|
|
|
494
560
|
}, (map) => map);
|
|
495
561
|
};
|
|
496
562
|
;
|
|
497
|
-
export let
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
563
|
+
export let useNumericMode = (mapper = useToNumber) => {
|
|
564
|
+
return Collector.full(() => new Map(), (map, element) => {
|
|
565
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
566
|
+
let count = map.get(resolved) || 0n;
|
|
567
|
+
map.set(resolved, count + 1n);
|
|
568
|
+
return map;
|
|
569
|
+
}, (map) => {
|
|
570
|
+
let maxCount = 0n;
|
|
571
|
+
let mode = 0;
|
|
572
|
+
for (let [key, value] of map) {
|
|
573
|
+
if (value > maxCount) {
|
|
574
|
+
maxCount = value;
|
|
575
|
+
mode = key;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return mode;
|
|
579
|
+
});
|
|
580
|
+
};
|
|
581
|
+
;
|
|
582
|
+
export let useBigIntMode = (mapper = useToBigInt) => {
|
|
583
|
+
return Collector.full(() => new Map(), (map, element) => {
|
|
584
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
585
|
+
let count = map.get(resolved) || 0n;
|
|
586
|
+
map.set(resolved, count + 1n);
|
|
587
|
+
return map;
|
|
588
|
+
}, (map) => {
|
|
589
|
+
let maxCount = 0n;
|
|
590
|
+
let mode = 0n;
|
|
591
|
+
for (let [key, value] of map) {
|
|
592
|
+
if (value > maxCount) {
|
|
593
|
+
maxCount = value;
|
|
594
|
+
mode = key;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return mode;
|
|
598
|
+
});
|
|
599
|
+
};
|
|
600
|
+
;
|
|
601
|
+
;
|
|
602
|
+
export let useNumericVariance = (mapper = useToNumber) => {
|
|
603
|
+
return Collector.full(() => {
|
|
604
|
+
return {
|
|
605
|
+
summate: 0,
|
|
606
|
+
summateOfSquares: 0,
|
|
607
|
+
count: 0
|
|
608
|
+
};
|
|
609
|
+
}, (accumulator, element) => {
|
|
610
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
611
|
+
return {
|
|
612
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
613
|
+
summateOfSquares: accumulator.summateOfSquares + (isNumber(resolved) ? Math.pow(resolved, 2) : 0),
|
|
614
|
+
count: accumulator.count + 1
|
|
615
|
+
};
|
|
616
|
+
}, (result) => {
|
|
617
|
+
if (result.count < 2) {
|
|
618
|
+
return 0;
|
|
619
|
+
}
|
|
620
|
+
let mean = result.summate / result.count;
|
|
621
|
+
let variance = (result.summateOfSquares / result.count) - Math.pow(mean, 2);
|
|
622
|
+
return variance;
|
|
623
|
+
});
|
|
624
|
+
};
|
|
625
|
+
;
|
|
626
|
+
;
|
|
627
|
+
export let useBigIntVariance = (mapper = useToBigInt) => {
|
|
628
|
+
return Collector.full(() => {
|
|
629
|
+
return {
|
|
630
|
+
summate: 0n,
|
|
631
|
+
summateOfSquares: 0n,
|
|
632
|
+
count: 0n
|
|
633
|
+
};
|
|
634
|
+
}, (accumulator, element) => {
|
|
635
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
636
|
+
return {
|
|
637
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
638
|
+
summateOfSquares: accumulator.summateOfSquares + (isBigInt(resolved) ? resolved * resolved : 0n),
|
|
639
|
+
count: accumulator.count + 1n
|
|
640
|
+
};
|
|
641
|
+
}, (result) => {
|
|
642
|
+
if (result.count < 2n) {
|
|
643
|
+
return 0n;
|
|
644
|
+
}
|
|
645
|
+
let mean = result.summate / result.count;
|
|
646
|
+
let variance = (result.summateOfSquares / result.count) - (mean * mean);
|
|
647
|
+
return variance;
|
|
648
|
+
});
|
|
649
|
+
};
|
|
650
|
+
;
|
|
651
|
+
;
|
|
652
|
+
export let useNumericStandardDeviation = (mapper = useToNumber) => {
|
|
653
|
+
return Collector.full(() => {
|
|
654
|
+
return {
|
|
655
|
+
summate: 0,
|
|
656
|
+
summateOfSquares: 0,
|
|
657
|
+
count: 0
|
|
658
|
+
};
|
|
659
|
+
}, (accumulator, element) => {
|
|
660
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
661
|
+
return {
|
|
662
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
663
|
+
summateOfSquares: accumulator.summateOfSquares + (isNumber(resolved) ? Math.pow(resolved, 2) : 0),
|
|
664
|
+
count: accumulator.count + 1
|
|
665
|
+
};
|
|
666
|
+
}, (result) => {
|
|
667
|
+
if (result.count < 2) {
|
|
668
|
+
return 0;
|
|
669
|
+
}
|
|
670
|
+
let mean = result.summate / result.count;
|
|
671
|
+
let variance = (result.summateOfSquares / result.count) - Math.pow(mean, 2);
|
|
672
|
+
let standardDeviation = Math.sqrt(variance);
|
|
673
|
+
return standardDeviation;
|
|
674
|
+
});
|
|
675
|
+
};
|
|
676
|
+
;
|
|
677
|
+
;
|
|
678
|
+
export let useBigIntStandardDeviation = (mapper = useToBigInt) => {
|
|
679
|
+
return Collector.full(() => {
|
|
680
|
+
return {
|
|
681
|
+
summate: 0n,
|
|
682
|
+
summateOfSquares: 0n,
|
|
683
|
+
count: 0n
|
|
684
|
+
};
|
|
685
|
+
}, (accumulator, element) => {
|
|
686
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
687
|
+
return {
|
|
688
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
689
|
+
summateOfSquares: accumulator.summateOfSquares + (isBigInt(resolved) ? resolved * resolved : 0n),
|
|
690
|
+
count: accumulator.count + 1n
|
|
691
|
+
};
|
|
692
|
+
}, (result) => {
|
|
693
|
+
if (result.count < 2n) {
|
|
694
|
+
return 0n;
|
|
695
|
+
}
|
|
696
|
+
let mean = result.summate / result.count;
|
|
697
|
+
let variance = (result.summateOfSquares / result.count) - (mean * mean);
|
|
698
|
+
let standardDeviation = BigInt(Math.sqrt(Number(variance)));
|
|
699
|
+
return standardDeviation;
|
|
700
|
+
});
|
|
701
|
+
};
|
|
702
|
+
;
|
|
703
|
+
export let useNumericMedian = (mapper = useToNumber) => {
|
|
704
|
+
return Collector.full(() => [], (array, element) => {
|
|
705
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
706
|
+
array.push(resolved);
|
|
707
|
+
array.sort((a, b) => a - b);
|
|
708
|
+
return array;
|
|
709
|
+
}, (array) => {
|
|
710
|
+
let length = array.length;
|
|
711
|
+
if (length % 2 === 0) {
|
|
712
|
+
let mid = length / 2;
|
|
713
|
+
return (array[mid - 1] + array[mid]) / 2;
|
|
714
|
+
}
|
|
715
|
+
else {
|
|
716
|
+
let mid = Math.floor(length / 2);
|
|
717
|
+
return array[mid];
|
|
718
|
+
}
|
|
719
|
+
});
|
|
720
|
+
};
|
|
721
|
+
;
|
|
722
|
+
export let useBigIntMedian = (mapper = useToBigInt) => {
|
|
723
|
+
return Collector.full(() => [], (array, element) => {
|
|
724
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
725
|
+
array.push(resolved);
|
|
726
|
+
array.sort((a, b) => Number(a - b));
|
|
727
|
+
return array;
|
|
728
|
+
}, (array) => {
|
|
729
|
+
let length = array.length;
|
|
730
|
+
if (length % 2 === 0) {
|
|
731
|
+
let mid = length / 2;
|
|
732
|
+
return (array[Number(mid - 1)] + array[mid]) / 2n;
|
|
733
|
+
}
|
|
734
|
+
else {
|
|
735
|
+
let mid = Math.floor(length / 2);
|
|
736
|
+
return array[mid];
|
|
737
|
+
}
|
|
738
|
+
});
|
|
739
|
+
};
|
|
740
|
+
export let useToGeneratorFunction = () => {
|
|
741
|
+
return Collector.full(() => [], (array, element) => {
|
|
742
|
+
array.push(element);
|
|
743
|
+
return array;
|
|
744
|
+
}, (array) => {
|
|
745
|
+
return (function* () {
|
|
746
|
+
for (let element of array) {
|
|
747
|
+
yield element;
|
|
748
|
+
}
|
|
749
|
+
})();
|
|
750
|
+
});
|
|
751
|
+
};
|
|
752
|
+
export let useToAsyncGeneratorFunction = () => {
|
|
753
|
+
return Collector.full(() => [], (array, element) => {
|
|
754
|
+
array.push(element);
|
|
755
|
+
return array;
|
|
756
|
+
}, (array) => {
|
|
757
|
+
return (async function* () {
|
|
758
|
+
for (let element of array) {
|
|
759
|
+
yield element;
|
|
760
|
+
}
|
|
761
|
+
})();
|
|
762
|
+
});
|
|
507
763
|
};
|