semantic-typescript 0.0.8 → 0.2.5

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.
@@ -0,0 +1,84 @@
1
+ import { Collector } from "./collector";
2
+ import { Optional } from "./optional";
3
+ import { Semantic } from "./semantic";
4
+ import type { BiConsumer, BiFunctional, Comparator, Consumer, Functional, Predicate, Supplier, TriFunctional, Generator, BiPredicate, TriPredicate } from "./utility";
5
+ export declare abstract class Collectable<E> {
6
+ protected readonly Collectable: symbol;
7
+ constructor();
8
+ anyMatch(predicate: Predicate<E>): boolean;
9
+ allMatch(predicate: Predicate<E>): boolean;
10
+ collect<A, R>(collector: Collector<E, A, R>): R;
11
+ collect<A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R;
12
+ collect<A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R;
13
+ collect<A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R;
14
+ collect<A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R;
15
+ collect<A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R;
16
+ collect<A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R;
17
+ collect<A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R;
18
+ collect<A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R;
19
+ count(): bigint;
20
+ isEmpty(): boolean;
21
+ findAny(): Optional<E>;
22
+ findFirst(): Optional<E>;
23
+ findLast(): Optional<E>;
24
+ forEach(action: Consumer<E>): void;
25
+ forEach(action: BiConsumer<E, bigint>): void;
26
+ group<K>(classifier: Functional<E, K>): Map<K, Array<E>>;
27
+ groupBy<K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Map<K, Array<V>>;
28
+ join(): string;
29
+ join(delimiter: string): string;
30
+ join(prefix: string, delimiter: string, suffix: string): string;
31
+ join(prefiex: string, accumulator: BiFunctional<string, E, string>, suffix: string): string;
32
+ join(prefiex: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): string;
33
+ log(): void;
34
+ log(accumulator: BiFunctional<string, E, string>): void;
35
+ log(accumulator: BiFunctional<string, E, string> | TriFunctional<string, E, bigint, string>): void;
36
+ log(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): void;
37
+ log(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): void;
38
+ nonMatch(predicate: Predicate<E>): boolean;
39
+ partition(count: bigint): Array<Array<E>>;
40
+ partitionBy(classifier: Functional<E, bigint>): Array<Array<E>>;
41
+ reduce(accumulator: BiFunctional<E, E, E>): Optional<E>;
42
+ reduce(accumulator: TriFunctional<E, E, bigint, E>): Optional<E>;
43
+ reduce(identity: E, accumulator: BiFunctional<E, E, E>): E;
44
+ reduce(identity: E, accumulator: TriFunctional<E, E, bigint, E>): E;
45
+ reduce<R>(identity: R, accumulator: BiFunctional<R, E, R>, finisher: BiFunctional<R, R, R>): R;
46
+ reduce<R>(identity: R, accumulator: TriFunctional<R, E, bigint, R>, finisher: BiFunctional<R, R, R>): R;
47
+ semantic(): Semantic<E>;
48
+ protected abstract source(): Generator<E> | Iterable<E>;
49
+ toArray(): Array<E>;
50
+ toMap<K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Map<K, V>;
51
+ toSet(): Set<E>;
52
+ write(stream: WritableStream<string>): Promise<WritableStream<string>>;
53
+ write(stream: WritableStream<string>, accumulator: BiFunctional<E, bigint, string>): Promise<WritableStream<string>>;
54
+ write(stream: WritableStream<Uint8Array>, accumulator: BiFunctional<E, bigint, Uint8Array>): Promise<WritableStream<Uint8Array>>;
55
+ }
56
+ export declare class UnorderedCollectable<E> extends Collectable<E> {
57
+ protected readonly UnorderedCollectable: symbol;
58
+ protected generator: Generator<E>;
59
+ constructor(generator: Generator<E>);
60
+ source(): Generator<E>;
61
+ }
62
+ type Indexed<K, V> = {
63
+ index: K;
64
+ value: V;
65
+ };
66
+ export declare class OrderedCollectable<E> extends Collectable<E> {
67
+ protected readonly OrderedCollectable: symbol;
68
+ protected ordered: Array<Indexed<bigint, E>>;
69
+ constructor(iterable: Iterable<E>);
70
+ constructor(iterable: Iterable<E>, comparator: Comparator<E>);
71
+ constructor(generator: Generator<E>);
72
+ constructor(generator: Generator<E>, comparator: Comparator<E>);
73
+ source(): Generator<E> | Iterable<E>;
74
+ }
75
+ export declare class WindowCollectable<E> extends OrderedCollectable<E> {
76
+ protected readonly WindowCollectable: symbol;
77
+ constructor(iterable: Iterable<E>);
78
+ constructor(iterable: Iterable<E>, comparator: Comparator<E>);
79
+ constructor(generator: Generator<E>);
80
+ constructor(generator: Generator<E>, comparator: Comparator<E>);
81
+ slide(size: bigint, step?: bigint): Semantic<Semantic<E>>;
82
+ tumble(size: bigint): Semantic<Semantic<E>>;
83
+ }
84
+ export {};
@@ -0,0 +1,476 @@
1
+ import { Collector } from "./collector";
2
+ import { from } from "./factory";
3
+ import { isCollector, isFunction, isIterable, isObject, isString } from "./guard";
4
+ import { useCompare } from "./hook";
5
+ import { Optional } from "./optional";
6
+ import { Semantic } from "./semantic";
7
+ import { CollectableSymbol, OrderedCollectableSymbol, UnorderedCollectableSymbol, WindowCollectableSymbol } from "./symbol";
8
+ import { invalidate, validate } from "./utility";
9
+ export class Collectable {
10
+ Collectable = CollectableSymbol;
11
+ constructor() {
12
+ }
13
+ anyMatch(predicate) {
14
+ return this.collect(() => {
15
+ return false;
16
+ }, (element) => {
17
+ return predicate(element);
18
+ }, (result, element) => {
19
+ return result || predicate(element);
20
+ }, (result) => {
21
+ return result;
22
+ });
23
+ }
24
+ allMatch(predicate) {
25
+ return this.collect(() => {
26
+ return true;
27
+ }, (element) => {
28
+ return !predicate(element);
29
+ }, (result, element) => {
30
+ return result && predicate(element);
31
+ }, (result) => {
32
+ return result;
33
+ });
34
+ }
35
+ collect(argument1, argument2, argument3, argument4) {
36
+ let source = this.source();
37
+ if (isCollector(argument1)) {
38
+ let collector = argument1;
39
+ return collector.collect(source);
40
+ }
41
+ if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3)) {
42
+ let identity = argument1;
43
+ let accumulator = argument2;
44
+ let finisher = argument3;
45
+ let collector = Collector.full(identity, accumulator, finisher);
46
+ return collector.collect(source);
47
+ }
48
+ if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3) && isFunction(argument4)) {
49
+ let identity = argument1;
50
+ let interrupt = argument2;
51
+ let accumulator = argument3;
52
+ let finisher = argument4;
53
+ let collector = Collector.shortable(identity, interrupt, accumulator, finisher);
54
+ return collector.collect(source);
55
+ }
56
+ throw new TypeError("Invalid arguments.");
57
+ }
58
+ count() {
59
+ return this.collect(() => {
60
+ return 0n;
61
+ }, (count) => {
62
+ return count + 1n;
63
+ }, (count) => {
64
+ return count;
65
+ });
66
+ }
67
+ isEmpty() {
68
+ return this.count() === 0n;
69
+ }
70
+ findAny() {
71
+ return this.collect(() => {
72
+ return Optional.ofNullable();
73
+ }, () => {
74
+ return true;
75
+ }, (result, element) => {
76
+ return result.isPresent() && Math.random() > 0.5 ? result : Optional.of(element);
77
+ }, (result) => {
78
+ return result;
79
+ });
80
+ }
81
+ findFirst() {
82
+ return this.collect(() => {
83
+ return Optional.ofNullable();
84
+ }, () => {
85
+ return true;
86
+ }, (result, element) => {
87
+ return result.isPresent() ? result : Optional.of(element);
88
+ }, (result) => {
89
+ return result;
90
+ });
91
+ }
92
+ findLast() {
93
+ return this.collect(() => {
94
+ return Optional.ofNullable();
95
+ }, () => {
96
+ return true;
97
+ }, (result, element) => {
98
+ return result.isPresent() ? result : Optional.of(element);
99
+ }, (result) => {
100
+ return result;
101
+ });
102
+ }
103
+ forEach(action) {
104
+ if (isFunction(action)) {
105
+ this.collect(() => {
106
+ return 0n;
107
+ }, (count, element) => {
108
+ action(element, count);
109
+ return count + 1n;
110
+ }, (count) => {
111
+ return count;
112
+ });
113
+ }
114
+ }
115
+ group(classifier) {
116
+ if (isFunction(classifier)) {
117
+ return this.collect(() => {
118
+ return new Map();
119
+ }, (map, element) => {
120
+ let key = classifier(element);
121
+ let raw = map.get(key);
122
+ let array = validate(raw) ? raw : [];
123
+ array.push(element);
124
+ map.set(key, array);
125
+ return map;
126
+ }, (map) => {
127
+ return map;
128
+ });
129
+ }
130
+ throw new TypeError("Invalid arguments.");
131
+ }
132
+ groupBy(keyExtractor, valueExtractor) {
133
+ return this.collect(() => {
134
+ return new Map();
135
+ }, (map, element) => {
136
+ let key = keyExtractor(element);
137
+ let value = valueExtractor(element);
138
+ let group = (validate(map.get(key)) ? map.get(key) : []);
139
+ group.push(value);
140
+ map.set(key, group);
141
+ return map;
142
+ }, (map) => {
143
+ return map;
144
+ });
145
+ }
146
+ join(argument1, argument2, argument3) {
147
+ if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
148
+ return this.collect(() => {
149
+ return "[";
150
+ }, (text, element) => {
151
+ return text + element + ",";
152
+ }, (text) => {
153
+ return text.substring(0, text.length - 1) + "]";
154
+ });
155
+ }
156
+ if (isString(argument1) && invalidate(argument2) && invalidate(argument3)) {
157
+ let delimiter = argument1;
158
+ return this.collect(() => {
159
+ return "[";
160
+ }, (text, element) => {
161
+ return text + element + delimiter;
162
+ }, (text) => {
163
+ return text.substring(0, text.length - 1) + "]";
164
+ });
165
+ }
166
+ if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
167
+ let prefix = argument1;
168
+ let accumulator = argument2;
169
+ let suffix = argument3;
170
+ return this.collect(() => {
171
+ return prefix;
172
+ }, (text, element, index) => {
173
+ return text + accumulator(text, element, index);
174
+ }, (text) => {
175
+ return text + suffix;
176
+ });
177
+ }
178
+ if (isString(argument1) && isString(argument2) && isString(argument3)) {
179
+ let prefix = argument1;
180
+ let delimiter = argument2;
181
+ let suffix = argument3;
182
+ return this.collect(() => {
183
+ return prefix;
184
+ }, (text, element) => {
185
+ return text + element + delimiter;
186
+ }, (text) => {
187
+ return text + suffix;
188
+ });
189
+ }
190
+ throw new TypeError("Invalid arguments.");
191
+ }
192
+ log(argument1, argument2, argument3) {
193
+ if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
194
+ let text = this.join();
195
+ console.log(text);
196
+ }
197
+ else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
198
+ let accumulator = argument1;
199
+ let text = this.join("[", accumulator, "]");
200
+ console.log(text);
201
+ }
202
+ else {
203
+ throw new TypeError("Invalid arguments.");
204
+ }
205
+ }
206
+ nonMatch(predicate) {
207
+ return this.collect(() => {
208
+ return true;
209
+ }, (element) => {
210
+ return predicate(element);
211
+ }, (result, element) => {
212
+ return result || predicate(element);
213
+ }, (result) => {
214
+ return result;
215
+ });
216
+ }
217
+ partition(count) {
218
+ let limited = count > 1n ? count : 1n;
219
+ return this.collect(() => {
220
+ return [];
221
+ }, (array, element) => {
222
+ let index = limited % BigInt(array.length);
223
+ if (index === 0n) {
224
+ array.push([]);
225
+ }
226
+ array[Number(index)].push(element);
227
+ return array;
228
+ }, (result) => {
229
+ return result;
230
+ });
231
+ }
232
+ partitionBy(classifier) {
233
+ return this.collect(() => {
234
+ return [];
235
+ }, (array, element) => {
236
+ let index = classifier(element);
237
+ while (index > BigInt(array.length) - 1n) {
238
+ array.push([]);
239
+ }
240
+ array[Number(index)].push(element);
241
+ return array;
242
+ }, (result) => {
243
+ return result;
244
+ });
245
+ }
246
+ reduce(argument1, argument2, argument3) {
247
+ if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
248
+ let accumulator = argument1;
249
+ return this.collect(() => Optional.ofNullable(), (result, element, index) => {
250
+ if (result.isEmpty()) {
251
+ return Optional.of(element);
252
+ }
253
+ else {
254
+ let current = result.get();
255
+ return Optional.of(accumulator(current, element, index));
256
+ }
257
+ }, (result) => result);
258
+ }
259
+ else if (validate(argument1) && isFunction(argument2) && invalidate(argument3)) {
260
+ let identity = argument1;
261
+ let accumulator = argument2;
262
+ return this.collect(() => identity, (result, element, index) => {
263
+ return accumulator(result, element, index);
264
+ }, (result) => result);
265
+ }
266
+ else if (validate(argument1) && isFunction(argument2) && isFunction(argument3)) {
267
+ let identity = argument1;
268
+ let accumulator = argument2;
269
+ let finisher = argument3;
270
+ return this.collect(() => identity, (result, element, index) => {
271
+ return accumulator(result, element, index);
272
+ }, (result) => finisher(result, result));
273
+ }
274
+ else {
275
+ throw new TypeError("Invalid arguments.");
276
+ }
277
+ }
278
+ semantic() {
279
+ let source = this.source();
280
+ if (isIterable(source)) {
281
+ return from(source);
282
+ }
283
+ else if (isFunction(source)) {
284
+ return new Semantic(source);
285
+ }
286
+ else {
287
+ throw new TypeError("Invalid source.");
288
+ }
289
+ }
290
+ toArray() {
291
+ return this.collect(() => {
292
+ return [];
293
+ }, (array, element) => {
294
+ array.push(element);
295
+ return array;
296
+ }, (result) => {
297
+ return result;
298
+ });
299
+ }
300
+ toMap(keyExtractor, valueExtractor) {
301
+ return this.collect(() => {
302
+ return new Map();
303
+ }, (map, element) => {
304
+ let key = keyExtractor(element);
305
+ let value = valueExtractor(element);
306
+ map.set(key, value);
307
+ return map;
308
+ }, (map) => {
309
+ return map;
310
+ });
311
+ }
312
+ toSet() {
313
+ return this.collect(() => {
314
+ return new Set();
315
+ }, (set, element) => {
316
+ set.add(element);
317
+ return set;
318
+ }, (result) => {
319
+ return result;
320
+ });
321
+ }
322
+ write(stream, accumulator) {
323
+ if (isObject(stream) && invalidate(accumulator)) {
324
+ let optional = this.collect(() => {
325
+ return Optional.ofNonNull(stream);
326
+ }, (result, element) => {
327
+ try {
328
+ return result.map((stream) => {
329
+ let writer = stream.getWriter();
330
+ writer.write(String(element));
331
+ return stream;
332
+ });
333
+ }
334
+ catch (reason) {
335
+ return Optional.empty();
336
+ }
337
+ }, (a) => {
338
+ return a;
339
+ });
340
+ return new Promise((resolve, reject) => {
341
+ optional.ifPresent(resolve, reject);
342
+ });
343
+ }
344
+ else if (isObject(stream) && isFunction(accumulator)) {
345
+ let optional = this.collect(() => {
346
+ return Optional.ofNonNull(stream);
347
+ }, (result, element, index) => {
348
+ try {
349
+ return result.map((stream) => {
350
+ let writer = stream.getWriter();
351
+ writer.write(accumulator(element, index));
352
+ return stream;
353
+ });
354
+ }
355
+ catch (reason) {
356
+ return Optional.empty();
357
+ }
358
+ }, (a) => {
359
+ return a;
360
+ });
361
+ return new Promise((resolve, reject) => {
362
+ optional.ifPresent(resolve, reject);
363
+ });
364
+ }
365
+ else {
366
+ throw new TypeError("Invalid arguments.");
367
+ }
368
+ }
369
+ }
370
+ ;
371
+ export class UnorderedCollectable extends Collectable {
372
+ UnorderedCollectable = UnorderedCollectableSymbol;
373
+ generator;
374
+ constructor(generator) {
375
+ super();
376
+ this.generator = generator;
377
+ }
378
+ source() {
379
+ return this.generator;
380
+ }
381
+ }
382
+ ;
383
+ export class OrderedCollectable extends Collectable {
384
+ OrderedCollectable = OrderedCollectableSymbol;
385
+ ordered = [];
386
+ constructor(argument1, argument2) {
387
+ super();
388
+ let buffer = [];
389
+ if (isIterable(argument1)) {
390
+ let iterable = argument1;
391
+ let index = 0n;
392
+ for (let element of iterable) {
393
+ buffer.push({
394
+ index: index,
395
+ value: element
396
+ });
397
+ index++;
398
+ }
399
+ }
400
+ else if (isFunction(argument1)) {
401
+ let generator = argument1;
402
+ generator((element, index) => {
403
+ buffer.push({
404
+ index: index,
405
+ value: element
406
+ });
407
+ }, () => false);
408
+ }
409
+ else {
410
+ throw new TypeError("Invalid arguments.");
411
+ }
412
+ buffer.map((indexed, _index, array) => {
413
+ let length = BigInt(array.length);
414
+ return {
415
+ index: ((indexed.index % length) + length) % length,
416
+ value: indexed.value
417
+ };
418
+ }).sort((a, b) => {
419
+ if (isFunction(argument2)) {
420
+ let comparator = argument2;
421
+ return comparator(a.value, b.value);
422
+ }
423
+ else {
424
+ return useCompare(a.index, b.index);
425
+ }
426
+ }).forEach((indexed) => {
427
+ this.ordered.push(indexed);
428
+ });
429
+ }
430
+ source() {
431
+ return this.ordered.map((indexed) => indexed.value);
432
+ }
433
+ }
434
+ ;
435
+ export class WindowCollectable extends OrderedCollectable {
436
+ WindowCollectable = WindowCollectableSymbol;
437
+ constructor(parameter, comparator) {
438
+ if (isIterable(parameter)) {
439
+ if (isFunction(comparator)) {
440
+ super(parameter, comparator);
441
+ }
442
+ else {
443
+ super(parameter);
444
+ }
445
+ }
446
+ else if (isFunction(parameter)) {
447
+ if (isFunction(comparator)) {
448
+ super(parameter, comparator);
449
+ }
450
+ else {
451
+ super(parameter);
452
+ }
453
+ }
454
+ }
455
+ slide(size, step = 1n) {
456
+ if (size > 0n && step > 0n) {
457
+ let source = this.toArray();
458
+ let windows = [];
459
+ let windowStartIndex = 0n;
460
+ while (windowStartIndex < BigInt(source.length)) {
461
+ let windowEnd = windowStartIndex + size;
462
+ let window = source.slice(Number(windowStartIndex), Number(windowEnd));
463
+ if (window.length > 0) {
464
+ windows.push(window);
465
+ }
466
+ windowStartIndex += step;
467
+ }
468
+ return from(windows).map((window) => from(window));
469
+ }
470
+ throw new RangeError("Invalid arguments.");
471
+ }
472
+ tumble(size) {
473
+ return this.slide(size, size);
474
+ }
475
+ }
476
+ ;
@@ -0,0 +1,24 @@
1
+ import type { Semantic } from "./semantic";
2
+ import type { BiFunctional, BiPredicate, Functional, Predicate, Supplier, TriFunctional, Generator, TriPredicate } from "./utility";
3
+ export declare class Collector<E, A, R> {
4
+ protected identity: Supplier<A>;
5
+ protected interrupt: Predicate<E> | BiPredicate<E, bigint> | TriPredicate<E, bigint, A>;
6
+ protected accumulator: BiFunctional<A, E, A> | TriFunctional<A, E, bigint, A>;
7
+ protected finisher: Functional<A, R>;
8
+ protected readonly Collector: symbol;
9
+ protected constructor(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>);
10
+ protected constructor(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>);
11
+ protected constructor(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>);
12
+ protected constructor(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>);
13
+ collect(generator: Generator<E>): R;
14
+ collect(iterable: Iterable<E>): R;
15
+ collect(semantic: Semantic<E>): R;
16
+ static full<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
17
+ static full<E, A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
18
+ static shortable<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
19
+ static shortable<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
20
+ static shortable<E, A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
21
+ static shortable<E, A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
22
+ static shortable<E, A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
23
+ static shortable<E, A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
24
+ }
@@ -0,0 +1,63 @@
1
+ import { isFunction, isIterable, isSemantic } from "./guard";
2
+ import { CollectableSymbol } from "./symbol";
3
+ export class Collector {
4
+ identity;
5
+ interrupt;
6
+ accumulator;
7
+ finisher;
8
+ Collector = CollectableSymbol;
9
+ constructor(identity, interruptor, accumulator, finisher) {
10
+ if (isFunction(identity) && isFunction(interruptor) && isFunction(accumulator) && isFunction(finisher)) {
11
+ this.identity = identity;
12
+ this.interrupt = interruptor;
13
+ this.accumulator = accumulator;
14
+ this.finisher = finisher;
15
+ }
16
+ else {
17
+ throw new TypeError("Invalid arguments");
18
+ }
19
+ }
20
+ collect(parameter) {
21
+ let accumulator = this.identity();
22
+ let count = 0n;
23
+ if (isFunction(parameter)) {
24
+ parameter((element, index) => {
25
+ accumulator = this.accumulator(accumulator, element, index);
26
+ count++;
27
+ }, (element, index) => this.interrupt(element, index, accumulator));
28
+ }
29
+ else if (isIterable(parameter)) {
30
+ let iterable = parameter;
31
+ let index = 0n;
32
+ for (let element of iterable) {
33
+ if (this.interrupt(element, index, accumulator)) {
34
+ break;
35
+ }
36
+ accumulator = this.accumulator(accumulator, element, count);
37
+ count++;
38
+ index++;
39
+ }
40
+ }
41
+ else if (isSemantic(parameter)) {
42
+ let semantic = parameter;
43
+ let generator = Reflect.get(semantic, "generator");
44
+ if (isFunction(generator)) {
45
+ generator((element, index) => {
46
+ accumulator = this.accumulator(accumulator, element, index);
47
+ count++;
48
+ }, (element, index) => this.interrupt(element, index, accumulator));
49
+ }
50
+ else {
51
+ throw new TypeError("Invalid arguments");
52
+ }
53
+ }
54
+ return this.finisher(accumulator);
55
+ }
56
+ static full(identity, accumulator, finisher) {
57
+ return new Collector(identity, () => false, accumulator, finisher);
58
+ }
59
+ static shortable(identity, interruptor, accumulator, finisher) {
60
+ return new Collector(identity, interruptor, accumulator, finisher);
61
+ }
62
+ }
63
+ ;
@@ -0,0 +1,11 @@
1
+ import { Semantic } from "./semantic";
2
+ import type { BiFunctional, BiPredicate, Functional, Predicate, Supplier, TriFunctional, Generator } from "./utility";
3
+ export declare let blob: Functional<Blob, Semantic<Uint8Array>> & BiFunctional<Blob, bigint, Semantic<Uint8Array>>;
4
+ export declare let empty: <E>() => Semantic<E>;
5
+ export declare let fill: (<E>(element: E, count: bigint) => Semantic<E>) & (<E>(supplier: Supplier<E>, count: bigint) => Semantic<E>);
6
+ export declare let from: <E>(iterable: Iterable<E>) => Semantic<E>;
7
+ export declare let generate: (<E>(supplier: Supplier<E>, interrupt: Predicate<E>) => Semantic<E>) & (<E>(supplier: Supplier<E>, interrupt: BiPredicate<E, bigint>) => Semantic<E>);
8
+ export declare let interval: Functional<number, Semantic<number>> & BiFunctional<number, number, Semantic<number>>;
9
+ export declare let iterate: <E>(generator: Generator<E>) => Semantic<E>;
10
+ export declare let range: BiFunctional<number, number, Semantic<number>> & TriFunctional<number, number, number, Semantic<number>>;
11
+ export declare let websocket: Functional<WebSocket, Semantic<MessageEvent | CloseEvent | Event>>;