semantic-typescript 0.0.1
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/semantic.d.ts +297 -0
- package/dist/semantic.js +1816 -0
- package/package.json +17 -0
package/dist/semantic.js
ADDED
|
@@ -0,0 +1,1816 @@
|
|
|
1
|
+
export let validate = (t) => {
|
|
2
|
+
return t !== null && t !== (void 0);
|
|
3
|
+
};
|
|
4
|
+
export let invalidate = (t) => {
|
|
5
|
+
return t === null || t === undefined;
|
|
6
|
+
};
|
|
7
|
+
export let isBoolean = (t) => {
|
|
8
|
+
return typeof t === "boolean";
|
|
9
|
+
};
|
|
10
|
+
export let isString = (t) => {
|
|
11
|
+
return typeof t === "string";
|
|
12
|
+
};
|
|
13
|
+
export let isNumber = (t) => {
|
|
14
|
+
return typeof t === "number";
|
|
15
|
+
};
|
|
16
|
+
export let isFunction = (t) => {
|
|
17
|
+
return typeof t === "function";
|
|
18
|
+
};
|
|
19
|
+
export let isObject = (t) => {
|
|
20
|
+
return typeof t === "object" && t !== null;
|
|
21
|
+
};
|
|
22
|
+
export let isSymbol = (t) => {
|
|
23
|
+
return typeof t === "symbol";
|
|
24
|
+
};
|
|
25
|
+
export let isBigint = (t) => {
|
|
26
|
+
return typeof t === "bigint";
|
|
27
|
+
};
|
|
28
|
+
export let isIterable = (t) => {
|
|
29
|
+
if (isObject(t)) {
|
|
30
|
+
return isFunction(Reflect.get(t, Symbol.iterator));
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
};
|
|
34
|
+
export let useCompare = (t1, t2) => {
|
|
35
|
+
if (typeof t1 === typeof t2) {
|
|
36
|
+
switch (typeof t1) {
|
|
37
|
+
case "string":
|
|
38
|
+
return t1.localeCompare(t2);
|
|
39
|
+
case "number":
|
|
40
|
+
return t1 - t2;
|
|
41
|
+
case "bigint":
|
|
42
|
+
return Number(t1 - t2);
|
|
43
|
+
case "boolean":
|
|
44
|
+
return t1 ? 1 : -1;
|
|
45
|
+
case "symbol":
|
|
46
|
+
return t1.toString().localeCompare(t2.toString());
|
|
47
|
+
case "function":
|
|
48
|
+
throw new TypeError("Cannot compare functions.");
|
|
49
|
+
case "undefined":
|
|
50
|
+
return 0;
|
|
51
|
+
case "object":
|
|
52
|
+
let a = Object.prototype.valueOf.call(t1);
|
|
53
|
+
let b = Object.prototype.valueOf.call(t2);
|
|
54
|
+
if (a === b) {
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
if (a < b) {
|
|
58
|
+
return -1;
|
|
59
|
+
}
|
|
60
|
+
return 1;
|
|
61
|
+
default:
|
|
62
|
+
throw new TypeError("Invalid type.");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
throw new TypeError("Cannot compare values of different types.");
|
|
66
|
+
};
|
|
67
|
+
export let useRandom = (index) => {
|
|
68
|
+
if (isNumber(index)) {
|
|
69
|
+
let x = Number(index);
|
|
70
|
+
let phi = (1 + Math.sqrt(5)) / 2;
|
|
71
|
+
let vanDerCorput = (base, n) => {
|
|
72
|
+
let result = 0;
|
|
73
|
+
let f = 1 / base;
|
|
74
|
+
let i = n;
|
|
75
|
+
while (i > 0) {
|
|
76
|
+
result += (i % base) * f;
|
|
77
|
+
i = Math.floor(i / base);
|
|
78
|
+
f = f / base;
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
};
|
|
82
|
+
let h = vanDerCorput(2, x) + vanDerCorput(3, x);
|
|
83
|
+
let golden = (x * phi) % 1;
|
|
84
|
+
let lcg = (1103515245 * x + 12345) % 2147483648;
|
|
85
|
+
let mixed = (h * 0.5 + golden * 0.3 + lcg / 2147483648 * 0.2);
|
|
86
|
+
return (mixed * 1000000);
|
|
87
|
+
}
|
|
88
|
+
if (isBigint(index)) {
|
|
89
|
+
let x = BigInt(index);
|
|
90
|
+
let two = 2n;
|
|
91
|
+
let three = 3n;
|
|
92
|
+
let scale = 1000000n;
|
|
93
|
+
let vanDerCorput = (base, n) => {
|
|
94
|
+
let result = 0n;
|
|
95
|
+
let f = 1n;
|
|
96
|
+
let i = n;
|
|
97
|
+
let basePower = 1n;
|
|
98
|
+
while (i > 0n) {
|
|
99
|
+
result += (i % base) * f;
|
|
100
|
+
i = i / base;
|
|
101
|
+
f = f * scale / basePower;
|
|
102
|
+
basePower = basePower * base;
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
};
|
|
106
|
+
let h1 = vanDerCorput(two, x);
|
|
107
|
+
let h2 = vanDerCorput(three, x);
|
|
108
|
+
let combined = (h1 + h2) % (scale * 10n);
|
|
109
|
+
let random = (combined * 123456789n) % scale;
|
|
110
|
+
return random;
|
|
111
|
+
}
|
|
112
|
+
throw new TypeError("Invalid input type");
|
|
113
|
+
};
|
|
114
|
+
export let OptionalSymbol = Symbol.for("Optional");
|
|
115
|
+
export let SemanticSymbol = Symbol.for("Semantic");
|
|
116
|
+
export let CollectorsSymbol = Symbol.for("Collector");
|
|
117
|
+
export let CollectableSymbol = Symbol.for("Collectable");
|
|
118
|
+
export let OrderedCollectableSymbol = Symbol.for("OrderedCollectable");
|
|
119
|
+
export let UnorderedCollectableSymbol = Symbol.for("UnorderedCollectable");
|
|
120
|
+
export let StatisticsSymbol = Symbol.for("Statistics");
|
|
121
|
+
export let isOptional = (t) => {
|
|
122
|
+
if (isObject(t)) {
|
|
123
|
+
return Reflect.get(t, "Optional") === OptionalSymbol;
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
};
|
|
127
|
+
export let isSemantic = (t) => {
|
|
128
|
+
if (isObject(t)) {
|
|
129
|
+
return Reflect.get(t, "Semantic") === SemanticSymbol;
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
};
|
|
133
|
+
export let isCollector = (t) => {
|
|
134
|
+
if (isObject(t)) {
|
|
135
|
+
return Reflect.get(t, "Collector") === CollectorsSymbol;
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
};
|
|
139
|
+
export let isCollectable = (t) => {
|
|
140
|
+
if (isObject(t)) {
|
|
141
|
+
return Reflect.get(t, "Collectable") === CollectableSymbol;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
};
|
|
145
|
+
export let isOrderedCollectable = (t) => {
|
|
146
|
+
if (isObject(t)) {
|
|
147
|
+
return Reflect.get(t, "OrderedCollectable") === OrderedCollectableSymbol;
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
150
|
+
};
|
|
151
|
+
export let isUnorderedCollectable = (t) => {
|
|
152
|
+
if (isObject(t)) {
|
|
153
|
+
return Reflect.get(t, "UnorderedCollectable") === UnorderedCollectableSymbol;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
};
|
|
157
|
+
export let isStatistics = (t) => {
|
|
158
|
+
if (isObject(t)) {
|
|
159
|
+
return Reflect.get(t, "Statistics") === StatisticsSymbol;
|
|
160
|
+
}
|
|
161
|
+
return false;
|
|
162
|
+
};
|
|
163
|
+
;
|
|
164
|
+
;
|
|
165
|
+
;
|
|
166
|
+
;
|
|
167
|
+
;
|
|
168
|
+
;
|
|
169
|
+
;
|
|
170
|
+
;
|
|
171
|
+
;
|
|
172
|
+
;
|
|
173
|
+
class Optional {
|
|
174
|
+
value;
|
|
175
|
+
Optional = OptionalSymbol;
|
|
176
|
+
constructor(value) {
|
|
177
|
+
this.value = value;
|
|
178
|
+
}
|
|
179
|
+
filter(predicate) {
|
|
180
|
+
if (this.isPresent() && predicate(this.value)) {
|
|
181
|
+
return new Optional(this.value);
|
|
182
|
+
}
|
|
183
|
+
return new Optional((void 0));
|
|
184
|
+
}
|
|
185
|
+
get() {
|
|
186
|
+
if (this.isPresent()) {
|
|
187
|
+
return this.value;
|
|
188
|
+
}
|
|
189
|
+
throw new TypeError("Optional is empty");
|
|
190
|
+
}
|
|
191
|
+
getOrDefault(defaultValue) {
|
|
192
|
+
if (this.isPresent()) {
|
|
193
|
+
return this.value;
|
|
194
|
+
}
|
|
195
|
+
if (validate(defaultValue)) {
|
|
196
|
+
return defaultValue;
|
|
197
|
+
}
|
|
198
|
+
throw new TypeError("Default value is not valid");
|
|
199
|
+
}
|
|
200
|
+
ifPresent(action) {
|
|
201
|
+
if (this.isPresent()) {
|
|
202
|
+
action(this.value);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
isEmpty() {
|
|
206
|
+
return invalidate(this.value);
|
|
207
|
+
}
|
|
208
|
+
isPresent() {
|
|
209
|
+
return validate(this.value);
|
|
210
|
+
}
|
|
211
|
+
map(mapper) {
|
|
212
|
+
if (this.isPresent()) {
|
|
213
|
+
return new Optional(mapper(this.value));
|
|
214
|
+
}
|
|
215
|
+
return new Optional(null);
|
|
216
|
+
}
|
|
217
|
+
static of(value) {
|
|
218
|
+
return Optional.ofNullable(value);
|
|
219
|
+
}
|
|
220
|
+
static ofNullable(value = (void 0)) {
|
|
221
|
+
return new Optional(value);
|
|
222
|
+
}
|
|
223
|
+
static ofNonNull(value) {
|
|
224
|
+
if (validate(value)) {
|
|
225
|
+
return new Optional(value);
|
|
226
|
+
}
|
|
227
|
+
throw new TypeError("Value is not valid");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
;
|
|
231
|
+
export let empty = () => {
|
|
232
|
+
return new Semantic(() => { });
|
|
233
|
+
};
|
|
234
|
+
export let fill = (element, count) => {
|
|
235
|
+
if (validate(element) && count > 0n) {
|
|
236
|
+
return new Semantic((accept, interrupt) => {
|
|
237
|
+
for (let i = 0n; i < count; i++) {
|
|
238
|
+
let item = isFunction(element) ? element() : element;
|
|
239
|
+
if (interrupt(item)) {
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
accept(item, i);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
throw new TypeError("Invalid arguments.");
|
|
247
|
+
};
|
|
248
|
+
export let from = (iterable) => {
|
|
249
|
+
if (isIterable(iterable)) {
|
|
250
|
+
return new Semantic((accept, interrupt) => {
|
|
251
|
+
let index = 0n;
|
|
252
|
+
for (let element of iterable) {
|
|
253
|
+
if (interrupt(element)) {
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
accept(element, index);
|
|
257
|
+
index++;
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
throw new TypeError("Invalid arguments");
|
|
262
|
+
};
|
|
263
|
+
export let range = (start, end, step = (typeof start === 'bigint' ? 1n : 1)) => {
|
|
264
|
+
if ((isNumber(step) && step === 0) || (isBigint(step) && step === 0n)) {
|
|
265
|
+
throw new TypeError("Step cannot be zero.");
|
|
266
|
+
}
|
|
267
|
+
if (isNumber(start) && isNumber(end) && isNumber(step)) {
|
|
268
|
+
let minimum = start, maximum = end, limit = step;
|
|
269
|
+
let condition = limit > 0 ? (i) => i < maximum : (i) => i > maximum;
|
|
270
|
+
return new Semantic((accept, interrupt) => {
|
|
271
|
+
for (let i = minimum; condition(i); i += limit) {
|
|
272
|
+
let value = i;
|
|
273
|
+
if (interrupt(value)) {
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
accept(value, BigInt(i));
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
else if (isBigint(start) && isBigint(end) && isBigint(step)) {
|
|
281
|
+
let minimum = start, maximum = end, limit = step;
|
|
282
|
+
let condition = limit > 0n ? (i) => i < maximum : (i) => i > maximum;
|
|
283
|
+
return new Semantic((accept, interrupt) => {
|
|
284
|
+
for (let i = minimum; condition(i); i += limit) {
|
|
285
|
+
let value = i;
|
|
286
|
+
if (interrupt(value)) {
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
accept(value, i);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
throw new TypeError("Invalid arguments.");
|
|
294
|
+
};
|
|
295
|
+
export function iterate(generator) {
|
|
296
|
+
return new Semantic(generator);
|
|
297
|
+
}
|
|
298
|
+
export class Semantic {
|
|
299
|
+
generator;
|
|
300
|
+
Semantic = SemanticSymbol;
|
|
301
|
+
constructor(generator) {
|
|
302
|
+
this.generator = generator;
|
|
303
|
+
}
|
|
304
|
+
concat(other) {
|
|
305
|
+
if (isSemantic(other)) {
|
|
306
|
+
return new Semantic((accept, interrupt) => {
|
|
307
|
+
let count = 0n;
|
|
308
|
+
this.generator((element, index) => {
|
|
309
|
+
accept(element, index);
|
|
310
|
+
count++;
|
|
311
|
+
}, interrupt);
|
|
312
|
+
other.generator((element, index) => {
|
|
313
|
+
accept(element, index + count);
|
|
314
|
+
}, interrupt);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
if (isIterable(other)) {
|
|
318
|
+
return new Semantic((accept, interrupt) => {
|
|
319
|
+
let count = 0n;
|
|
320
|
+
this.generator((element, index) => {
|
|
321
|
+
accept(element, index);
|
|
322
|
+
count++;
|
|
323
|
+
}, interrupt);
|
|
324
|
+
for (let element of other) {
|
|
325
|
+
accept(element, count);
|
|
326
|
+
count++;
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
throw new TypeError("Invalid arguments.");
|
|
331
|
+
}
|
|
332
|
+
distinct(comparator) {
|
|
333
|
+
if (validate(comparator)) {
|
|
334
|
+
return new Semantic((accept, interrupt) => {
|
|
335
|
+
let array = [];
|
|
336
|
+
this.generator((element, index) => {
|
|
337
|
+
if (!array.some((e) => comparator(e, element))) {
|
|
338
|
+
array.push(element);
|
|
339
|
+
accept(element, index);
|
|
340
|
+
}
|
|
341
|
+
}, interrupt);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
return new Semantic((accept, interrupt) => {
|
|
345
|
+
let set = new Set();
|
|
346
|
+
this.generator((element, index) => {
|
|
347
|
+
if (!set.has(element)) {
|
|
348
|
+
set.add(element);
|
|
349
|
+
accept(element, index);
|
|
350
|
+
}
|
|
351
|
+
}, interrupt);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
dropWhile(predicate) {
|
|
355
|
+
if (isFunction(predicate)) {
|
|
356
|
+
return new Semantic((accept, interrupt) => {
|
|
357
|
+
let dropping = true;
|
|
358
|
+
this.generator((element, index) => {
|
|
359
|
+
if (dropping) {
|
|
360
|
+
if (!predicate(element)) {
|
|
361
|
+
dropping = false;
|
|
362
|
+
accept(element, index);
|
|
363
|
+
}
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
accept(element, index);
|
|
367
|
+
}, interrupt);
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
throw new TypeError("Invalid arguments.");
|
|
371
|
+
}
|
|
372
|
+
filter(predicate) {
|
|
373
|
+
if (isFunction(predicate)) {
|
|
374
|
+
return new Semantic((accept, interrupt) => {
|
|
375
|
+
this.generator((element, index) => {
|
|
376
|
+
if (predicate(element)) {
|
|
377
|
+
accept(element, index);
|
|
378
|
+
}
|
|
379
|
+
}, interrupt);
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
throw new TypeError("Invalid arguments.");
|
|
383
|
+
}
|
|
384
|
+
flat(mapper) {
|
|
385
|
+
if (isFunction(mapper)) {
|
|
386
|
+
return new Semantic((accept, interrupt) => {
|
|
387
|
+
let count = 0n;
|
|
388
|
+
let stop = false;
|
|
389
|
+
this.generator((element, index) => {
|
|
390
|
+
let result = mapper(element);
|
|
391
|
+
if (isIterable(result)) {
|
|
392
|
+
let subIndex = 0n;
|
|
393
|
+
for (let subElement of result) {
|
|
394
|
+
accept(subElement, count + subIndex + index);
|
|
395
|
+
stop = stop || interrupt(subElement);
|
|
396
|
+
count++;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
else if (isSemantic(result)) {
|
|
400
|
+
result.generator((subElement, subIndex) => {
|
|
401
|
+
accept(subElement, count + subIndex + index);
|
|
402
|
+
stop = stop || interrupt(subElement);
|
|
403
|
+
count++;
|
|
404
|
+
}, (element) => interrupt(element) || stop);
|
|
405
|
+
}
|
|
406
|
+
}, (element) => interrupt(element) || stop);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
throw new TypeError("Invalid arguments.");
|
|
410
|
+
}
|
|
411
|
+
flatMap(mapper) {
|
|
412
|
+
if (isFunction(mapper)) {
|
|
413
|
+
return new Semantic((accept, interrupt) => {
|
|
414
|
+
let count = 0n;
|
|
415
|
+
let stop = false;
|
|
416
|
+
this.generator((element) => {
|
|
417
|
+
let result = mapper(element);
|
|
418
|
+
if (isIterable(result)) {
|
|
419
|
+
for (let subElement of result) {
|
|
420
|
+
accept(subElement, count);
|
|
421
|
+
stop = stop || interrupt(subElement);
|
|
422
|
+
count++;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
else if (isSemantic(result)) {
|
|
426
|
+
result.generator((subElement) => {
|
|
427
|
+
accept(subElement, count);
|
|
428
|
+
stop = stop || interrupt(subElement);
|
|
429
|
+
count++;
|
|
430
|
+
}, (element) => interrupt(element) || stop);
|
|
431
|
+
}
|
|
432
|
+
}, () => stop);
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
throw new TypeError("Invalid arguments.");
|
|
436
|
+
}
|
|
437
|
+
limit(n) {
|
|
438
|
+
if (isNumber(n)) {
|
|
439
|
+
let limit = BigInt(n);
|
|
440
|
+
return new Semantic((accept, interrupt) => {
|
|
441
|
+
let count = 0n;
|
|
442
|
+
this.generator((element, index) => {
|
|
443
|
+
if (count < limit) {
|
|
444
|
+
accept(element, index);
|
|
445
|
+
count++;
|
|
446
|
+
}
|
|
447
|
+
}, (element) => interrupt(element) || count >= limit);
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
if (isBigint(n)) {
|
|
451
|
+
let limit = n;
|
|
452
|
+
return new Semantic((accept, interrupt) => {
|
|
453
|
+
let count = 0n;
|
|
454
|
+
this.generator((element, index) => {
|
|
455
|
+
if (count < limit) {
|
|
456
|
+
accept(element, index);
|
|
457
|
+
count++;
|
|
458
|
+
}
|
|
459
|
+
}, (element) => interrupt(element) || count >= limit);
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
throw new TypeError("Invalid arguments.");
|
|
463
|
+
}
|
|
464
|
+
map(mapper) {
|
|
465
|
+
if (isFunction(mapper)) {
|
|
466
|
+
return new Semantic((accept, interrupt) => {
|
|
467
|
+
let stop = false;
|
|
468
|
+
this.generator((element, index) => {
|
|
469
|
+
let resolved = mapper(element);
|
|
470
|
+
accept(resolved, index);
|
|
471
|
+
stop = stop || interrupt(resolved);
|
|
472
|
+
}, () => stop);
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
throw new TypeError("Invalid arguments.");
|
|
476
|
+
}
|
|
477
|
+
peek(consumer) {
|
|
478
|
+
if (isFunction(consumer)) {
|
|
479
|
+
return new Semantic((accept, interrupt) => {
|
|
480
|
+
this.generator((element, index) => {
|
|
481
|
+
accept(element, index);
|
|
482
|
+
consumer(element, index);
|
|
483
|
+
}, interrupt);
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
throw new TypeError("Invalid arguments.");
|
|
487
|
+
}
|
|
488
|
+
redirect(redirector) {
|
|
489
|
+
if (isFunction(redirector)) {
|
|
490
|
+
return new Semantic((accept, interrupt) => {
|
|
491
|
+
this.generator((element, index) => {
|
|
492
|
+
accept(element, redirector(element, index));
|
|
493
|
+
}, interrupt);
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
throw new TypeError("Invalid arguments.");
|
|
497
|
+
}
|
|
498
|
+
reverse() {
|
|
499
|
+
return new Semantic((accept, interrupt) => {
|
|
500
|
+
this.generator((element, index) => {
|
|
501
|
+
accept(element, -index);
|
|
502
|
+
}, interrupt);
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
shuffle(mapper) {
|
|
506
|
+
if (isFunction(mapper)) {
|
|
507
|
+
return new Semantic((accept, interrupt) => {
|
|
508
|
+
this.generator((element, index) => {
|
|
509
|
+
accept(element, mapper(element, index));
|
|
510
|
+
}, interrupt);
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
return new Semantic((accept, interrupt) => {
|
|
514
|
+
this.generator((element, index) => {
|
|
515
|
+
accept(element, useRandom(index));
|
|
516
|
+
}, interrupt);
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
skip(n) {
|
|
520
|
+
if (isNumber(n)) {
|
|
521
|
+
return new Semantic((accept, interrupt) => {
|
|
522
|
+
let count = 0n;
|
|
523
|
+
let limit = BigInt(n);
|
|
524
|
+
this.generator((element, index) => {
|
|
525
|
+
if (count < limit) {
|
|
526
|
+
count++;
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
accept(element, index);
|
|
530
|
+
}
|
|
531
|
+
}, interrupt);
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
if (isBigint(n)) {
|
|
535
|
+
return new Semantic((accept, interrupt) => {
|
|
536
|
+
let count = 0n;
|
|
537
|
+
this.generator((element, index) => {
|
|
538
|
+
if (count < n) {
|
|
539
|
+
count++;
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
542
|
+
accept(element, index);
|
|
543
|
+
}
|
|
544
|
+
}, interrupt);
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
throw new TypeError("Invalid arguments.");
|
|
548
|
+
}
|
|
549
|
+
sorted(comparator) {
|
|
550
|
+
if (isFunction(comparator)) {
|
|
551
|
+
return new OrderedCollectable(this.generator, comparator);
|
|
552
|
+
}
|
|
553
|
+
return new OrderedCollectable(this.generator);
|
|
554
|
+
}
|
|
555
|
+
sub(start, end) {
|
|
556
|
+
return new Semantic((accept, interrupt) => {
|
|
557
|
+
let count = 0n;
|
|
558
|
+
this.generator((element, index) => {
|
|
559
|
+
if (count < end) {
|
|
560
|
+
count++;
|
|
561
|
+
if (count >= start) {
|
|
562
|
+
accept(element, index);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}, interrupt);
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
takeWhile(predicate) {
|
|
569
|
+
return new Semantic((accept, interrupt) => {
|
|
570
|
+
this.generator((element, index) => {
|
|
571
|
+
if (!predicate(element)) {
|
|
572
|
+
interrupt(element);
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
accept(element, index);
|
|
576
|
+
}, interrupt);
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
toOrdered() {
|
|
580
|
+
return new OrderedCollectable(this.generator);
|
|
581
|
+
}
|
|
582
|
+
toNumericStatistics() {
|
|
583
|
+
return new NumericStatistics(this.generator);
|
|
584
|
+
}
|
|
585
|
+
toBigintStatistics() {
|
|
586
|
+
return new BigIntStatistics(this.generator);
|
|
587
|
+
}
|
|
588
|
+
toUnoredered() {
|
|
589
|
+
return new UnorderedCollectable(this.generator);
|
|
590
|
+
}
|
|
591
|
+
toWindow() {
|
|
592
|
+
return new WindowCollectable(this.generator);
|
|
593
|
+
}
|
|
594
|
+
translate(...args) {
|
|
595
|
+
let parameter = args[0];
|
|
596
|
+
if (isBigint(parameter)) {
|
|
597
|
+
let offset = parameter;
|
|
598
|
+
return new Semantic((accept, interrupt) => {
|
|
599
|
+
this.generator((element, index) => {
|
|
600
|
+
accept(element, index + offset);
|
|
601
|
+
}, interrupt);
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
else if (isFunction(parameter)) {
|
|
605
|
+
let translator = parameter;
|
|
606
|
+
return new Semantic((accept, interrupt) => {
|
|
607
|
+
this.generator((element, index) => {
|
|
608
|
+
accept(element, index + translator(element, index));
|
|
609
|
+
}, interrupt);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
throw new TypeError("Invalid arguments.");
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
;
|
|
616
|
+
export class Collector {
|
|
617
|
+
identity;
|
|
618
|
+
interruptor;
|
|
619
|
+
accumulator;
|
|
620
|
+
finisher;
|
|
621
|
+
Collector = CollectableSymbol;
|
|
622
|
+
constructor(identity, interruptor, accumulator, finisher) {
|
|
623
|
+
if (isFunction(identity) && isFunction(interruptor) && isFunction(accumulator) && isFunction(finisher)) {
|
|
624
|
+
this.identity = identity;
|
|
625
|
+
this.interruptor = interruptor;
|
|
626
|
+
this.accumulator = accumulator;
|
|
627
|
+
this.finisher = finisher;
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
throw new TypeError("Invalid arguments");
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
collect(parameter) {
|
|
634
|
+
let accumulator = this.identity();
|
|
635
|
+
let count = 0n;
|
|
636
|
+
if (isFunction(parameter)) {
|
|
637
|
+
parameter((element, index) => {
|
|
638
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
639
|
+
count++;
|
|
640
|
+
}, this.interruptor);
|
|
641
|
+
}
|
|
642
|
+
else if (isIterable(parameter)) {
|
|
643
|
+
let iterable = parameter;
|
|
644
|
+
for (let element of iterable) {
|
|
645
|
+
accumulator = this.accumulator(accumulator, element, count);
|
|
646
|
+
count++;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return this.finisher(accumulator);
|
|
650
|
+
}
|
|
651
|
+
static full(identity, accumulator, finisher) {
|
|
652
|
+
return new Collector(identity, () => false, accumulator, finisher);
|
|
653
|
+
}
|
|
654
|
+
static shortable(identity, interruptor, accumulator, finisher) {
|
|
655
|
+
return new Collector(identity, interruptor, accumulator, finisher);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
;
|
|
659
|
+
export class Collectable {
|
|
660
|
+
constructor() {
|
|
661
|
+
}
|
|
662
|
+
anyMatch(predicate) {
|
|
663
|
+
return this.collect(() => {
|
|
664
|
+
return false;
|
|
665
|
+
}, (element) => {
|
|
666
|
+
return predicate(element);
|
|
667
|
+
}, (result, element) => {
|
|
668
|
+
return result || predicate(element);
|
|
669
|
+
}, (result) => {
|
|
670
|
+
return result;
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
allMatch(predicate) {
|
|
674
|
+
return this.collect(() => {
|
|
675
|
+
return true;
|
|
676
|
+
}, (element) => {
|
|
677
|
+
return !predicate(element);
|
|
678
|
+
}, (result, element) => {
|
|
679
|
+
return result && predicate(element);
|
|
680
|
+
}, (result) => {
|
|
681
|
+
return result;
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
collect(identityOrCollector, accumulatorOrInterruptor, accumulatorOrFinisher, finisher) {
|
|
685
|
+
let source = this.source();
|
|
686
|
+
if (isFunction(source) || isIterable(source)) {
|
|
687
|
+
if (isCollector(identityOrCollector)) {
|
|
688
|
+
return identityOrCollector.collect(source);
|
|
689
|
+
}
|
|
690
|
+
if (isFunction(identityOrCollector) && isFunction(accumulatorOrInterruptor) && isFunction(accumulatorOrFinisher)) {
|
|
691
|
+
if (isFunction(finisher)) {
|
|
692
|
+
return Collector.shortable(identityOrCollector, accumulatorOrInterruptor, accumulatorOrFinisher, finisher).collect(this.source);
|
|
693
|
+
}
|
|
694
|
+
return Collector.full(identityOrCollector, accumulatorOrInterruptor, accumulatorOrFinisher).collect(source);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
throw new TypeError("Invalid arguments.");
|
|
698
|
+
}
|
|
699
|
+
count() {
|
|
700
|
+
return this.collect(() => {
|
|
701
|
+
return 0n;
|
|
702
|
+
}, (count) => {
|
|
703
|
+
return count + 1n;
|
|
704
|
+
}, (count) => {
|
|
705
|
+
return count;
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
isEmpty() {
|
|
709
|
+
return this.count() === 0n;
|
|
710
|
+
}
|
|
711
|
+
findAny() {
|
|
712
|
+
return this.collect(() => {
|
|
713
|
+
return Optional.ofNullable();
|
|
714
|
+
}, () => {
|
|
715
|
+
return true;
|
|
716
|
+
}, (result, element) => {
|
|
717
|
+
return result.isPresent() && Math.random() > 0.5 ? result : Optional.of(element);
|
|
718
|
+
}, (result) => {
|
|
719
|
+
return result;
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
findFirst() {
|
|
723
|
+
return this.collect(() => {
|
|
724
|
+
return Optional.ofNullable();
|
|
725
|
+
}, () => {
|
|
726
|
+
return true;
|
|
727
|
+
}, (result, element) => {
|
|
728
|
+
return result.isPresent() ? result : Optional.of(element);
|
|
729
|
+
}, (result) => {
|
|
730
|
+
return result;
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
findLast() {
|
|
734
|
+
return this.collect(() => {
|
|
735
|
+
return Optional.ofNullable();
|
|
736
|
+
}, () => {
|
|
737
|
+
return true;
|
|
738
|
+
}, (result, element) => {
|
|
739
|
+
return result.isPresent() ? result : Optional.of(element);
|
|
740
|
+
}, (result) => {
|
|
741
|
+
return result;
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
forEach(action) {
|
|
745
|
+
if (isFunction(action)) {
|
|
746
|
+
this.collect(() => {
|
|
747
|
+
return 0n;
|
|
748
|
+
}, (count, element) => {
|
|
749
|
+
action(element, count);
|
|
750
|
+
return count + 1n;
|
|
751
|
+
}, (count) => {
|
|
752
|
+
return count;
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
group(classifier) {
|
|
757
|
+
if (isFunction(classifier)) {
|
|
758
|
+
return this.collect(() => {
|
|
759
|
+
return new Map();
|
|
760
|
+
}, (map, element) => {
|
|
761
|
+
let key = classifier(element);
|
|
762
|
+
let raw = map.get(key);
|
|
763
|
+
let array = validate(raw) ? raw : [];
|
|
764
|
+
array.push(element);
|
|
765
|
+
map.set(key, array);
|
|
766
|
+
return map;
|
|
767
|
+
}, (map) => {
|
|
768
|
+
return map;
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
throw new TypeError("Invalid arguments.");
|
|
772
|
+
}
|
|
773
|
+
groupBy(keyExtractor, valueExtractor) {
|
|
774
|
+
return this.collect(() => {
|
|
775
|
+
return new Map();
|
|
776
|
+
}, (map, element) => {
|
|
777
|
+
let key = keyExtractor(element);
|
|
778
|
+
let value = valueExtractor(element);
|
|
779
|
+
map.set(key, value);
|
|
780
|
+
return map;
|
|
781
|
+
}, (map) => {
|
|
782
|
+
return map;
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
join(argument1, argument2, argument3) {
|
|
786
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
787
|
+
return this.collect(() => {
|
|
788
|
+
return "[";
|
|
789
|
+
}, (text, element) => {
|
|
790
|
+
return text + element + ",";
|
|
791
|
+
}, (text) => {
|
|
792
|
+
return text.substring(0, text.length - 1) + "]";
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
if (isString(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
796
|
+
let delimiter = argument1;
|
|
797
|
+
return this.collect(() => {
|
|
798
|
+
return "[";
|
|
799
|
+
}, (text, element) => {
|
|
800
|
+
return text + element + delimiter;
|
|
801
|
+
}, (text) => {
|
|
802
|
+
return text.substring(0, text.length - 1) + "]";
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
806
|
+
let prefix = argument1;
|
|
807
|
+
let accumulator = argument2;
|
|
808
|
+
let suffix = argument3;
|
|
809
|
+
return this.collect(() => {
|
|
810
|
+
return prefix;
|
|
811
|
+
}, (text, element, index) => {
|
|
812
|
+
return text + accumulator(text, element, index);
|
|
813
|
+
}, (text) => {
|
|
814
|
+
return text + suffix;
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
if (isString(argument1) && isString(argument2) && isString(argument3)) {
|
|
818
|
+
let prefix = argument1;
|
|
819
|
+
let delimiter = argument2;
|
|
820
|
+
let suffix = argument3;
|
|
821
|
+
return this.collect(() => {
|
|
822
|
+
return prefix;
|
|
823
|
+
}, (text, element) => {
|
|
824
|
+
return text + element + delimiter;
|
|
825
|
+
}, (text) => {
|
|
826
|
+
return text + suffix;
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
throw new TypeError("Invalid arguments.");
|
|
830
|
+
}
|
|
831
|
+
log(argument1, argument2, argument3) {
|
|
832
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
833
|
+
let text = this.join();
|
|
834
|
+
console.log(text);
|
|
835
|
+
}
|
|
836
|
+
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
837
|
+
let accumulator = argument1;
|
|
838
|
+
let text = this.join("[", accumulator, "]");
|
|
839
|
+
console.log(text);
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
throw new TypeError("Invalid arguments.");
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
nonMatch(predicate) {
|
|
846
|
+
return this.collect(() => {
|
|
847
|
+
return true;
|
|
848
|
+
}, (element) => {
|
|
849
|
+
return predicate(element);
|
|
850
|
+
}, (result, element) => {
|
|
851
|
+
return result || predicate(element);
|
|
852
|
+
}, (result) => {
|
|
853
|
+
return result;
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
partition(count) {
|
|
857
|
+
let limited = count > 1n ? count : 1n;
|
|
858
|
+
return this.collect(() => {
|
|
859
|
+
return [];
|
|
860
|
+
}, (array, element) => {
|
|
861
|
+
let index = limited % BigInt(array.length);
|
|
862
|
+
if (index === 0n) {
|
|
863
|
+
array.push([]);
|
|
864
|
+
}
|
|
865
|
+
array[Number(index)].push(element);
|
|
866
|
+
return array;
|
|
867
|
+
}, (result) => {
|
|
868
|
+
return result;
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
partitionBy(classifier) {
|
|
872
|
+
return this.collect(() => {
|
|
873
|
+
return [];
|
|
874
|
+
}, (array, element) => {
|
|
875
|
+
let index = classifier(element);
|
|
876
|
+
while (index > BigInt(array.length) - 1n) {
|
|
877
|
+
array.push([]);
|
|
878
|
+
}
|
|
879
|
+
array[Number(index)].push(element);
|
|
880
|
+
return array;
|
|
881
|
+
}, (result) => {
|
|
882
|
+
return result;
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
reduce(argument1, argument2, argument3) {
|
|
886
|
+
if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
887
|
+
let accumulator = argument1;
|
|
888
|
+
return this.collect(() => Optional.ofNullable(), (result, element, index) => {
|
|
889
|
+
if (result.isEmpty()) {
|
|
890
|
+
return Optional.of(element);
|
|
891
|
+
}
|
|
892
|
+
else {
|
|
893
|
+
let current = result.get();
|
|
894
|
+
return Optional.of(accumulator(current, element, index));
|
|
895
|
+
}
|
|
896
|
+
}, (result) => result);
|
|
897
|
+
}
|
|
898
|
+
else if (validate(argument1) && isFunction(argument2) && invalidate(argument3)) {
|
|
899
|
+
let identity = argument1;
|
|
900
|
+
let accumulator = argument2;
|
|
901
|
+
return this.collect(() => identity, (result, element, index) => {
|
|
902
|
+
return accumulator(result, element, index);
|
|
903
|
+
}, (result) => result);
|
|
904
|
+
}
|
|
905
|
+
else if (validate(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
906
|
+
let identity = argument1;
|
|
907
|
+
let accumulator = argument2;
|
|
908
|
+
let finisher = argument3;
|
|
909
|
+
return this.collect(() => identity, (result, element, index) => {
|
|
910
|
+
return accumulator(result, element, index);
|
|
911
|
+
}, (result) => finisher(result, result));
|
|
912
|
+
}
|
|
913
|
+
else {
|
|
914
|
+
throw new TypeError("Invalid arguments.");
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
semantic() {
|
|
918
|
+
let source = this.source();
|
|
919
|
+
if (isIterable(source)) {
|
|
920
|
+
return from(source);
|
|
921
|
+
}
|
|
922
|
+
else if (isFunction(source)) {
|
|
923
|
+
return new Semantic(source);
|
|
924
|
+
}
|
|
925
|
+
else {
|
|
926
|
+
throw new TypeError("Invalid source.");
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
toArray() {
|
|
930
|
+
return this.collect(() => {
|
|
931
|
+
return [];
|
|
932
|
+
}, (array, element) => {
|
|
933
|
+
array.push(element);
|
|
934
|
+
return array;
|
|
935
|
+
}, (result) => {
|
|
936
|
+
return result;
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
toMap(keyExtractor, valueExtractor) {
|
|
940
|
+
return this.collect(() => {
|
|
941
|
+
return new Map();
|
|
942
|
+
}, (map, element) => {
|
|
943
|
+
let key = keyExtractor(element);
|
|
944
|
+
let value = valueExtractor(element);
|
|
945
|
+
map.set(key, value);
|
|
946
|
+
return map;
|
|
947
|
+
}, (map) => {
|
|
948
|
+
return map;
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
toSet() {
|
|
952
|
+
return this.collect(() => {
|
|
953
|
+
return new Set();
|
|
954
|
+
}, (set, element) => {
|
|
955
|
+
set.add(element);
|
|
956
|
+
return set;
|
|
957
|
+
}, (result) => {
|
|
958
|
+
return result;
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
export class UnorderedCollectable extends Collectable {
|
|
963
|
+
generator;
|
|
964
|
+
constructor(generator) {
|
|
965
|
+
super();
|
|
966
|
+
this.generator = generator;
|
|
967
|
+
}
|
|
968
|
+
source() {
|
|
969
|
+
return this.generator;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
export class OrderedCollectable extends Collectable {
|
|
973
|
+
ordered = [];
|
|
974
|
+
constructor(argument1, argument2) {
|
|
975
|
+
super();
|
|
976
|
+
if (isIterable(argument1)) {
|
|
977
|
+
let iterable = argument1;
|
|
978
|
+
let index = 0n;
|
|
979
|
+
for (let element of iterable) {
|
|
980
|
+
this.ordered.push({ index: index, value: element });
|
|
981
|
+
index++;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
else if (isFunction(argument1)) {
|
|
985
|
+
let generator = argument1;
|
|
986
|
+
generator((element, index) => {
|
|
987
|
+
this.ordered.push({ index: index, value: element });
|
|
988
|
+
}, () => false);
|
|
989
|
+
}
|
|
990
|
+
else {
|
|
991
|
+
throw new TypeError("Invalid arguments.");
|
|
992
|
+
}
|
|
993
|
+
if (isFunction(argument2)) {
|
|
994
|
+
let comparator = argument2;
|
|
995
|
+
this.ordered = this.ordered.sort((a, b) => comparator(a.value, b.value)).map((indexed, index) => {
|
|
996
|
+
return {
|
|
997
|
+
index: BigInt(index),
|
|
998
|
+
value: indexed.value
|
|
999
|
+
};
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
else {
|
|
1003
|
+
this.ordered = this.ordered.sort((a, b) => {
|
|
1004
|
+
return useCompare(a.value, b.value);
|
|
1005
|
+
}).map((indexed, index) => {
|
|
1006
|
+
return {
|
|
1007
|
+
index: BigInt(index),
|
|
1008
|
+
value: indexed.value
|
|
1009
|
+
};
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
source() {
|
|
1014
|
+
this.ordered.forEach((indexed) => {
|
|
1015
|
+
console.log(indexed.index, indexed.value);
|
|
1016
|
+
});
|
|
1017
|
+
return this.ordered.map((indexed) => indexed.value);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
export class WindowCollectable extends OrderedCollectable {
|
|
1021
|
+
constructor(parameter, comparator) {
|
|
1022
|
+
if (isIterable(parameter)) {
|
|
1023
|
+
if (isFunction(comparator)) {
|
|
1024
|
+
super(parameter, comparator);
|
|
1025
|
+
}
|
|
1026
|
+
else {
|
|
1027
|
+
super(parameter);
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
else if (isFunction(parameter)) {
|
|
1031
|
+
if (isFunction(comparator)) {
|
|
1032
|
+
super(parameter, comparator);
|
|
1033
|
+
}
|
|
1034
|
+
else {
|
|
1035
|
+
super(parameter);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
slide(size, step = 1n) {
|
|
1040
|
+
if (size > 0n && step > 0n) {
|
|
1041
|
+
let source = this.toArray();
|
|
1042
|
+
let windows = [];
|
|
1043
|
+
let windowStartIndex = 0n;
|
|
1044
|
+
while (windowStartIndex < BigInt(source.length)) {
|
|
1045
|
+
let windowEnd = windowStartIndex + size;
|
|
1046
|
+
let window = source.slice(Number(windowStartIndex), Number(windowEnd));
|
|
1047
|
+
if (window.length > 0) {
|
|
1048
|
+
windows.push(window);
|
|
1049
|
+
}
|
|
1050
|
+
windowStartIndex += step;
|
|
1051
|
+
}
|
|
1052
|
+
return from(windows).map((window) => from(window));
|
|
1053
|
+
}
|
|
1054
|
+
throw new RangeError("Invalid arguments.");
|
|
1055
|
+
}
|
|
1056
|
+
tumble(size) {
|
|
1057
|
+
return this.slide(size, size);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
;
|
|
1061
|
+
export class Statistics extends OrderedCollectable {
|
|
1062
|
+
constructor(parameter, comparator) {
|
|
1063
|
+
if (isIterable(parameter)) {
|
|
1064
|
+
if (isFunction(comparator)) {
|
|
1065
|
+
super(parameter, comparator);
|
|
1066
|
+
}
|
|
1067
|
+
else {
|
|
1068
|
+
super(parameter);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
else if (isFunction(parameter)) {
|
|
1072
|
+
if (isFunction(comparator)) {
|
|
1073
|
+
super(parameter, comparator);
|
|
1074
|
+
}
|
|
1075
|
+
else {
|
|
1076
|
+
super(parameter);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
count() {
|
|
1081
|
+
return BigInt(this.ordered.length);
|
|
1082
|
+
}
|
|
1083
|
+
maximum(argument1) {
|
|
1084
|
+
if (invalidate(argument1)) {
|
|
1085
|
+
if (!this.isEmpty()) {
|
|
1086
|
+
return this.collect(() => {
|
|
1087
|
+
return Optional.ofNullable();
|
|
1088
|
+
}, (result, element) => {
|
|
1089
|
+
if (result.isEmpty()) {
|
|
1090
|
+
return Optional.of(element);
|
|
1091
|
+
}
|
|
1092
|
+
else {
|
|
1093
|
+
let current = result.get();
|
|
1094
|
+
if (useCompare(current, element) > 0) {
|
|
1095
|
+
return Optional.of(element);
|
|
1096
|
+
}
|
|
1097
|
+
else {
|
|
1098
|
+
return result;
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
}, (result) => result);
|
|
1102
|
+
}
|
|
1103
|
+
else {
|
|
1104
|
+
return Optional.ofNullable();
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
else {
|
|
1108
|
+
let comparator = argument1;
|
|
1109
|
+
if (!this.isEmpty()) {
|
|
1110
|
+
return this.collect(() => {
|
|
1111
|
+
return Optional.ofNullable();
|
|
1112
|
+
}, (result, element) => {
|
|
1113
|
+
if (result.isEmpty()) {
|
|
1114
|
+
return Optional.of(element);
|
|
1115
|
+
}
|
|
1116
|
+
else {
|
|
1117
|
+
let current = result.get();
|
|
1118
|
+
if (comparator(current, element) > 0) {
|
|
1119
|
+
return Optional.of(element);
|
|
1120
|
+
}
|
|
1121
|
+
else {
|
|
1122
|
+
return result;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}, (result) => result);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
return Optional.ofNullable();
|
|
1129
|
+
}
|
|
1130
|
+
minimum(argument1) {
|
|
1131
|
+
if (invalidate(argument1)) {
|
|
1132
|
+
if (!this.isEmpty()) {
|
|
1133
|
+
return this.collect(() => {
|
|
1134
|
+
return Optional.ofNullable();
|
|
1135
|
+
}, (result, element) => {
|
|
1136
|
+
if (result.isEmpty()) {
|
|
1137
|
+
return Optional.of(element);
|
|
1138
|
+
}
|
|
1139
|
+
else {
|
|
1140
|
+
let current = result.get();
|
|
1141
|
+
if (useCompare(current, element) < 0) {
|
|
1142
|
+
return Optional.of(element);
|
|
1143
|
+
}
|
|
1144
|
+
else {
|
|
1145
|
+
return result;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
}, (result) => result);
|
|
1149
|
+
}
|
|
1150
|
+
else {
|
|
1151
|
+
return Optional.ofNullable();
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
else {
|
|
1155
|
+
let comparator = argument1;
|
|
1156
|
+
if (!this.isEmpty()) {
|
|
1157
|
+
return this.collect(() => {
|
|
1158
|
+
return Optional.ofNullable();
|
|
1159
|
+
}, (result, element) => {
|
|
1160
|
+
if (result.isEmpty()) {
|
|
1161
|
+
return Optional.of(element);
|
|
1162
|
+
}
|
|
1163
|
+
else {
|
|
1164
|
+
let current = result.get();
|
|
1165
|
+
if (comparator(current, element) < 0) {
|
|
1166
|
+
return Optional.of(element);
|
|
1167
|
+
}
|
|
1168
|
+
else {
|
|
1169
|
+
return result;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
}, (result) => result);
|
|
1173
|
+
}
|
|
1174
|
+
return Optional.ofNullable();
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
;
|
|
1179
|
+
export class NumericStatistics extends Statistics {
|
|
1180
|
+
constructor(parameter, comparator) {
|
|
1181
|
+
if (isIterable(parameter)) {
|
|
1182
|
+
if (isFunction(comparator)) {
|
|
1183
|
+
super(parameter, comparator);
|
|
1184
|
+
}
|
|
1185
|
+
else {
|
|
1186
|
+
super(parameter);
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
else if (isFunction(parameter)) {
|
|
1190
|
+
if (isFunction(comparator)) {
|
|
1191
|
+
super(parameter, comparator);
|
|
1192
|
+
}
|
|
1193
|
+
else {
|
|
1194
|
+
super(parameter);
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
range(argument1) {
|
|
1199
|
+
if (this.isEmpty()) {
|
|
1200
|
+
return 0;
|
|
1201
|
+
}
|
|
1202
|
+
if (invalidate(argument1)) {
|
|
1203
|
+
let minimum = this.ordered[0].value;
|
|
1204
|
+
let maximum = this.ordered[0].value;
|
|
1205
|
+
for (let i = 1; i < this.ordered.length; i++) {
|
|
1206
|
+
let current = this.ordered[i].value;
|
|
1207
|
+
if (useCompare(current, minimum) < 0) {
|
|
1208
|
+
minimum = current;
|
|
1209
|
+
}
|
|
1210
|
+
if (useCompare(current, maximum) > 0) {
|
|
1211
|
+
maximum = current;
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
return useCompare(maximum, minimum);
|
|
1215
|
+
}
|
|
1216
|
+
else if (isFunction(argument1)) {
|
|
1217
|
+
let mapper = argument1;
|
|
1218
|
+
let minimum = mapper(this.ordered[0].value);
|
|
1219
|
+
let maximum = mapper(this.ordered[0].value);
|
|
1220
|
+
for (let i = 1; i < this.ordered.length; i++) {
|
|
1221
|
+
let current = mapper(this.ordered[i].value);
|
|
1222
|
+
if (current < minimum) {
|
|
1223
|
+
minimum = current;
|
|
1224
|
+
}
|
|
1225
|
+
if (current > maximum) {
|
|
1226
|
+
maximum = current;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
return maximum - minimum;
|
|
1230
|
+
}
|
|
1231
|
+
throw new TypeError("Invalid arguments.");
|
|
1232
|
+
}
|
|
1233
|
+
variance(argument1) {
|
|
1234
|
+
if (this.isEmpty() || this.count() === 1n) {
|
|
1235
|
+
return 0;
|
|
1236
|
+
}
|
|
1237
|
+
if (invalidate(argument1)) {
|
|
1238
|
+
let mean = this.mean();
|
|
1239
|
+
let summate = this.summate();
|
|
1240
|
+
return (summate / Number(this.count())) - (mean * mean);
|
|
1241
|
+
}
|
|
1242
|
+
else if (isFunction(argument1)) {
|
|
1243
|
+
let mapper = argument1;
|
|
1244
|
+
let mean = this.mean(mapper);
|
|
1245
|
+
let summate = this.summate(mapper);
|
|
1246
|
+
return (summate / Number(this.count())) - (mean * mean);
|
|
1247
|
+
}
|
|
1248
|
+
throw new TypeError("Invalid arguments.");
|
|
1249
|
+
}
|
|
1250
|
+
standardDeviation(argument1) {
|
|
1251
|
+
if (invalidate(argument1)) {
|
|
1252
|
+
return Math.sqrt(this.variance());
|
|
1253
|
+
}
|
|
1254
|
+
else if (isFunction(argument1)) {
|
|
1255
|
+
let mapper = argument1;
|
|
1256
|
+
let variance = this.variance(mapper);
|
|
1257
|
+
return Math.sqrt(variance);
|
|
1258
|
+
}
|
|
1259
|
+
throw new TypeError("Invalid arguments.");
|
|
1260
|
+
}
|
|
1261
|
+
mean(argument1) {
|
|
1262
|
+
if (this.isEmpty()) {
|
|
1263
|
+
return 0;
|
|
1264
|
+
}
|
|
1265
|
+
if (invalidate(argument1)) {
|
|
1266
|
+
let summate = this.summate();
|
|
1267
|
+
return summate / Number(this.count());
|
|
1268
|
+
}
|
|
1269
|
+
else if (isFunction(argument1)) {
|
|
1270
|
+
let mapper = argument1;
|
|
1271
|
+
let summate = this.summate(mapper);
|
|
1272
|
+
return summate / Number(this.count());
|
|
1273
|
+
}
|
|
1274
|
+
throw new TypeError("Invalid arguments.");
|
|
1275
|
+
}
|
|
1276
|
+
median(argument1) {
|
|
1277
|
+
if (this.isEmpty()) {
|
|
1278
|
+
return 0;
|
|
1279
|
+
}
|
|
1280
|
+
if (invalidate(argument1)) {
|
|
1281
|
+
let count = Number(this.count());
|
|
1282
|
+
let middle = Math.floor(count / 2);
|
|
1283
|
+
let median = Number(this.ordered[middle].value);
|
|
1284
|
+
if (count % 2 === 0) {
|
|
1285
|
+
median = (median + Number(this.ordered[middle - 1].value)) / 2;
|
|
1286
|
+
}
|
|
1287
|
+
return median;
|
|
1288
|
+
}
|
|
1289
|
+
else if (isFunction(argument1)) {
|
|
1290
|
+
let mapper = argument1;
|
|
1291
|
+
let count = Number(this.count());
|
|
1292
|
+
let middle = Math.floor(count / 2);
|
|
1293
|
+
let median = mapper(this.ordered[middle].value);
|
|
1294
|
+
if (count % 2 === 0) {
|
|
1295
|
+
median = (median + mapper(this.ordered[middle - 1].value)) / 2;
|
|
1296
|
+
}
|
|
1297
|
+
return median;
|
|
1298
|
+
}
|
|
1299
|
+
throw new TypeError("Invalid arguments.");
|
|
1300
|
+
}
|
|
1301
|
+
mode(argument1) {
|
|
1302
|
+
if (this.isEmpty()) {
|
|
1303
|
+
return 0;
|
|
1304
|
+
}
|
|
1305
|
+
if (invalidate(argument1)) {
|
|
1306
|
+
let frequency = this.frequency();
|
|
1307
|
+
let mode = 0;
|
|
1308
|
+
let maxFrequency = 0n;
|
|
1309
|
+
for (let [value, freq] of frequency) {
|
|
1310
|
+
if (freq > maxFrequency) {
|
|
1311
|
+
mode = value;
|
|
1312
|
+
maxFrequency = freq;
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
return mode;
|
|
1316
|
+
}
|
|
1317
|
+
else if (isFunction(argument1)) {
|
|
1318
|
+
let mapper = argument1;
|
|
1319
|
+
let frequency = this.frequency(mapper);
|
|
1320
|
+
let mode = 0;
|
|
1321
|
+
let maxFrequency = 0n;
|
|
1322
|
+
for (let [value, freq] of frequency) {
|
|
1323
|
+
if (freq > maxFrequency) {
|
|
1324
|
+
mode = value;
|
|
1325
|
+
maxFrequency = freq;
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
return mode;
|
|
1329
|
+
}
|
|
1330
|
+
throw new TypeError("Invalid arguments.");
|
|
1331
|
+
}
|
|
1332
|
+
frequency(argument1) {
|
|
1333
|
+
if (this.isEmpty()) {
|
|
1334
|
+
return new Map();
|
|
1335
|
+
}
|
|
1336
|
+
if (invalidate(argument1)) {
|
|
1337
|
+
let frequency = new Map();
|
|
1338
|
+
for (let i = 0; i < this.ordered.length; i++) {
|
|
1339
|
+
let current = Number(this.ordered[i].value);
|
|
1340
|
+
let count = frequency.get(current) || 0n;
|
|
1341
|
+
frequency.set(current, count + 1n);
|
|
1342
|
+
}
|
|
1343
|
+
return frequency;
|
|
1344
|
+
}
|
|
1345
|
+
else if (isFunction(argument1)) {
|
|
1346
|
+
let mapper = argument1;
|
|
1347
|
+
let frequency = new Map();
|
|
1348
|
+
for (let i = 0; i < this.ordered.length; i++) {
|
|
1349
|
+
let current = mapper(this.ordered[i].value);
|
|
1350
|
+
let count = frequency.get(current) || 0n;
|
|
1351
|
+
frequency.set(current, count + 1n);
|
|
1352
|
+
}
|
|
1353
|
+
return frequency;
|
|
1354
|
+
}
|
|
1355
|
+
throw new TypeError("Invalid arguments.");
|
|
1356
|
+
}
|
|
1357
|
+
summate(argument1) {
|
|
1358
|
+
if (this.isEmpty()) {
|
|
1359
|
+
return 0;
|
|
1360
|
+
}
|
|
1361
|
+
if (invalidate(argument1)) {
|
|
1362
|
+
let summate = 0;
|
|
1363
|
+
for (let i = 0; i < this.ordered.length; i++) {
|
|
1364
|
+
let current = Number(this.ordered[i].value);
|
|
1365
|
+
summate += current;
|
|
1366
|
+
}
|
|
1367
|
+
return summate;
|
|
1368
|
+
}
|
|
1369
|
+
else if (isFunction(argument1)) {
|
|
1370
|
+
let mapper = argument1;
|
|
1371
|
+
let summate = 0;
|
|
1372
|
+
for (let i = 0; i < this.ordered.length; i++) {
|
|
1373
|
+
let current = mapper(this.ordered[i].value);
|
|
1374
|
+
summate += current;
|
|
1375
|
+
}
|
|
1376
|
+
return summate;
|
|
1377
|
+
}
|
|
1378
|
+
throw new TypeError("Invalid arguments.");
|
|
1379
|
+
}
|
|
1380
|
+
quantile(quantile, argument1) {
|
|
1381
|
+
if (this.isEmpty()) {
|
|
1382
|
+
return 0;
|
|
1383
|
+
}
|
|
1384
|
+
if (quantile < 0 || quantile > 1) {
|
|
1385
|
+
throw new RangeError("Invalid quantile.");
|
|
1386
|
+
}
|
|
1387
|
+
if (invalidate(argument1)) {
|
|
1388
|
+
let count = Number(this.count());
|
|
1389
|
+
let index = Math.floor(quantile * count);
|
|
1390
|
+
if (index === count) {
|
|
1391
|
+
index--;
|
|
1392
|
+
}
|
|
1393
|
+
let value = Number(this.ordered[index].value);
|
|
1394
|
+
return value;
|
|
1395
|
+
}
|
|
1396
|
+
else if (isFunction(argument1)) {
|
|
1397
|
+
let mapper = argument1;
|
|
1398
|
+
let count = Number(this.count());
|
|
1399
|
+
let index = Math.floor(quantile * count);
|
|
1400
|
+
if (index === count) {
|
|
1401
|
+
index--;
|
|
1402
|
+
}
|
|
1403
|
+
let value = mapper(this.ordered[index].value);
|
|
1404
|
+
return value;
|
|
1405
|
+
}
|
|
1406
|
+
throw new TypeError("Invalid arguments.");
|
|
1407
|
+
}
|
|
1408
|
+
interquartileRange(argument1) {
|
|
1409
|
+
if (this.isEmpty()) {
|
|
1410
|
+
return 0;
|
|
1411
|
+
}
|
|
1412
|
+
if (invalidate(argument1)) {
|
|
1413
|
+
let lower = this.quantile(0.25);
|
|
1414
|
+
let upper = this.quantile(0.75);
|
|
1415
|
+
return upper - lower;
|
|
1416
|
+
}
|
|
1417
|
+
else if (isFunction(argument1)) {
|
|
1418
|
+
let mapper = argument1;
|
|
1419
|
+
let lower = this.quantile(0.25, mapper);
|
|
1420
|
+
let upper = this.quantile(0.75, mapper);
|
|
1421
|
+
return upper - lower;
|
|
1422
|
+
}
|
|
1423
|
+
throw new TypeError("Invalid arguments.");
|
|
1424
|
+
}
|
|
1425
|
+
skewness(argument1) {
|
|
1426
|
+
if (this.isEmpty()) {
|
|
1427
|
+
return 0;
|
|
1428
|
+
}
|
|
1429
|
+
if (invalidate(argument1)) {
|
|
1430
|
+
let mean = this.mean();
|
|
1431
|
+
let standardDeviation = this.standardDeviation();
|
|
1432
|
+
if (standardDeviation === 0) {
|
|
1433
|
+
return 0;
|
|
1434
|
+
}
|
|
1435
|
+
let data = this.toArray();
|
|
1436
|
+
let summate = 0;
|
|
1437
|
+
for (let value of data) {
|
|
1438
|
+
let z = (value - mean) / standardDeviation;
|
|
1439
|
+
summate += Math.pow(z, 3);
|
|
1440
|
+
}
|
|
1441
|
+
return summate / data.length;
|
|
1442
|
+
}
|
|
1443
|
+
else if (isFunction(argument1)) {
|
|
1444
|
+
let mapper = argument1;
|
|
1445
|
+
let mean = this.mean(mapper);
|
|
1446
|
+
let standardDeviation = this.standardDeviation(mapper);
|
|
1447
|
+
if (standardDeviation === 0) {
|
|
1448
|
+
return 0;
|
|
1449
|
+
}
|
|
1450
|
+
let data = this.toArray().map(item => mapper(item));
|
|
1451
|
+
let summate = 0;
|
|
1452
|
+
for (let value of data) {
|
|
1453
|
+
let z = (value - mean) / standardDeviation;
|
|
1454
|
+
summate += Math.pow(z, 3);
|
|
1455
|
+
}
|
|
1456
|
+
return summate / data.length;
|
|
1457
|
+
}
|
|
1458
|
+
throw new TypeError("Invalid arguments.");
|
|
1459
|
+
}
|
|
1460
|
+
kurtosis(argument1) {
|
|
1461
|
+
if (this.isEmpty()) {
|
|
1462
|
+
return 0;
|
|
1463
|
+
}
|
|
1464
|
+
if (invalidate(argument1)) {
|
|
1465
|
+
let mean = this.mean();
|
|
1466
|
+
let standardDeviation = this.standardDeviation();
|
|
1467
|
+
if (standardDeviation === 0) {
|
|
1468
|
+
return 0;
|
|
1469
|
+
}
|
|
1470
|
+
let data = this.toArray();
|
|
1471
|
+
let summate = 0;
|
|
1472
|
+
for (let value of data) {
|
|
1473
|
+
let z = (value - mean) / standardDeviation;
|
|
1474
|
+
summate += Math.pow(z, 4);
|
|
1475
|
+
}
|
|
1476
|
+
return summate / (data.length * data.length) - 3;
|
|
1477
|
+
}
|
|
1478
|
+
else if (isFunction(argument1)) {
|
|
1479
|
+
let mapper = argument1;
|
|
1480
|
+
let mean = this.mean(mapper);
|
|
1481
|
+
let standardDeviation = this.standardDeviation(mapper);
|
|
1482
|
+
if (standardDeviation === 0) {
|
|
1483
|
+
return 0;
|
|
1484
|
+
}
|
|
1485
|
+
let data = this.toArray().map(item => mapper(item));
|
|
1486
|
+
let summate = 0;
|
|
1487
|
+
for (let value of data) {
|
|
1488
|
+
let z = (value - mean) / standardDeviation;
|
|
1489
|
+
summate += Math.pow(z, 4);
|
|
1490
|
+
}
|
|
1491
|
+
return summate / (data.length * data.length) - 3;
|
|
1492
|
+
}
|
|
1493
|
+
throw new TypeError("Invalid arguments.");
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
;
|
|
1497
|
+
export class BigIntStatistics extends Statistics {
|
|
1498
|
+
constructor(parameter, comparator) {
|
|
1499
|
+
if (isIterable(parameter)) {
|
|
1500
|
+
if (isFunction(comparator)) {
|
|
1501
|
+
super(parameter, comparator);
|
|
1502
|
+
}
|
|
1503
|
+
else {
|
|
1504
|
+
super(parameter);
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
else if (isFunction(parameter)) {
|
|
1508
|
+
if (isFunction(comparator)) {
|
|
1509
|
+
super(parameter, comparator);
|
|
1510
|
+
}
|
|
1511
|
+
else {
|
|
1512
|
+
super(parameter);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
range(argument1) {
|
|
1517
|
+
if (this.isEmpty()) {
|
|
1518
|
+
return 0n;
|
|
1519
|
+
}
|
|
1520
|
+
if (invalidate(argument1)) {
|
|
1521
|
+
let minimum = this.ordered[0].value;
|
|
1522
|
+
let maximum = this.ordered[0].value;
|
|
1523
|
+
for (let i = 1; i < this.ordered.length; i++) {
|
|
1524
|
+
let current = this.ordered[i].value;
|
|
1525
|
+
if (useCompare(current, minimum) < 0) {
|
|
1526
|
+
minimum = current;
|
|
1527
|
+
}
|
|
1528
|
+
if (useCompare(current, maximum) > 0) {
|
|
1529
|
+
maximum = current;
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
return BigInt(useCompare(maximum, minimum));
|
|
1533
|
+
}
|
|
1534
|
+
else if (isFunction(argument1)) {
|
|
1535
|
+
let mapper = argument1;
|
|
1536
|
+
let minimum = mapper(this.ordered[0].value);
|
|
1537
|
+
let maximum = mapper(this.ordered[0].value);
|
|
1538
|
+
for (let i = 1; i < this.ordered.length; i++) {
|
|
1539
|
+
let current = mapper(this.ordered[i].value);
|
|
1540
|
+
if (current < minimum) {
|
|
1541
|
+
minimum = current;
|
|
1542
|
+
}
|
|
1543
|
+
if (current > maximum) {
|
|
1544
|
+
maximum = current;
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
return maximum - minimum;
|
|
1548
|
+
}
|
|
1549
|
+
throw new TypeError("Invalid arguments.");
|
|
1550
|
+
}
|
|
1551
|
+
variance(argument1) {
|
|
1552
|
+
if (this.isEmpty() || this.count() === 1n) {
|
|
1553
|
+
return 0n;
|
|
1554
|
+
}
|
|
1555
|
+
if (invalidate(argument1)) {
|
|
1556
|
+
let mean = this.mean();
|
|
1557
|
+
let summate = this.summate();
|
|
1558
|
+
return (summate / this.count()) - (mean * mean);
|
|
1559
|
+
}
|
|
1560
|
+
else if (isFunction(argument1)) {
|
|
1561
|
+
let mapper = argument1;
|
|
1562
|
+
let mean = this.mean(mapper);
|
|
1563
|
+
let summate = this.summate(mapper);
|
|
1564
|
+
return (summate / this.count()) - (mean * mean);
|
|
1565
|
+
}
|
|
1566
|
+
throw new TypeError("Invalid arguments.");
|
|
1567
|
+
}
|
|
1568
|
+
standardDeviation(argument1) {
|
|
1569
|
+
if (invalidate(argument1)) {
|
|
1570
|
+
return BigInt(Math.sqrt(Number(this.variance())));
|
|
1571
|
+
}
|
|
1572
|
+
else if (isFunction(argument1)) {
|
|
1573
|
+
let mapper = argument1;
|
|
1574
|
+
let variance = this.variance(mapper);
|
|
1575
|
+
return BigInt(Math.sqrt(Number(variance)));
|
|
1576
|
+
}
|
|
1577
|
+
throw new TypeError("Invalid arguments.");
|
|
1578
|
+
}
|
|
1579
|
+
mean(argument1) {
|
|
1580
|
+
if (this.isEmpty()) {
|
|
1581
|
+
return 0n;
|
|
1582
|
+
}
|
|
1583
|
+
if (invalidate(argument1)) {
|
|
1584
|
+
let summate = this.summate();
|
|
1585
|
+
return summate / this.count();
|
|
1586
|
+
}
|
|
1587
|
+
else if (isFunction(argument1)) {
|
|
1588
|
+
let mapper = argument1;
|
|
1589
|
+
let summate = this.summate(mapper);
|
|
1590
|
+
return summate / this.count();
|
|
1591
|
+
}
|
|
1592
|
+
throw new TypeError("Invalid arguments.");
|
|
1593
|
+
}
|
|
1594
|
+
median(argument1) {
|
|
1595
|
+
if (this.isEmpty()) {
|
|
1596
|
+
return 0n;
|
|
1597
|
+
}
|
|
1598
|
+
if (invalidate(argument1)) {
|
|
1599
|
+
let count = Number(this.count());
|
|
1600
|
+
let middle = Math.floor(count / 2);
|
|
1601
|
+
let median = BigInt(Number(this.ordered[middle].value));
|
|
1602
|
+
if (count % 2 === 0) {
|
|
1603
|
+
median = (median + BigInt(Number(this.ordered[middle - 1].value))) / 2n;
|
|
1604
|
+
return median;
|
|
1605
|
+
}
|
|
1606
|
+
return median;
|
|
1607
|
+
}
|
|
1608
|
+
else if (isFunction(argument1)) {
|
|
1609
|
+
let mapper = argument1;
|
|
1610
|
+
let count = this.count();
|
|
1611
|
+
let middle = count / 2n;
|
|
1612
|
+
let median = mapper(this.ordered[Number(middle)].value);
|
|
1613
|
+
if (count % 2n === 0n) {
|
|
1614
|
+
median = (median + mapper(this.ordered[Number(middle - 1n)].value)) / 2n;
|
|
1615
|
+
}
|
|
1616
|
+
return median;
|
|
1617
|
+
}
|
|
1618
|
+
throw new TypeError("Invalid arguments.");
|
|
1619
|
+
}
|
|
1620
|
+
mode(argument1) {
|
|
1621
|
+
if (this.isEmpty()) {
|
|
1622
|
+
return 0n;
|
|
1623
|
+
}
|
|
1624
|
+
if (invalidate(argument1)) {
|
|
1625
|
+
let frequency = this.frequency();
|
|
1626
|
+
let mode = 0n;
|
|
1627
|
+
let maxFrequency = 0n;
|
|
1628
|
+
for (let [value, freq] of frequency) {
|
|
1629
|
+
if (freq > maxFrequency) {
|
|
1630
|
+
mode = value;
|
|
1631
|
+
maxFrequency = freq;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
return mode;
|
|
1635
|
+
}
|
|
1636
|
+
else if (isFunction(argument1)) {
|
|
1637
|
+
let mapper = argument1;
|
|
1638
|
+
let frequency = this.frequency(mapper);
|
|
1639
|
+
let mode = 0n;
|
|
1640
|
+
let maxFrequency = 0n;
|
|
1641
|
+
for (let [value, frequence] of frequency) {
|
|
1642
|
+
if (frequence > maxFrequency) {
|
|
1643
|
+
mode = value;
|
|
1644
|
+
maxFrequency = frequence;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
return mode;
|
|
1648
|
+
}
|
|
1649
|
+
throw new TypeError("Invalid arguments.");
|
|
1650
|
+
}
|
|
1651
|
+
frequency(argument1) {
|
|
1652
|
+
if (this.isEmpty()) {
|
|
1653
|
+
return new Map();
|
|
1654
|
+
}
|
|
1655
|
+
if (invalidate(argument1)) {
|
|
1656
|
+
let frequency = new Map();
|
|
1657
|
+
for (let i = 0; i < this.ordered.length; i++) {
|
|
1658
|
+
let current = BigInt(Number(this.ordered[i].value));
|
|
1659
|
+
let count = frequency.get(current) || 0n;
|
|
1660
|
+
frequency.set(current, count + 1n);
|
|
1661
|
+
}
|
|
1662
|
+
return frequency;
|
|
1663
|
+
}
|
|
1664
|
+
else if (isFunction(argument1)) {
|
|
1665
|
+
let mapper = argument1;
|
|
1666
|
+
let frequency = new Map();
|
|
1667
|
+
for (let i = 0; i < this.ordered.length; i++) {
|
|
1668
|
+
let current = mapper(this.ordered[i].value);
|
|
1669
|
+
let count = frequency.get(current) || 0n;
|
|
1670
|
+
frequency.set(current, count + 1n);
|
|
1671
|
+
}
|
|
1672
|
+
return frequency;
|
|
1673
|
+
}
|
|
1674
|
+
throw new TypeError("Invalid arguments.");
|
|
1675
|
+
}
|
|
1676
|
+
summate(argument1) {
|
|
1677
|
+
if (this.isEmpty()) {
|
|
1678
|
+
return 0n;
|
|
1679
|
+
}
|
|
1680
|
+
if (invalidate(argument1)) {
|
|
1681
|
+
let summate = 0n;
|
|
1682
|
+
for (let i = 0; i < this.ordered.length; i++) {
|
|
1683
|
+
let current = BigInt(Number(this.ordered[i].value));
|
|
1684
|
+
summate += current;
|
|
1685
|
+
}
|
|
1686
|
+
return summate;
|
|
1687
|
+
}
|
|
1688
|
+
else if (isFunction(argument1)) {
|
|
1689
|
+
let mapper = argument1;
|
|
1690
|
+
let summate = 0n;
|
|
1691
|
+
for (let i = 0; i < this.ordered.length; i++) {
|
|
1692
|
+
let current = mapper(this.ordered[i].value);
|
|
1693
|
+
summate += current;
|
|
1694
|
+
}
|
|
1695
|
+
return summate;
|
|
1696
|
+
}
|
|
1697
|
+
throw new TypeError("Invalid arguments.");
|
|
1698
|
+
}
|
|
1699
|
+
quantile(quantile, mapper) {
|
|
1700
|
+
if (this.isEmpty()) {
|
|
1701
|
+
return 0n;
|
|
1702
|
+
}
|
|
1703
|
+
if (typeof quantile !== "number" || quantile < 0 || quantile > 1) {
|
|
1704
|
+
throw new RangeError("Invalid quantile.");
|
|
1705
|
+
}
|
|
1706
|
+
if (invalidate(mapper)) {
|
|
1707
|
+
let count = Number(this.count());
|
|
1708
|
+
let index = Math.floor(quantile * count);
|
|
1709
|
+
if (index === count) {
|
|
1710
|
+
index--;
|
|
1711
|
+
}
|
|
1712
|
+
let value = BigInt(Number(this.ordered[index].value));
|
|
1713
|
+
return value;
|
|
1714
|
+
}
|
|
1715
|
+
else if (isFunction(mapper)) {
|
|
1716
|
+
let count = Number(this.count());
|
|
1717
|
+
let index = Math.floor(quantile * count);
|
|
1718
|
+
if (index === count) {
|
|
1719
|
+
index--;
|
|
1720
|
+
}
|
|
1721
|
+
let value = mapper(this.ordered[index].value);
|
|
1722
|
+
return value;
|
|
1723
|
+
}
|
|
1724
|
+
throw new TypeError("Invalid arguments.");
|
|
1725
|
+
}
|
|
1726
|
+
interquartileRange(argument1) {
|
|
1727
|
+
if (this.isEmpty()) {
|
|
1728
|
+
return 0n;
|
|
1729
|
+
}
|
|
1730
|
+
if (invalidate(argument1)) {
|
|
1731
|
+
let lower = this.quantile(0.25);
|
|
1732
|
+
let upper = this.quantile(0.75);
|
|
1733
|
+
return upper - lower;
|
|
1734
|
+
}
|
|
1735
|
+
else if (isFunction(argument1)) {
|
|
1736
|
+
let mapper = argument1;
|
|
1737
|
+
let lower = this.quantile(0.25, mapper);
|
|
1738
|
+
let upper = this.quantile(0.75, mapper);
|
|
1739
|
+
return upper - lower;
|
|
1740
|
+
}
|
|
1741
|
+
throw new TypeError("Invalid arguments.");
|
|
1742
|
+
}
|
|
1743
|
+
skewness(argument1) {
|
|
1744
|
+
if (this.isEmpty()) {
|
|
1745
|
+
return 0n;
|
|
1746
|
+
}
|
|
1747
|
+
if (invalidate(argument1)) {
|
|
1748
|
+
let mean = this.mean();
|
|
1749
|
+
let standardDeviation = this.standardDeviation();
|
|
1750
|
+
if (standardDeviation === 0n) {
|
|
1751
|
+
return 0n;
|
|
1752
|
+
}
|
|
1753
|
+
let data = this.toArray();
|
|
1754
|
+
let summate = 0n;
|
|
1755
|
+
for (let value of data) {
|
|
1756
|
+
let z = BigInt(Number(value)) - mean;
|
|
1757
|
+
summate += z * z * z;
|
|
1758
|
+
}
|
|
1759
|
+
return summate / BigInt(data.length);
|
|
1760
|
+
}
|
|
1761
|
+
else if (isFunction(argument1)) {
|
|
1762
|
+
let mapper = argument1;
|
|
1763
|
+
let mean = this.mean(mapper);
|
|
1764
|
+
let standardDeviation = this.standardDeviation(mapper);
|
|
1765
|
+
if (standardDeviation === 0n) {
|
|
1766
|
+
return 0n;
|
|
1767
|
+
}
|
|
1768
|
+
let data = this.toArray().map(item => mapper(item));
|
|
1769
|
+
let summate = 0n;
|
|
1770
|
+
for (let value of data) {
|
|
1771
|
+
let z = value - mean;
|
|
1772
|
+
summate += z * z * z;
|
|
1773
|
+
}
|
|
1774
|
+
return summate / BigInt(data.length);
|
|
1775
|
+
}
|
|
1776
|
+
throw new TypeError("Invalid arguments.");
|
|
1777
|
+
}
|
|
1778
|
+
kurtosis(argument1) {
|
|
1779
|
+
if (this.isEmpty()) {
|
|
1780
|
+
return 0n;
|
|
1781
|
+
}
|
|
1782
|
+
if (invalidate(argument1)) {
|
|
1783
|
+
let mean = this.mean();
|
|
1784
|
+
let standardDeviation = this.standardDeviation();
|
|
1785
|
+
if (standardDeviation === 0n) {
|
|
1786
|
+
return 0n;
|
|
1787
|
+
}
|
|
1788
|
+
let data = this.toArray();
|
|
1789
|
+
let summate = 0n;
|
|
1790
|
+
let count = data.length;
|
|
1791
|
+
for (let value of data) {
|
|
1792
|
+
let z = BigInt(Number(value)) - mean;
|
|
1793
|
+
summate += z * z * z * z;
|
|
1794
|
+
}
|
|
1795
|
+
return summate / BigInt(count * count) - 3n;
|
|
1796
|
+
}
|
|
1797
|
+
else if (isFunction(argument1)) {
|
|
1798
|
+
let mapper = argument1;
|
|
1799
|
+
let mean = this.mean(mapper);
|
|
1800
|
+
let standardDeviation = this.standardDeviation(mapper);
|
|
1801
|
+
if (standardDeviation === 0n) {
|
|
1802
|
+
return 0n;
|
|
1803
|
+
}
|
|
1804
|
+
let data = this.toArray().map(item => mapper(item));
|
|
1805
|
+
let summate = 0n;
|
|
1806
|
+
let count = data.length;
|
|
1807
|
+
for (let value of data) {
|
|
1808
|
+
let z = value - mean;
|
|
1809
|
+
summate += z * z * z * z;
|
|
1810
|
+
}
|
|
1811
|
+
return summate / BigInt(count * count) - 3n;
|
|
1812
|
+
}
|
|
1813
|
+
throw new TypeError("Invalid arguments.");
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
;
|