@mark1russell7/collections 0.0.0-bootstrap.0 → 0.1.0
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/LICENSE +21 -0
- package/README.md +77 -1
- package/build/dag/index.d.ts +144 -0
- package/build/dag/index.d.ts.map +1 -0
- package/build/dag/index.js +294 -0
- package/build/dag/index.js.map +1 -0
- package/build/graph/index.d.ts +20 -0
- package/build/graph/index.d.ts.map +1 -0
- package/build/graph/index.js +48 -0
- package/build/graph/index.js.map +1 -0
- package/build/index.d.ts +4952 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7811 -0
- package/build/index.js.map +1 -0
- package/build/testing/index.d.ts +25 -0
- package/build/testing/index.d.ts.map +1 -0
- package/build/testing/index.js +72 -0
- package/build/testing/index.js.map +1 -0
- package/package.json +71 -4
- package/src/async/async-queue.ts +481 -0
- package/src/async/channels.ts +450 -0
- package/src/async/settled.ts +41 -0
- package/src/behaviors/bounded.ts +485 -0
- package/src/behaviors/composite.ts +99 -0
- package/src/behaviors/evented.ts +464 -0
- package/src/behaviors/lru.ts +449 -0
- package/src/behaviors/readonly.ts +236 -0
- package/src/behaviors/safe.ts +358 -0
- package/src/behaviors/synchronized.ts +316 -0
- package/src/behaviors/ttl.ts +464 -0
- package/src/core/effects.ts +327 -0
- package/src/core/events.ts +363 -0
- package/src/core/middleware.ts +413 -0
- package/src/core/policies.ts +316 -0
- package/src/core/traits.ts +164 -0
- package/src/dag/builder.ts +124 -0
- package/src/dag/executor.ts +190 -0
- package/src/dag/index.ts +14 -0
- package/src/dag/traversal.ts +129 -0
- package/src/dag/types.ts +87 -0
- package/src/fx/collectors.ts +500 -0
- package/src/fx/iter.ts +536 -0
- package/src/graph/hub-order.ts +53 -0
- package/src/graph/index.ts +5 -0
- package/src/impl/array-deque.ts +463 -0
- package/src/impl/array-list.ts +490 -0
- package/src/impl/hash-map.ts +623 -0
- package/src/impl/hash-set.ts +342 -0
- package/src/impl/linked-hash-map.ts +572 -0
- package/src/impl/linked-list.ts +610 -0
- package/src/impl/priority-queue.ts +448 -0
- package/src/impl/summary-tree.ts +594 -0
- package/src/impl/tree-map.ts +913 -0
- package/src/impl/tree-set.ts +469 -0
- package/src/index.ts +237 -0
- package/src/interfaces/collection.ts +141 -0
- package/src/interfaces/list.ts +281 -0
- package/src/interfaces/map.ts +405 -0
- package/src/interfaces/queue.ts +353 -0
- package/src/interfaces/set.ts +158 -0
- package/src/order/index.ts +6 -0
- package/src/order/rank.ts +271 -0
- package/src/storage/listeners.ts +72 -0
- package/src/testing/check.ts +81 -0
- package/src/testing/index.ts +7 -0
- package/src/utils/defaults.ts +345 -0
- package/src/utils/factories.ts +357 -0
- package/src/utils/helpers.ts +248 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collectors for Stream-style collection operations.
|
|
3
|
+
*
|
|
4
|
+
* This module gives Java Stream API-style collectors for transforming iterables
|
|
5
|
+
* into various collection types. The model is java.util.stream.Collectors.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* import { collect, toList, groupingBy } from './collectors'
|
|
9
|
+
*
|
|
10
|
+
* const grouped = collect(users, groupingBy(u => u.age))
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { arrayList, type ArrayList } from "../impl/array-list.js";
|
|
14
|
+
import { hashMap, type HashMap } from "../impl/hash-map.js";
|
|
15
|
+
import { hashSet, type HashSet } from "../impl/hash-set.js";
|
|
16
|
+
|
|
17
|
+
// ============================================================================
|
|
18
|
+
// Collector interface
|
|
19
|
+
// ============================================================================
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A Collector represents a mutable reduction operation.
|
|
23
|
+
*
|
|
24
|
+
* @template T - The type of input elements
|
|
25
|
+
* @template A - The mutable accumulation type
|
|
26
|
+
* @template R - The final result type
|
|
27
|
+
*/
|
|
28
|
+
export interface Collector<T, A, R> {
|
|
29
|
+
/**
|
|
30
|
+
* This value makes the initial accumulator.
|
|
31
|
+
*/
|
|
32
|
+
supplier: () => A;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* This value incorporates an element into the accumulator and gives the next accumulator. A mutable
|
|
36
|
+
* accumulator (a list, a map) gives itself. An immutable one (a count, a sum) gives the
|
|
37
|
+
* new value. (Before, the result was ignored, so every immutable collector - counting,
|
|
38
|
+
* summing, minBy, maxBy, reducing, first, last - given its seed: BUGS-2026-07 C12.)
|
|
39
|
+
*/
|
|
40
|
+
accumulator: (acc: A, element: T) => A;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* This value combines two accumulators (for parallel streams).
|
|
44
|
+
*/
|
|
45
|
+
combiner?: (acc1: A, acc2: A) => A;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* This value transforms the accumulator into the final result.
|
|
49
|
+
*/
|
|
50
|
+
finisher: (acc: A) => R;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* This function collects elements using a collector.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* const list = collect([1, 2, 3], toList())
|
|
58
|
+
* const set = collect([1, 2, 2, 3], toSet())
|
|
59
|
+
*/
|
|
60
|
+
export function collect<T, A, R>(
|
|
61
|
+
iterable: Iterable<T>,
|
|
62
|
+
collector: Collector<T, A, R>
|
|
63
|
+
): R {
|
|
64
|
+
let acc = collector.supplier();
|
|
65
|
+
for (const element of iterable) {
|
|
66
|
+
acc = collector.accumulator(acc, element);
|
|
67
|
+
}
|
|
68
|
+
return collector.finisher(acc);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ============================================================================
|
|
72
|
+
// Basic collectors
|
|
73
|
+
// ============================================================================
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* This function collects elements into an ArrayList.
|
|
77
|
+
*/
|
|
78
|
+
export function toList<T>(): Collector<T, ArrayList<T>, ArrayList<T>> {
|
|
79
|
+
return {
|
|
80
|
+
supplier: () => arrayList<T>(),
|
|
81
|
+
accumulator: (list, element) => (list.add(element), list),
|
|
82
|
+
finisher: (list) => list,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* This function collects elements into a HashSet.
|
|
88
|
+
*/
|
|
89
|
+
export function toSet<T>(): Collector<T, HashSet<T>, HashSet<T>> {
|
|
90
|
+
return {
|
|
91
|
+
supplier: () => hashSet<T>(),
|
|
92
|
+
accumulator: (set, element) => (set.add(element), set),
|
|
93
|
+
finisher: (set) => set,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* This function collects elements into a native Array.
|
|
99
|
+
*/
|
|
100
|
+
export function toArray<T>(): Collector<T, T[], T[]> {
|
|
101
|
+
return {
|
|
102
|
+
supplier: () => [],
|
|
103
|
+
accumulator: (arr, element) => (arr.push(element), arr),
|
|
104
|
+
finisher: (arr) => arr,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* This function collects elements into a HashMap using key and value extractors.
|
|
110
|
+
*/
|
|
111
|
+
export function toMap<T, K, V>(
|
|
112
|
+
keyExtractor: (element: T) => K,
|
|
113
|
+
valueExtractor: (element: T) => V
|
|
114
|
+
): Collector<T, HashMap<K, V>, HashMap<K, V>> {
|
|
115
|
+
return {
|
|
116
|
+
supplier: () => hashMap<K, V>(),
|
|
117
|
+
accumulator: (map, element) => {
|
|
118
|
+
map.set(keyExtractor(element), valueExtractor(element));
|
|
119
|
+
return map;
|
|
120
|
+
},
|
|
121
|
+
finisher: (map) => map,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ============================================================================
|
|
126
|
+
// Grouping collectors
|
|
127
|
+
// ============================================================================
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* This function groups elements by a classifier function.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* const byAge = collect(users, groupingBy(u => u.age))
|
|
134
|
+
* // Map<number, ArrayList<User>>
|
|
135
|
+
*/
|
|
136
|
+
export function groupingBy<T, K>(
|
|
137
|
+
classifier: (element: T) => K
|
|
138
|
+
): Collector<T, HashMap<K, ArrayList<T>>, HashMap<K, ArrayList<T>>> {
|
|
139
|
+
return {
|
|
140
|
+
supplier: () => hashMap<K, ArrayList<T>>(),
|
|
141
|
+
accumulator: (map, element) => {
|
|
142
|
+
const key = classifier(element);
|
|
143
|
+
const list = map.computeIfAbsent(key, () => arrayList<T>());
|
|
144
|
+
list.add(element);
|
|
145
|
+
return map;
|
|
146
|
+
},
|
|
147
|
+
finisher: (map) => map,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Groups and transforms with a downstream collector.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* const countByAge = collect(
|
|
156
|
+
* users,
|
|
157
|
+
* groupingBy(u => u.age, counting())
|
|
158
|
+
* )
|
|
159
|
+
* // Map<number, number>
|
|
160
|
+
*/
|
|
161
|
+
export function groupingByWith<T, K, A, D>(
|
|
162
|
+
classifier: (element: T) => K,
|
|
163
|
+
downstream: Collector<T, A, D>
|
|
164
|
+
): Collector<T, HashMap<K, A>, HashMap<K, D>> {
|
|
165
|
+
return {
|
|
166
|
+
supplier: () => hashMap<K, A>(),
|
|
167
|
+
accumulator: (map, element) => {
|
|
168
|
+
const key = classifier(element);
|
|
169
|
+
const acc = map.has(key) ? map.get(key) : downstream.supplier();
|
|
170
|
+
map.set(key, downstream.accumulator(acc, element));
|
|
171
|
+
return map;
|
|
172
|
+
},
|
|
173
|
+
finisher: (map) => {
|
|
174
|
+
const result = hashMap<K, D>();
|
|
175
|
+
for (const entry of map.entries()) {
|
|
176
|
+
result.set(entry.key, downstream.finisher(entry.value));
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Partitions elements into two groups based on a predicate.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* const partitioned = collect(
|
|
188
|
+
* numbers,
|
|
189
|
+
* partitioningBy(n => n % 2 === 0)
|
|
190
|
+
* )
|
|
191
|
+
* // Map<boolean, ArrayList<number>>
|
|
192
|
+
*/
|
|
193
|
+
export function partitioningBy<T>(
|
|
194
|
+
predicate: (element: T) => boolean
|
|
195
|
+
): Collector<T, HashMap<boolean, ArrayList<T>>, HashMap<boolean, ArrayList<T>>> {
|
|
196
|
+
return groupingBy((element) => predicate(element));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ============================================================================
|
|
200
|
+
// Aggregation collectors
|
|
201
|
+
// ============================================================================
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* This function counts the number of elements.
|
|
205
|
+
*/
|
|
206
|
+
export function counting<T>(): Collector<T, number, number> {
|
|
207
|
+
return {
|
|
208
|
+
supplier: () => 0,
|
|
209
|
+
accumulator: (count) => count + 1,
|
|
210
|
+
finisher: (count) => count,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Sums numeric elements.
|
|
216
|
+
*/
|
|
217
|
+
export function summingNumber<T>(
|
|
218
|
+
mapper: (element: T) => number
|
|
219
|
+
): Collector<T, number, number> {
|
|
220
|
+
return {
|
|
221
|
+
supplier: () => 0,
|
|
222
|
+
accumulator: (sum, element) => sum + mapper(element),
|
|
223
|
+
finisher: (sum) => sum,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Averages numeric elements.
|
|
229
|
+
*/
|
|
230
|
+
export function averagingNumber<T>(
|
|
231
|
+
mapper: (element: T) => number
|
|
232
|
+
): Collector<T, { sum: number; count: number }, number> {
|
|
233
|
+
return {
|
|
234
|
+
supplier: () => ({ sum: 0, count: 0 }),
|
|
235
|
+
accumulator: (acc, element) => {
|
|
236
|
+
acc.sum += mapper(element);
|
|
237
|
+
acc.count++;
|
|
238
|
+
return acc;
|
|
239
|
+
},
|
|
240
|
+
finisher: (acc) => (acc.count === 0 ? 0 : acc.sum / acc.count),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* This function finds the minimum element.
|
|
246
|
+
*/
|
|
247
|
+
export function minBy<T>(
|
|
248
|
+
comparator: (a: T, b: T) => number
|
|
249
|
+
): Collector<T, T | undefined, T | undefined> {
|
|
250
|
+
return {
|
|
251
|
+
supplier: () => undefined,
|
|
252
|
+
accumulator: (min, element) => {
|
|
253
|
+
if (min === undefined || comparator(element, min) < 0) {
|
|
254
|
+
return element;
|
|
255
|
+
}
|
|
256
|
+
return min;
|
|
257
|
+
},
|
|
258
|
+
finisher: (min) => min,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* This function finds the maximum element.
|
|
264
|
+
*/
|
|
265
|
+
export function maxBy<T>(
|
|
266
|
+
comparator: (a: T, b: T) => number
|
|
267
|
+
): Collector<T, T | undefined, T | undefined> {
|
|
268
|
+
return {
|
|
269
|
+
supplier: () => undefined,
|
|
270
|
+
accumulator: (max, element) => {
|
|
271
|
+
if (max === undefined || comparator(element, max) > 0) {
|
|
272
|
+
return element;
|
|
273
|
+
}
|
|
274
|
+
return max;
|
|
275
|
+
},
|
|
276
|
+
finisher: (max) => max,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ============================================================================
|
|
281
|
+
// String collectors
|
|
282
|
+
// ============================================================================
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Joins elements into a string.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* const csv = collect(['a', 'b', 'c'], joining(', '))
|
|
289
|
+
* // "a, b, c"
|
|
290
|
+
*
|
|
291
|
+
* const bracketed = collect(
|
|
292
|
+
* ['a', 'b', 'c'],
|
|
293
|
+
* joining(', ', '[', ']')
|
|
294
|
+
* )
|
|
295
|
+
* // "[a, b, c]"
|
|
296
|
+
*/
|
|
297
|
+
export function joining<T>(
|
|
298
|
+
delimiter: string = "",
|
|
299
|
+
prefix: string = "",
|
|
300
|
+
suffix: string = ""
|
|
301
|
+
): Collector<T, string[], string> {
|
|
302
|
+
return {
|
|
303
|
+
supplier: () => [],
|
|
304
|
+
accumulator: (parts, element) => (parts.push(String(element)), parts),
|
|
305
|
+
finisher: (parts) => prefix + parts.join(delimiter) + suffix,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// ============================================================================
|
|
310
|
+
// Mapping collectors
|
|
311
|
+
// ============================================================================
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* This function applies a mapping function before collecting.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* const names = collect(
|
|
318
|
+
* users,
|
|
319
|
+
* mapping(u => u.name, toList())
|
|
320
|
+
* )
|
|
321
|
+
*/
|
|
322
|
+
export function mapping<T, U, A, R>(
|
|
323
|
+
mapper: (element: T) => U,
|
|
324
|
+
downstream: Collector<U, A, R>
|
|
325
|
+
): Collector<T, A, R> {
|
|
326
|
+
return {
|
|
327
|
+
supplier: downstream.supplier,
|
|
328
|
+
accumulator: (acc, element) => downstream.accumulator(acc, mapper(element)),
|
|
329
|
+
finisher: downstream.finisher,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* This function filters elements before collecting.
|
|
335
|
+
*/
|
|
336
|
+
export function filtering<T, A, R>(
|
|
337
|
+
predicate: (element: T) => boolean,
|
|
338
|
+
downstream: Collector<T, A, R>
|
|
339
|
+
): Collector<T, A, R> {
|
|
340
|
+
return {
|
|
341
|
+
supplier: downstream.supplier,
|
|
342
|
+
accumulator: (acc, element) => {
|
|
343
|
+
return predicate(element) ? downstream.accumulator(acc, element) : acc;
|
|
344
|
+
},
|
|
345
|
+
finisher: downstream.finisher,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* FlatMaps elements before collecting.
|
|
351
|
+
*/
|
|
352
|
+
export function flatMapping<T, U, A, R>(
|
|
353
|
+
mapper: (element: T) => Iterable<U>,
|
|
354
|
+
downstream: Collector<U, A, R>
|
|
355
|
+
): Collector<T, A, R> {
|
|
356
|
+
return {
|
|
357
|
+
supplier: downstream.supplier,
|
|
358
|
+
accumulator: (acc, element) => {
|
|
359
|
+
for (const mapped of mapper(element)) {
|
|
360
|
+
acc = downstream.accumulator(acc, mapped);
|
|
361
|
+
}
|
|
362
|
+
return acc;
|
|
363
|
+
},
|
|
364
|
+
finisher: downstream.finisher,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ============================================================================
|
|
369
|
+
// Reducing collectors
|
|
370
|
+
// ============================================================================
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* General reduction collector.
|
|
374
|
+
*/
|
|
375
|
+
export function reducing<T>(
|
|
376
|
+
identity: T,
|
|
377
|
+
reducer: (acc: T, element: T) => T
|
|
378
|
+
): Collector<T, T, T> {
|
|
379
|
+
return {
|
|
380
|
+
supplier: () => identity,
|
|
381
|
+
accumulator: (acc, element) => reducer(acc, element),
|
|
382
|
+
finisher: (acc) => acc,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Reducing with mapper.
|
|
388
|
+
*/
|
|
389
|
+
export function reducingWith<T, U>(
|
|
390
|
+
identity: U,
|
|
391
|
+
mapper: (element: T) => U,
|
|
392
|
+
reducer: (acc: U, mapped: U) => U
|
|
393
|
+
): Collector<T, U, U> {
|
|
394
|
+
return {
|
|
395
|
+
supplier: () => identity,
|
|
396
|
+
accumulator: (acc, element) => reducer(acc, mapper(element)),
|
|
397
|
+
finisher: (acc) => acc,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// ============================================================================
|
|
402
|
+
// Composite collectors
|
|
403
|
+
// ============================================================================
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* This function collects with two collectors and combines results.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* const stats = collect(
|
|
410
|
+
* numbers,
|
|
411
|
+
* teeing(
|
|
412
|
+
* summingNumber(n => n),
|
|
413
|
+
* counting(),
|
|
414
|
+
* (sum, count) => ({ sum, count, avg: sum / count })
|
|
415
|
+
* )
|
|
416
|
+
* )
|
|
417
|
+
*/
|
|
418
|
+
export function teeing<T, A1, A2, R1, R2, R>(
|
|
419
|
+
collector1: Collector<T, A1, R1>,
|
|
420
|
+
collector2: Collector<T, A2, R2>,
|
|
421
|
+
combiner: (r1: R1, r2: R2) => R
|
|
422
|
+
): Collector<T, [A1, A2], R> {
|
|
423
|
+
return {
|
|
424
|
+
supplier: () => [collector1.supplier(), collector2.supplier()] as [A1, A2],
|
|
425
|
+
accumulator: ([acc1, acc2], element) => [
|
|
426
|
+
collector1.accumulator(acc1, element),
|
|
427
|
+
collector2.accumulator(acc2, element),
|
|
428
|
+
],
|
|
429
|
+
finisher: ([acc1, acc2]) => {
|
|
430
|
+
return combiner(collector1.finisher(acc1), collector2.finisher(acc2));
|
|
431
|
+
},
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// ============================================================================
|
|
436
|
+
// Utility collectors
|
|
437
|
+
// ============================================================================
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* This function collects the first element.
|
|
441
|
+
*/
|
|
442
|
+
export function first<T>(): Collector<T, T | undefined, T | undefined> {
|
|
443
|
+
return {
|
|
444
|
+
supplier: () => undefined,
|
|
445
|
+
accumulator: (first, element) => (first === undefined ? element : first),
|
|
446
|
+
finisher: (first) => first,
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* This function collects the last element.
|
|
452
|
+
*/
|
|
453
|
+
export function last<T>(): Collector<T, T | undefined, T | undefined> {
|
|
454
|
+
return {
|
|
455
|
+
supplier: () => undefined,
|
|
456
|
+
accumulator: (_last, element) => element,
|
|
457
|
+
finisher: (last) => last,
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* This type makes summary statistics.
|
|
463
|
+
*/
|
|
464
|
+
export interface SummaryStatistics {
|
|
465
|
+
count: number;
|
|
466
|
+
sum: number;
|
|
467
|
+
min: number;
|
|
468
|
+
max: number;
|
|
469
|
+
average: number;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
export function summarizingNumber<T>(
|
|
473
|
+
mapper: (element: T) => number
|
|
474
|
+
): Collector<T, Partial<SummaryStatistics>, SummaryStatistics> {
|
|
475
|
+
return {
|
|
476
|
+
supplier: () => ({
|
|
477
|
+
count: 0,
|
|
478
|
+
sum: 0,
|
|
479
|
+
min: Infinity,
|
|
480
|
+
max: -Infinity,
|
|
481
|
+
}),
|
|
482
|
+
accumulator: (stats, element) => {
|
|
483
|
+
const value = mapper(element);
|
|
484
|
+
// The supplier sets every field. (Before, `stats.min || Infinity` treated a minimum of 0
|
|
485
|
+
// as missing: BUGS-2026-07 L16.)
|
|
486
|
+
stats.count = stats.count! + 1;
|
|
487
|
+
stats.sum = stats.sum! + value;
|
|
488
|
+
stats.min = Math.min(stats.min!, value);
|
|
489
|
+
stats.max = Math.max(stats.max!, value);
|
|
490
|
+
return stats;
|
|
491
|
+
},
|
|
492
|
+
finisher: (stats) => ({
|
|
493
|
+
count: stats.count!,
|
|
494
|
+
sum: stats.sum!,
|
|
495
|
+
min: stats.min!,
|
|
496
|
+
max: stats.max!,
|
|
497
|
+
average: stats.count! === 0 ? 0 : stats.sum! / stats.count!,
|
|
498
|
+
}),
|
|
499
|
+
};
|
|
500
|
+
}
|