semantic-typescript 0.3.7 → 0.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collectable.js +46 -0
- package/dist/collector.d.ts +8 -8
- package/dist/collector.js +113 -47
- package/dist/factory.d.ts +38 -8
- package/dist/factory.js +87 -5
- package/dist/guard.d.ts +7 -4
- package/dist/guard.js +19 -7
- package/dist/hook.d.ts +1 -0
- package/dist/hook.js +51 -8
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/optional.js +36 -3
- package/dist/semantic.d.ts +15 -4
- package/dist/semantic.js +69 -59
- package/dist/statistics.js +30 -0
- package/dist/window.js +10 -0
- package/package.json +1 -1
- package/readme.cn.md +732 -210
- package/readme.md +339 -240
package/readme.cn.md
CHANGED
|
@@ -1,10 +1,60 @@
|
|
|
1
|
-
# Semantic-TypeScript
|
|
1
|
+
# Semantic-TypeScript 流处理库
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 引言
|
|
4
4
|
|
|
5
|
-
Semantic-TypeScript
|
|
5
|
+
**Semantic-TypeScript: 现代网络流处理技术的范式转变**
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Semantic-TypeScript 代表着流处理技术的一次重大进步,它综合了 JavaScript GeneratorFunction、Java Stream 和 MySQL 索引范式中最有效的概念。其基础设计原则围绕通过复杂的数据索引方法构建极其高效的数据处理管道。该库为现代前端开发专门提供了一个严格类型安全、函数式纯正的流操作体验。
|
|
8
|
+
|
|
9
|
+
与传统同步处理架构相比,Semantic-TypeScript 实现了完全异步的处理模型。在数据流生成过程中,终端接收数据的时间完全由上游对 `accept` 和 `interrupt` 回调机制的调用决定。这种深思熟虑的架构选择使库能够异常优雅地处理:
|
|
10
|
+
|
|
11
|
+
- 具有确定性延迟特性的**实时数据流**
|
|
12
|
+
- 通过内存高效处理管道处理的**大规模数据集**
|
|
13
|
+
- 具有保证一致性和可靠性的**异步数据源**
|
|
14
|
+
|
|
15
|
+
该库的创新方法从根本上重新构想了开发者与流数据的交互方式,在一个统一的、内聚的包中提供了前所未有的性能特性和开发者体验。
|
|
16
|
+
|
|
17
|
+
### 为何选择 Semantic-TypeScript?
|
|
18
|
+
|
|
19
|
+
在 TypeScript 项目中选择合适的数据流处理库需要在性能、开发者体验和架构契合度之间取得平衡。Semantic-TypeScript 的设计旨在所有这些维度上都表现出色,为现代网络提供了一个范式转变的方法。以下是它脱颖而出的原因。
|
|
20
|
+
|
|
21
|
+
#### 1. **统一、类型安全的流处理范式**
|
|
22
|
+
Semantic-TypeScript 综合了 **JavaScript GeneratorFunctions**、**Java Streams API** 和**数据库索引策略**中最有效的概念。它为处理任何数据序列(无论是静态数组、实时事件还是异步数据块)提供了一个一致的声明式 API,同时充分利用 TypeScript 的全部功能来确保端到端的类型安全。这消除了整个类别的运行时错误,并将流操作转变为可预测的、编译器验证的活动。
|
|
23
|
+
|
|
24
|
+
#### 2. **智能惰性求值与不妥协的性能**
|
|
25
|
+
该库的核心建立在**惰性求值**之上。像 `filter`、`map` 和 `flatMap` 这样的操作只是组合一个处理管道;直到调用终端操作(如 `collect` 或 `toArray`)时才会执行任何工作。这与**短路**功能相结合(通过像 `limit`、`anyMatch` 或自定义的 `interruptor` 函数等操作),使得在处理已知结果时可以提前停止,从而显著提高大型或无限流的效率。
|
|
26
|
+
|
|
27
|
+
#### 3. **架构区分:`Semantic<E>` 与 `Collectable<E>`**
|
|
28
|
+
这个库引入了一个其他库通常缺乏的关键架构分离:
|
|
29
|
+
* **`Semantic<E>`**: 表示抽象的、惰性的流定义——数据转换的“蓝图”。它是不可变的且可组合的。
|
|
30
|
+
* **`Collectable<E>`**: 表示流的物化、可执行视图,提供所有终端操作。
|
|
31
|
+
|
|
32
|
+
这种分离强制执行了一个清晰的思维模型,并允许进行优化(例如,如果不需要顺序,`UnorderedCollectable` 可以跳过昂贵的排序步骤)。
|
|
33
|
+
|
|
34
|
+
#### 4. **`Collector<E, A, R>` 模式的力量**
|
|
35
|
+
灵感来自 Java 的 `Collector` 模式是灵活性的引擎。它将*如何累积流元素*的*规范*与流本身的*执行*解耦。该库为日常任务提供了一组丰富的内置收集器(`useToArray`、`useGroupBy`、`useSummate` 等),同时该模式使得实现您自己复杂的、可重用的归约逻辑变得简单。这比一组固定的终端方法要强大和可组合得多。
|
|
36
|
+
|
|
37
|
+
#### 5. **对现代网络和异步数据的原生支持**
|
|
38
|
+
Semantic-TypeScript 为当代前端和 Node.js 开发而设计。它为现代网络源提供了原生工厂方法:
|
|
39
|
+
* `from(iterable)`、`range()` 用于静态数据。
|
|
40
|
+
* `interval()`、`animationFrame()` 用于基于时间的流。
|
|
41
|
+
* `blob()` 用于分块二进制数据处理。
|
|
42
|
+
* `websocket()` 用于实时消息流。
|
|
43
|
+
它对 `Iterable` 和 `AsyncIterable` 协议的基础支持意味着它能够以同样优雅的 API 处理同步和异步数据流。
|
|
44
|
+
|
|
45
|
+
#### 6. **超越基本聚合:内置统计分析**
|
|
46
|
+
超越简单的求和与平均值。该库提供了专用的 `NumericStatistics` 和 `BigIntStatistics` 类,允许直接从您的流中立即访问高级统计度量——**方差、标准差、中位数、分位数、偏度和峰度**。这将复杂的数据分析变成一行代码,使您免于手动实现或集成另一个专门的库。
|
|
47
|
+
|
|
48
|
+
#### 7. **为开发者体验而设计**
|
|
49
|
+
* **流式的、可链式调用的 API**: 将复杂的数据管道编写为可读的、连续的操作链。
|
|
50
|
+
* **全面的实用工具集**: 开箱即用地提供了必要的守卫(`isOptional`、`isSemantic`)、类型安全的实用工具(`useCompare`、`useTraverse`)和函数式接口。
|
|
51
|
+
* **`Optional<T>` 集成**: 安全地对无值状态建模,消除了查找操作中的空指针问题。
|
|
52
|
+
* **性能指南**: 文档清楚地指导您在何时使用 `toUnordered()`(为了最大速度)与 `toOrdered()`(当顺序很重要时)。
|
|
53
|
+
|
|
54
|
+
#### 8. **稳健的、面向社区的基础**
|
|
55
|
+
作为一个托管在 **GitHub** 上并通过 **npm** 分发的开源库,它是为实际使用而构建的。广泛的文档,包括时间和空间复杂度注释以及实用示例,降低了学习曲线,并为性能敏感的应用提供了清晰度。
|
|
56
|
+
|
|
57
|
+
**总而言之,如果您正在寻找一个严格设计、类型安全且高性能的流处理库,它将企业级数据转换模式的力量带到 TypeScript 生态系统中,同时又不损害现代网络开发的习惯和需求,请选择 Semantic-TypeScript。**
|
|
8
58
|
|
|
9
59
|
## 安装
|
|
10
60
|
|
|
@@ -12,32 +62,32 @@ Semantic-TypeScript 是一个受 JavaScript GeneratorFunction、Java Stream 和
|
|
|
12
62
|
npm install semantic-typescript
|
|
13
63
|
```
|
|
14
64
|
|
|
15
|
-
##
|
|
65
|
+
## 基本类型
|
|
16
66
|
|
|
17
67
|
| 类型 | 描述 |
|
|
18
|
-
|
|
68
|
+
|------|-------------|
|
|
19
69
|
| `Invalid<T>` | 扩展自 `null` 或 `undefined` 的类型 |
|
|
20
70
|
| `Valid<T>` | 排除 `null` 和 `undefined` 的类型 |
|
|
21
71
|
| `MaybeInvalid<T>` | 可能为 `null` 或 `undefined` 的类型 |
|
|
22
72
|
| `Primitive` | 原始类型的集合 |
|
|
23
73
|
| `MaybePrimitive<T>` | 可能是原始类型的类型 |
|
|
24
|
-
| `OptionalSymbol` | `Optional`
|
|
25
|
-
| `SemanticSymbol` | `Semantic`
|
|
26
|
-
| `CollectorsSymbol` | `Collector`
|
|
27
|
-
| `CollectableSymbol` | `Collectable`
|
|
28
|
-
| `OrderedCollectableSymbol` | `OrderedCollectable`
|
|
29
|
-
| `WindowCollectableSymbol` | `WindowCollectable`
|
|
30
|
-
| `StatisticsSymbol` | `Statistics`
|
|
31
|
-
| `NumericStatisticsSymbol` | `NumericStatistics`
|
|
32
|
-
| `BigIntStatisticsSymbol` | `BigIntStatistics`
|
|
33
|
-
| `UnorderedCollectableSymbol` | `UnorderedCollectable`
|
|
74
|
+
| `OptionalSymbol` | `Optional` 类的符号标识符 |
|
|
75
|
+
| `SemanticSymbol` | `Semantic` 类的符号标识符 |
|
|
76
|
+
| `CollectorsSymbol` | `Collector` 类的符号标识符 |
|
|
77
|
+
| `CollectableSymbol` | `Collectable` 类的符号标识符 |
|
|
78
|
+
| `OrderedCollectableSymbol` | `OrderedCollectable` 类的符号标识符 |
|
|
79
|
+
| `WindowCollectableSymbol` | `WindowCollectable` 类的符号标识符 |
|
|
80
|
+
| `StatisticsSymbol` | `Statistics` 类的符号标识符 |
|
|
81
|
+
| `NumericStatisticsSymbol` | `NumericStatistics` 类的符号标识符 |
|
|
82
|
+
| `BigIntStatisticsSymbol` | `BigIntStatistics` 类的符号标识符 |
|
|
83
|
+
| `UnorderedCollectableSymbol` | `UnorderedCollectable` 类的符号标识符 |
|
|
34
84
|
|
|
35
85
|
## 函数式接口
|
|
36
86
|
|
|
37
87
|
| 接口 | 描述 |
|
|
38
|
-
|
|
39
|
-
| `Runnable` |
|
|
40
|
-
| `Supplier<R>` |
|
|
88
|
+
|-----------|-------------|
|
|
89
|
+
| `Runnable` | 无参数、无返回值的函数 |
|
|
90
|
+
| `Supplier<R>` | 无参数、返回 `R` 的函数 |
|
|
41
91
|
| `Functional<T, R>` | 单参数转换函数 |
|
|
42
92
|
| `BiFunctional<T, U, R>` | 双参数转换函数 |
|
|
43
93
|
| `TriFunctional<T, U, V, R>` | 三参数转换函数 |
|
|
@@ -51,7 +101,7 @@ npm install semantic-typescript
|
|
|
51
101
|
| `Generator<T>` | 生成器函数(核心与基础) |
|
|
52
102
|
|
|
53
103
|
```typescript
|
|
54
|
-
//
|
|
104
|
+
// 类型用法示例
|
|
55
105
|
let predicate: Predicate<number> = (n: number): boolean => n > 0;
|
|
56
106
|
let mapper: Functional<string, number> = (text: string): number => text.length;
|
|
57
107
|
let comparator: Comparator<number> = (a: number, b: number): number => a - b;
|
|
@@ -61,8 +111,8 @@ let comparator: Comparator<number> = (a: number, b: number): number => a - b;
|
|
|
61
111
|
|
|
62
112
|
| 函数 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
63
113
|
|------|------|------------|------------|
|
|
64
|
-
| `validate<T>(t: MaybeInvalid<T>): t is T` |
|
|
65
|
-
| `invalidate<T>(t: MaybeInvalid<T>): t is null \| undefined` |
|
|
114
|
+
| `validate<T>(t: MaybeInvalid<T>): t is T` | 验证值不为 null 或 undefined | O(1) | O(1) |
|
|
115
|
+
| `invalidate<T>(t: MaybeInvalid<T>): t is null \| undefined` | 验证值为 null 或 undefined | O(1) | O(1) |
|
|
66
116
|
| `isBoolean(t: unknown): t is boolean` | 检查是否为布尔值 | O(1) | O(1) |
|
|
67
117
|
| `isString(t: unknown): t is string` | 检查是否为字符串 | O(1) | O(1) |
|
|
68
118
|
| `isNumber(t: unknown): t is number` | 检查是否为数字 | O(1) | O(1) |
|
|
@@ -83,41 +133,74 @@ let comparator: Comparator<number> = (a: number, b: number): number => a - b;
|
|
|
83
133
|
| `isNumericStatistics(t: unknown): t is NumericStatistics<unknown>` | 检查是否为 NumericStatistics 实例 | O(1) | O(1) |
|
|
84
134
|
| `isBigIntStatistics(t: unknown): t is BigIntStatistics<unknown>` | 检查是否为 BigIntStatistics 实例 | O(1) | O(1) |
|
|
85
135
|
| `isPromise(t: unknown): t is Promise<unknown>` | 检查是否为 Promise 对象 | O(1) | O(1) |
|
|
86
|
-
| `
|
|
136
|
+
| `isAsyncFunction(t: unknown): t is AsyncFunction` | 检查是否为 AsyncFunction | O(1) | O(1) |
|
|
137
|
+
| `isGenerator(t: unknown): t is Generator<unknown>` | 检查是否为 Generator | O(1) | O(1) |
|
|
138
|
+
| `isGeneratorFunction(t: unknown): t is GeneratorFunction` | 检查是否为 GeneratorFunction | O(1) | O(1) |
|
|
139
|
+
| `isAsyncGeneratorFunction(t: unknown): t is AsyncGeneratorFunction` | 检查是否为 AsyncGeneratorFunction | O(1) | O(1) |
|
|
87
140
|
|
|
88
141
|
```typescript
|
|
89
|
-
//
|
|
142
|
+
// 类型守卫用法示例
|
|
90
143
|
let value: unknown = "hello";
|
|
91
144
|
|
|
92
145
|
if (isString(value)) {
|
|
93
|
-
console.log(value.length); // 类型安全,value
|
|
146
|
+
console.log(value.length); // 类型安全,value 推断为 string
|
|
94
147
|
}
|
|
95
148
|
|
|
96
149
|
if (isOptional(someValue)) {
|
|
97
|
-
someValue.ifPresent((value): void => console.log(
|
|
150
|
+
someValue.ifPresent((value): void => console.log(value));
|
|
98
151
|
}
|
|
99
152
|
|
|
100
153
|
if(isIterable(value)){
|
|
101
|
-
//
|
|
154
|
+
// 类型安全,现在它是一个可迭代对象。
|
|
102
155
|
for(let item of value){
|
|
103
156
|
console.log(item);
|
|
104
157
|
}
|
|
105
158
|
}
|
|
106
159
|
```
|
|
107
160
|
|
|
108
|
-
##
|
|
161
|
+
## 实用函数
|
|
109
162
|
|
|
110
163
|
| 函数 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
111
164
|
|------|------|------------|------------|
|
|
112
|
-
| `useCompare<T>(t1: T, t2: T): number` |
|
|
165
|
+
| `useCompare<T>(t1: T, t2: T): number` | 通用比较函数 | O(1) | O(1) |
|
|
113
166
|
| `useRandom<T = number \| bigint>(index: T): T` | 伪随机数生成器 | O(log n) | O(1) |
|
|
167
|
+
| `useTraverse(t, callback: BiPredicate<keyof T, T[keyof T]>): void` | 深度遍历对象,避免循环引用 | O(n) | O(1) |
|
|
168
|
+
| `useToNumber(t: unknown): number` | 将值转换为数字 | O(1) | O(1) |
|
|
169
|
+
| `useToBigInt(t: unknown): bigint` | 将值转换为 BigInt | O(1) | O(1) |
|
|
114
170
|
|
|
115
171
|
```typescript
|
|
116
|
-
//
|
|
172
|
+
// 实用函数用法示例
|
|
117
173
|
let numbers: Array<number> = [3, 1, 4, 1, 5];
|
|
118
174
|
numbers.sort(useCompare); // [1, 1, 3, 4, 5]
|
|
119
175
|
|
|
120
|
-
let randomNum = useRandom(42); // 基于种子的随机数
|
|
176
|
+
let randomNum: number = useRandom(42); // 基于种子的随机数
|
|
177
|
+
|
|
178
|
+
let o = {
|
|
179
|
+
a: 1,
|
|
180
|
+
b: {
|
|
181
|
+
c: 2,
|
|
182
|
+
d: o
|
|
183
|
+
},
|
|
184
|
+
c: [1, 3, 5, o],
|
|
185
|
+
e: undefined,
|
|
186
|
+
f: null
|
|
187
|
+
};
|
|
188
|
+
useTraverse(o, (value, key): boolean => {
|
|
189
|
+
console.log(key, value);
|
|
190
|
+
/*
|
|
191
|
+
a 1
|
|
192
|
+
c 2
|
|
193
|
+
1 3 5
|
|
194
|
+
不遍历循环引用、undefined 和 null 值。
|
|
195
|
+
*/
|
|
196
|
+
return true; // 返回 true 以继续遍历。
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
let toBeResolved: object = {
|
|
200
|
+
[Symbol.toPrimitive]: () => 5
|
|
201
|
+
};
|
|
202
|
+
let resolvedNumber: number = useToNumber(toBeResolved); // 5
|
|
203
|
+
let resolvedBigInt: bigint = useToBigInt(toBeResolved); // 5n
|
|
121
204
|
```
|
|
122
205
|
|
|
123
206
|
## 工厂方法
|
|
@@ -126,13 +209,13 @@ let randomNum = useRandom(42); // 基于种子的随机数
|
|
|
126
209
|
|
|
127
210
|
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
128
211
|
|------|------|------------|------------|
|
|
129
|
-
| `Optional.empty<T>()
|
|
130
|
-
| `Optional.of<T>(value)
|
|
131
|
-
| `Optional.ofNullable<T>(value)
|
|
132
|
-
| `Optional.ofNonNull<T>(value)
|
|
212
|
+
| `Optional.empty<T>(): Optional<T>` | 创建一个空的 Optional | O(1) | O(1) |
|
|
213
|
+
| `Optional.of<T>(value): Optional<T>` | 创建一个包含值的 Optional | O(1) | O(1) |
|
|
214
|
+
| `Optional.ofNullable<T>(value): Optional<T>` | 创建一个可能为空的 Optional | O(1) | O(1) |
|
|
215
|
+
| `Optional.ofNonNull<T>(value): Optional<T>` | 创建一个非 null 的 Optional | O(1) | O(1) |
|
|
133
216
|
|
|
134
217
|
```typescript
|
|
135
|
-
// Optional
|
|
218
|
+
// Optional 用法示例
|
|
136
219
|
let empty: Optional<number> = Optional.empty();
|
|
137
220
|
let present: Optional<number> = Optional.of(42);
|
|
138
221
|
let nullable: Optional<string> = Optional.ofNullable<string>(null);
|
|
@@ -146,62 +229,174 @@ console.log(empty.get(100)); // 输出 100
|
|
|
146
229
|
|
|
147
230
|
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
148
231
|
|------|------|------------|------------|
|
|
149
|
-
| `Collector.full(identity
|
|
150
|
-
| `Collector.
|
|
232
|
+
| `Collector.full(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个完整的收集器 | O(1) | O(1) |
|
|
233
|
+
| `Collector.full(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个完整的收集器 | O(1) | O(1) |
|
|
234
|
+
| `Collector.shortable(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | O(1) | O(1) |
|
|
235
|
+
| `Collector.shortable(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | O(1) | O(1) |
|
|
236
|
+
| `Collector.shortable(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | O(1) | O(1) |
|
|
237
|
+
| `Collector.shortable(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | O(1) | O(1) |
|
|
238
|
+
| `Collector.shortable(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | O(1) | O(1) |
|
|
239
|
+
| `Collector.shortable(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | O(1) | O(1) |
|
|
240
|
+
| `useAnyMatch<E>(predicate: Predicate<E>): Collector<E, boolean, boolean>` | 创建一个检查是否有任何元素匹配断言的收集器 | `predicate: Predicate<E>` | `Collector<E, boolean, boolean>` |
|
|
241
|
+
| `useAllMatch<E>(predicate: Predicate<E>): Collector<E, boolean, boolean>` | 创建一个检查是否所有元素都匹配断言的收集器 | `predicate: Predicate<E>` | `Collector<E, boolean, boolean>` |
|
|
242
|
+
| `useCollect<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个完整的收集器 | `identity: Supplier<A>`, `accumulator: BiFunctional<A, E, A>`, `finisher: Functional<A, R>` | `Collector<E, A, R>` |
|
|
243
|
+
| `useCollect<E, A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个完整的收集器 | `identity: Supplier<A>`, `accumulator: TriFunctional<A, E, bigint, A>`, `finisher: Functional<A, R>` | `Collector<E, A, R>` |
|
|
244
|
+
| `useCollect<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | `identity: Supplier<A>`, `interruptor: Predicate<E>`, `accumulator: BiFunctional<A, E, A>`, `finisher: Functional<A, R>` | `Collector<E, A, R>` |
|
|
245
|
+
| `useCollect<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | `identity: Supplier<A>`, `interruptor: Predicate<E>`, `accumulator: TriFunctional<A, E, bigint, A>`, `finisher: Functional<A, R>` | `Collector<E, A, R>` |
|
|
246
|
+
| `useCollect<E, A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | `identity: Supplier<A>`, `interruptor: BiPredicate<E, bigint>`, `accumulator: BiFunctional<A, E, A>`, `finisher: Functional<A, R>` | `Collector<E, A, R>` |
|
|
247
|
+
| `useCollect<E, A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | `identity: Supplier<A>`, `interruptor: BiPredicate<E, bigint>`, `accumulator: TriFunctional<A, E, bigint, A>`, `finisher: Functional<A, R>` | `Collector<E, A, R>` |
|
|
248
|
+
| `useCollect<E, A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>` | 创建一个可中断的收集器 | `identity: Supplier<A>`, `interruptor: TriPredicate<E, bigint, A>`, `accumulator: BiFunctional<A, E, A>`, `finisher: Functional<A, R>` | `Collector<E, A, R>` |
|
|
249
|
+
| `useCollect<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>` | 创建一个可中断的收集器 | `identity: Supplier<A>`, `interruptor: TriPredicate<E, bigint, A>`, `accumulator: TriFunctional<A, E, bigint, A>`, `finisher: Functional<A, R>` | `Collector<E, A, R>` |
|
|
250
|
+
| `useCount<E = unknown>(): Collector<E, bigint, bigint>` | 创建一个计算元素数量的收集器 | 无 | `Collector<E, bigint, bigint>` |
|
|
251
|
+
| `useError<E = unknown>(): Collector<E, string, string>` | 创建一个将错误累积到 console.error 的收集器 | 无 | `Collector<E, string, string>` |
|
|
252
|
+
| `useError<E = unknown>(accumulator: BiFunctional<string, E, string>): Collector<E, string, string>` | 创建一个使用自定义累加器累积错误的收集器 | `accumulator: BiFunctional<string, E, string>` | `Collector<E, string, string>` |
|
|
253
|
+
| `useError<E = unknown>(accumulator: TriFunctional<string, E, bigint, string>): Collector<E, string, string>` | 创建一个使用自定义累加器累积错误的收集器 | `accumulator: TriFunctional<string, E, bigint, string>` | `Collector<E, string, string>` |
|
|
254
|
+
| `useError<E = unknown>(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): Collector<E, string, string>` | 创建一个带前缀和后缀的累积错误收集器 | `prefix: string`, `accumulator: BiFunctional<string, E, string>`, `suffix: string` | `Collector<E, string, string>` |
|
|
255
|
+
| `useError<E = unknown>(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>` | 创建一个带前缀和后缀的累积错误收集器 | `prefix: string`, `accumulator: TriFunctional<string, E, bigint, string>`, `suffix: string` | `Collector<E, string, string>` |
|
|
256
|
+
| `useFindFirst<E>(): Collector<E, Optional<E>, Optional<E>>` | 创建一个查找第一个元素的收集器 | 无 | `Collector<E, Optional<E>, Optional<E>>` |
|
|
257
|
+
| `useFindAny<E>(): Collector<E, Optional<E>, Optional<E>>` | 创建一个随机查找任意元素的收集器 | 无 | `Collector<E, Optional<E>, Optional<E>>` |
|
|
258
|
+
| `useFindLast<E>(): Collector<E, Optional<E>, Optional<E>>` | 创建一个查找最后一个元素的收集器 | 无 | `Collector<E, Optional<E>, Optional<E>>` |
|
|
259
|
+
| `useFindMaximum<E>(): Collector<E, Optional<E>, Optional<E>>` | 创建一个查找最大元素的收集器 | 无 | `Collector<E, Optional<E>, Optional<E>>` |
|
|
260
|
+
| `useFindMaximum<E>(comparator: Comparator<E>): Collector<E, Optional<E>, Optional<E>>` | 创建一个使用自定义比较器查找最大元素的收集器 | `comparator: Comparator<E>` | `Collector<E, Optional<E>, Optional<E>>` |
|
|
261
|
+
| `useFindMinimum<E>(): Collector<E, Optional<E>, Optional<E>>` | 创建一个查找最小元素的收集器 | 无 | `Collector<E, Optional<E>, Optional<E>>` |
|
|
262
|
+
| `useFindMinimum<E>(comparator: Comparator<E>): Collector<E, Optional<E>, Optional<E>>` | 创建一个使用自定义比较器查找最小元素的收集器 | `comparator: Comparator<E>` | `Collector<E, Optional<E>, Optional<E>>` |
|
|
263
|
+
| `useForEach<E>(action: Consumer<E>): Collector<E, bigint, bigint>` | 创建一个对每个元素执行操作的收集器 | `action: Consumer<E>` | `Collector<E, bigint, bigint>` |
|
|
264
|
+
| `useForEach<E>(action: BiConsumer<E, bigint>): Collector<E, bigint, bigint>` | 创建一个对每个元素(带索引)执行操作的收集器 | `action: BiConsumer<E, bigint>` | `Collector<E, bigint, bigint>` |
|
|
265
|
+
| `useNoneMatch<E>(predicate: Predicate<E>): Collector<E, boolean, boolean>` | 创建一个检查是否有元素不匹配断言的收集器 | `predicate: Predicate<E>` | `Collector<E, boolean, boolean>` |
|
|
266
|
+
| `useGroup<E, K>(classifier: Functional<E, K>): Collector<E, Map<K, E[]>, Map<K, E[]>>` | 创建一个按分类器分组元素的收集器 | `classifier: Functional<E, K>` | `Collector<E, Map<K, E[]>, Map<K, E[]>>` |
|
|
267
|
+
| `useGroupBy<E, K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Collector<E, Map<K, V[]>, Map<K, V[]>>` | 创建一个按键分组并提取值的收集器 | `keyExtractor: Functional<E, K>`, `valueExtractor: Functional<E, V>` | `Collector<E, Map<K, V[]>, Map<K, V[]>>` |
|
|
268
|
+
| `useJoin<E = unknown>(): Collector<E, string, string>` | 创建一个将元素连接成字符串的收集器 | 无 | `Collector<E, string, string>` |
|
|
269
|
+
| `useJoin<E = unknown>(delimiter: string): Collector<E, string, string>` | 创建一个使用分隔符连接元素的收集器 | `delimiter: string` | `Collector<E, string, string>` |
|
|
270
|
+
| `useJoin<E = unknown>(prefix: string, delimiter: string, suffix: string): Collector<E, string, string>` | 创建一个使用前缀、分隔符和后缀连接元素的收集器 | `prefix: string`, `delimiter: string`, `suffix: string` | `Collector<E, string, string>` |
|
|
271
|
+
| `useJoin<E = unknown>(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): Collector<E, string, string>` | 创建一个使用自定义累加器连接元素的收集器 | `prefix: string`, `accumulator: BiFunctional<string, E, string>`, `suffix: string` | `Collector<E, string, string>` |
|
|
272
|
+
| `useJoin<E = unknown>(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>` | 创建一个使用自定义累加器连接元素的收集器 | `prefix: string`, `accumulator: TriFunctional<string, E, bigint, string>`, `suffix: string` | `Collector<E, string, string>` |
|
|
273
|
+
| `useLog<E = unknown>(): Collector<E, string, string>` | 创建一个将日志累积到 console.log 的收集器 | 无 | `Collector<E, string, string>` |
|
|
274
|
+
| `useLog<E = unknown>(accumulator: BiFunctional<string, E, string>): Collector<E, string, string>` | 创建一个使用自定义累加器累积日志的收集器 | `accumulator: BiFunctional<string, E, string>` | `Collector<E, string, string>` |
|
|
275
|
+
| `useLog<E = unknown>(accumulator: TriFunctional<string, E, bigint, string>): Collector<E, string, string>` | 创建一个使用自定义累加器累积日志的收集器 | `accumulator: TriFunctional<string, E, bigint, string>` | `Collector<E, string, string>` |
|
|
276
|
+
| `useLog<E = unknown>(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): Collector<E, string, string>` | 创建一个带前缀和后缀的累积日志收集器 | `prefix: string`, `accumulator: BiFunctional<string, E, string>`, `suffix: string` | `Collector<E, string, string>` |
|
|
277
|
+
| `useLog<E = unknown>(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>` | 创建一个带前缀和后缀的累积日志收集器 | `prefix: string`, `accumulator: TriFunctional<string, E, bigint, string>`, `suffix: string` | `Collector<E, string, string>` |
|
|
278
|
+
| `usePartition<E>(count: bigint): Collector<E, Array<Array<E>>, Array<Array<E>>>` | 创建一个将元素分组的收集器 | `count: bigint` | `Collector<E, Array<Array<E>>, Array<Array<E>>>` |
|
|
279
|
+
| `usePartitionBy<E>(classifier: Functional<E, bigint>): Collector<E, Array<E[]>, Array<E[]>>` | 创建一个按分类器对元素分区的收集器 | `classifier: Functional<E, bigint>` | `Collector<E, Array<E[]>, Array<E[]>>` |
|
|
280
|
+
| `useReduce<E>(accumulator: BiFunctional<E, E, E>): Collector<E, Optional<E>, Optional<E>>` | 创建一个归约元素的收集器 | `accumulator: BiFunctional<E, E, E>` | `Collector<E, Optional<E>, Optional<E>>` |
|
|
281
|
+
| `useReduce<E>(accumulator: TriFunctional<E, E, bigint, E>): Collector<E, Optional<E>, Optional<E>>` | 创建一个归约元素的收集器 | `accumulator: TriFunctional<E, E, bigint, E>` | `Collector<E, Optional<E>, Optional<E>>` |
|
|
282
|
+
| `useReduce<E>(identity: E, accumulator: BiFunctional<E, E, E>): Collector<E, E, E>` | 创建一个带恒等值的归约元素收集器 | `identity: E`, `accumulator: BiFunctional<E, E, E>` | `Collector<E, E, E>` |
|
|
283
|
+
| `useReduce<E>(identity: E, accumulator: TriFunctional<E, E, bigint, E>): Collector<E, E, E>` | 创建一个带恒等值的归约元素收集器 | `identity: E`, `accumulator: TriFunctional<E, E, bigint, E>` | `Collector<E, E, E>` |
|
|
284
|
+
| `useReduce<E, R>(identity: R, accumulator: BiFunctional<R, E, R>, finisher: Functional<R, R>): Collector<E, R, R>` | 创建一个带恒等值和最终器的归约元素收集器 | `identity: R`, `accumulator: BiFunctional<R, E, R>`, `finisher: Functional<R, R>` | `Collector<E, R, R>` |
|
|
285
|
+
| `useReduce<E, R>(identity: R, accumulator: TriFunctional<R, E, bigint, R>, finisher: Functional<R, R>): Collector<E, R, R>` | 创建一个带恒等值和最终器的归约元素收集器 | `identity: R`, `accumulator: TriFunctional<R, E, bigint, R>`, `finisher: Functional<R, R>` | `Collector<E, R, R>` |
|
|
286
|
+
| `useToArray<E>(): Collector<E, E[], E[]>` | 创建一个将元素累积到数组中的收集器 | 无 | `Collector<E, E[], E[]>` |
|
|
287
|
+
| `useToMap<E, K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Collector<E, Map<K, V>, Map<K, V>>` | 创建一个将元素累积到映射中的收集器 | `keyExtractor: Functional<E, K>`, `valueExtractor: Functional<E, V>` | `Collector<E, Map<K, V>, Map<K, V>>` |
|
|
288
|
+
| `useToSet<E>(): Collector<E, Set<E>, Set<E>>` | 创建一个将元素累积到集合中的收集器 | 无 | `Collector<E, Set<E>, Set<E>>` |
|
|
289
|
+
| `useWrite<E, S = string>(stream: WritableStream<S>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>` | 创建一个将元素写入流的收集器 | `stream: WritableStream<S>` | `Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>` |
|
|
290
|
+
| `useWrite<E, S = string>(stream: WritableStream<S>, accumulator: BiFunctional<WritableStream<S>, E, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>` | 创建一个使用自定义累加器将元素写入流的收集器 | `stream: WritableStream<S>`, `accumulator: BiFunctional<WritableStream<S>, E, WritableStream<S>>` | `Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>` |
|
|
291
|
+
| `useWrite<E, S = string>(stream: WritableStream<S>, accumulator: TriFunctional<WritableStream<S>, E, bigint, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>` | 创建一个使用自定义累加器将元素写入流的收集器 | `stream: WritableStream<S>`, `accumulator: TriFunctional<WritableStream<S>, E, bigint, WritableStream<S>>` | `Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>` |
|
|
292
|
+
| `useNumericSummate<E>(): Collector<E, number, number>` | 创建一个对数字值求和的收集器 | 无 | `Collector<number, number, number>` 或 `Collector<E, number, number>` |
|
|
293
|
+
| `useNumericSummate<E>(mapper: Functional<E, number>): Collector<E, number, number>` | 创建一个对映射的数字值求和的收集器 | `mapper: Functional<E, number>` | `Collector<E, number, number>` |
|
|
294
|
+
| `useBigIntSummate<E>(): Collector<E, bigint, bigint>` | 创建一个对 bigint 值求和的收集器 | 无 | `Collector<bigint, bigint, bigint>` 或 `Collector<E, bigint, bigint>` |
|
|
295
|
+
| `useBigIntSummate<E>(mapper: Functional<E, bigint>): Collector<E, bigint, bigint>` | 创建一个对映射的 bigint 值求和的收集器 | `mapper: Functional<E, bigint>` | `Collector<E, bigint, bigint>` |
|
|
296
|
+
| `useNumericAverage<E>(): Collector<E, NumericAverageAccumulator, number>` | 创建一个计算数字平均值的收集器 | 无 | `Collector<number, NumericAverageAccumulator, number>` 或 `Collector<E, NumericAverageAccumulator, number>` |
|
|
297
|
+
| `useNumericAverage<E>(mapper: Functional<E, number>): Collector<E, NumericAverageAccumulator, number>` | 创建一个计算映射值数字平均值的收集器 | `mapper: Functional<E, number>` | `Collector<E, NumericAverageAccumulator, number>` |
|
|
298
|
+
| `useBigIntAverage<E>(): Collector<E, BigIntAverageAccumulator, bigint>` | 创建一个计算 bigint 平均值的收集器 | 无 | `Collector<bigint, BigIntAverageAccumulator, bigint>` 或 `Collector<E, BigIntAverageAccumulator, bigint>` |
|
|
299
|
+
| `useBigIntAverage<E>(mapper: Functional<E, bigint>): Collector<E, BigIntAverageAccumulator, bigint>` | 创建一个计算映射值 bigint 平均值的收集器 | `mapper: Functional<E, bigint>` | `Collector<E, BigIntAverageAccumulator, bigint>` |
|
|
300
|
+
| `useFrequency<E>(): Collector<E, Map<E, bigint>, Map<E, bigint>>` | 创建一个计算元素频率的收集器 | 无 | `Collector<E, Map<E, bigint>, Map<E, bigint>>` |
|
|
301
|
+
| `useNumericMode<E>(): Collector<E, Map<number, bigint>, number>` | 创建一个查找数字众数的收集器 | 无 | `Collector<number, Map<number, bigint>, number>` 或 `Collector<E, Map<number, bigint>, number>` |
|
|
302
|
+
| `useNumericMode<E>(mapper: Functional<E, number>): Collector<E, Map<number, bigint>, number>` | 创建一个查找映射值数字众数的收集器 | `mapper: Functional<E, number>` | `Collector<E, Map<number, bigint>, number>` |
|
|
303
|
+
| `useBigIntMode<E>(): Collector<E, Map<bigint, bigint>, bigint>` | 创建一个查找 bigint 众数的收集器 | 无 | `Collector<bigint, Map<bigint, bigint>, bigint>` 或 `Collector<E, Map<bigint, bigint>, bigint>` |
|
|
304
|
+
| `useBigIntMode<E>(mapper: Functional<E, bigint>): Collector<E, Map<bigint, bigint>, bigint>` | 创建一个查找映射值 bigint 众数的收集器 | `mapper: Functional<E, bigint>` | `Collector<E, Map<bigint, bigint>, bigint>` |
|
|
305
|
+
| `useNumericVariance<E>(): Collector<E, VarianceAccumulator, number>` | 创建一个计算数字方差的收集器 | 无 | `Collector<number, VarianceAccumulator, number>` 或 `Collector<E, VarianceAccumulator, number>` |
|
|
306
|
+
| `useNumericVariance<E>(mapper: Functional<E, number>): Collector<E, VarianceAccumulator, number>` | 创建一个计算映射值数字方差的收集器 | `mapper: Functional<E, number>` | `Collector<E, VarianceAccumulator, number>` |
|
|
307
|
+
| `useBigIntVariance<E>(): Collector<E, BigIntVarianceAccumulator, bigint>` | 创建一个计算 bigint 方差的收集器 | 无 | `Collector<bigint, BigIntVarianceAccumulator, bigint>` 或 `Collector<E, BigIntVarianceAccumulator, bigint>` |
|
|
308
|
+
| `useBigIntVariance<E>(mapper: Functional<E, bigint>): Collector<E, BigIntVarianceAccumulator, bigint>` | 创建一个计算映射值 bigint 方差的收集器 | `mapper: Functional<E, bigint>` | `Collector<E, BigIntVarianceAccumulator, bigint>` |
|
|
309
|
+
| `useNumericStandardDeviation<E>(): Collector<E, StandardDeviationAccumulator, number>` | 创建一个计算数字标准差的收集器 | 无 | `Collector<number, StandardDeviationAccumulator, number>` 或 `Collector<E, StandardDeviationAccumulator, number>` |
|
|
310
|
+
| `useNumericStandardDeviation<E>(mapper: Functional<E, number>): Collector<E, StandardDeviationAccumulator, number>` | 创建一个计算映射值数字标准差的收集器 | `mapper: Functional<E, number>` | `Collector<E, StandardDeviationAccumulator, number>` |
|
|
311
|
+
| `useBigIntStandardDeviation<E>(): Collector<E, BigIntStandardDeviationAccumulator, bigint>` | 创建一个计算 bigint 标准差的收集器 | 无 | `Collector<bigint, BigIntStandardDeviationAccumulator, bigint>` 或 `Collector<E, BigIntStandardDeviationAccumulator, bigint>` |
|
|
312
|
+
| `useBigIntStandardDeviation<E>(mapper: Functional<E, bigint>): Collector<E, BigIntStandardDeviationAccumulator, bigint>` | 创建一个计算映射值 bigint 标准差的收集器 | `mapper: Functional<E, bigint>` | `Collector<E, BigIntStandardDeviationAccumulator, bigint>` |
|
|
313
|
+
| `useNumericMedian<E>(): Collector<E, number[], number>` | 创建一个计算数字中位数的收集器 | 无 | `Collector<number, number[], number>` 或 `Collector<E, number[], number>` |
|
|
314
|
+
| `useNumericMedian<E>(mapper: Functional<E, number>): Collector<E, number[], number>` | 创建一个计算映射值数字中位数的收集器 | `mapper: Functional<E, number>` | `Collector<E, number[], number>` |
|
|
315
|
+
| `useBigIntMedian<E>(): Collector<E, bigint[], bigint>` | 创建一个计算 bigint 中位数的收集器 | 无 | `Collector<bigint, bigint[], bigint>` 或 `Collector<E, bigint[], bigint>` |
|
|
316
|
+
| `useBigIntMedian<E>(mapper: Functional<E, bigint>): Collector<E, bigint[], bigint>` | 创建一个计算映射值 bigint 中位数的收集器 | `mapper: Functional<E, bigint>` | `Collector<E, bigint[], bigint>` |
|
|
317
|
+
| `useToGeneratorFunction<E>(): Collector<E, Array<E>, globalThis.Generator<E, void, undefined>>` | 创建一个将元素转换为生成器函数的收集器 | 无 | `Collector<E, Array<E>, globalThis.Generator<E, void, undefined>>` |
|
|
318
|
+
| `useToAsyncGeneratorFunction<E>(): Collector<E, Array<E>, globalThis.AsyncGenerator<E, void, undefined>>` | 创建一个将元素转换为异步生成器函数的收集器 | 无 | `Collector<E, Array<E>, globalThis.AsyncGenerator<E, void, undefined>>` |
|
|
151
319
|
|
|
152
320
|
```typescript
|
|
153
|
-
//
|
|
321
|
+
// 收集器转换示例
|
|
154
322
|
let numbers: Semantic<number> = from([3, 1, 4, 1, 5, 9, 2, 6, 5]);
|
|
155
323
|
|
|
156
|
-
//
|
|
324
|
+
// 性能优先:使用无序收集器
|
|
157
325
|
let unordered: UnorderedCollectable<number> = from([3, 1, 4, 1, 5, 9, 2, 6, 5])
|
|
158
326
|
.filter((n: number): boolean => n > 3)
|
|
159
|
-
.
|
|
327
|
+
.toUnordered();
|
|
160
328
|
|
|
161
|
-
// 需要排序:使用有序收集器
|
|
329
|
+
// 需要排序:使用有序收集器
|
|
162
330
|
let ordered: OrderedCollectable<number> = from([3, 1, 4, 1, 5, 9, 2, 6, 5])
|
|
163
331
|
.sorted();
|
|
164
332
|
|
|
165
|
-
//
|
|
166
|
-
let count: Collector<number, number, number> =
|
|
167
|
-
(): number => 0, // 初始值
|
|
168
|
-
(accumulator: number, element: number): number => accumulator + element, // 累加
|
|
169
|
-
(accumulator: number): number => accumulator // 完成
|
|
170
|
-
);
|
|
333
|
+
// 计算元素数量
|
|
334
|
+
let count: Collector<number, number, number> = useCount();
|
|
171
335
|
count.collect(from([1,2,3,4,5])); // 从流中计数
|
|
172
|
-
count.collect([1,2,3,4,5]); //
|
|
336
|
+
count.collect([1,2,3,4,5]); // 从可迭代对象中计数
|
|
173
337
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
(element: number, index: bigint, accumulator: Optional<number>): Optional<number> => accumulator.isPresent(), // 中断
|
|
177
|
-
(accumulator: Optional<number>, element: number, index: bigint): Optional<number> => Optional.of(element), // 累加
|
|
178
|
-
(accumulator: Optional<number>): Optional<number> => accumulator // 完成
|
|
179
|
-
);
|
|
338
|
+
// 查找第一个元素
|
|
339
|
+
let findFirst: Collector<number, number, number> = useFindFirst();
|
|
180
340
|
find.collect(from([1,2,3,4,5])); // 查找第一个元素
|
|
181
341
|
find.collect([1,2,3,4,5]); // 查找第一个元素
|
|
342
|
+
|
|
343
|
+
// 计算元素总和
|
|
344
|
+
let summate: Collector<number, number, number> = useSummate();
|
|
345
|
+
summate.collect(from([1,2,3,4,5])); // 从流中求和
|
|
346
|
+
summate.collect([1,2,3,4,5]); // 从可迭代对象中求和
|
|
347
|
+
|
|
348
|
+
// 计算元素平均值
|
|
349
|
+
let average: Collector<number, number, number> = useNumericAverage();
|
|
350
|
+
average.collect(from([1,2,3,4,5])); // 从流中计算平均值
|
|
351
|
+
average.collect([1,2,3,4,5]); // 从可迭代对象中计算平均值
|
|
182
352
|
```
|
|
183
353
|
|
|
184
|
-
|
|
354
|
+
## Collector 类方法
|
|
185
355
|
|
|
186
356
|
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
187
|
-
|
|
188
|
-
| `
|
|
189
|
-
| `
|
|
190
|
-
| `
|
|
191
|
-
| `
|
|
192
|
-
| `
|
|
193
|
-
| `
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
357
|
+
|--------|-------------|-----------------|------------------|
|
|
358
|
+
| `collect(generator: Generator<E>): R` | 从生成器函数收集元素 | O(n) | O(1) |
|
|
359
|
+
| `collect(iterable: Iterable<E>): R` | 从可迭代对象收集元素 | O(n) | O(1) |
|
|
360
|
+
| `collect(semantic: Semantic<E>): R` | 从语义流收集元素 | O(n) | O(1) |
|
|
361
|
+
| `collect(collectable: Collectable<E>): R` | 从可收集流收集元素 | O(n) | O(1) |
|
|
362
|
+
| `collect(start: number, end: number): R` | 从数字范围收集元素 | O(n) | O(1) |
|
|
363
|
+
| `collect(start: bigint, end: bigint): R` | 从 bigint 范围收集元素 | O(n) | O(1) |
|
|
364
|
+
|
|
365
|
+
### Semantic 工厂方法
|
|
366
|
+
|
|
367
|
+
基于提供的代码,以下是所有 Semantic 工厂函数的描述表:
|
|
368
|
+
|
|
369
|
+
| 函数 | 描述 | 参数 | 返回类型 |
|
|
370
|
+
|----------|-------------|------------|-------------|
|
|
371
|
+
| `animationFrame(period: number): Semantic<number>` | 创建一个发出动画帧时间戳的语义流 | `period: number` | `Semantic<number>` |
|
|
372
|
+
| `animationFrame(period: number, delay: number): Semantic<number>` | 创建一个带延迟的发出动画帧时间戳的语义流 | `period: number`, `delay: number` | `Semantic<number>` |
|
|
373
|
+
| `attribute<T extends object>(target: T): Semantic<Attribute<T>>` | 创建一个发出对象属性的语义流 | `target: T` | `Semantic<Attribute<T>>` |
|
|
374
|
+
| `blob(blob: Blob): Semantic<Uint8Array>` | 创建一个以块的形式发出 Blob 数据的语义流 | `blob: Blob` | `Semantic<Uint8Array>` |
|
|
375
|
+
| `blob(blob: Blob, chunk: bigint): Semantic<Uint8Array>` | 创建一个使用自定义块大小发出 Blob 数据的语义流 | `blob: Blob`, `chunk: bigint` | `Semantic<Uint8Array>` |
|
|
376
|
+
| `empty<E>(): Semantic<E>` | 创建一个不发出任何元素的空语义流 | 无 | `Semantic<E>` |
|
|
377
|
+
| `fill<E>(element: E, count: bigint): Semantic<E>` | 创建一个重复发出一个元素的语义流 | `element: E`, `count: bigint` | `Semantic<E>` |
|
|
378
|
+
| `fill<E>(supplier: Supplier<E>, count: bigint): Semantic<E>` | 创建一个从提供者发出值的语义流 | `supplier: Supplier<E>`, `count: bigint` | `Semantic<E>` |
|
|
379
|
+
| `from<E>(iterable: Iterable<E>): Semantic<E>` | 从可迭代对象创建语义流 | `iterable: Iterable<E>` | `Semantic<E>` |
|
|
380
|
+
| `from<E>(iterable: AsyncIterable<E>): Semantic<E>` | 从异步可迭代对象创建语义流 | `iterable: AsyncIterable<E>` | `Semantic<E>` |
|
|
381
|
+
| `generate<E>(supplier: Supplier<E>, count: bigint): Semantic<E>` | 创建一个生成固定数量值的语义流 | `supplier: Supplier<E>`, `count: bigint` | `Semantic<E>` |
|
|
382
|
+
| `generate<E>(element: E, count: bigint): Semantic<E>` | 创建一个重复一个元素的语义流 | `element: E`, `count: bigint` | `Semantic<E>` |
|
|
383
|
+
| `generate<E>(supplier: Supplier<E>, interrupt: Predicate<E>): Semantic<E>` | 创建一个生成直到满足条件的语义流 | `supplier: Supplier<E>`, `interrupt: Predicate<E>` | `Semantic<E>` |
|
|
384
|
+
| `generate<E>(supplier: Supplier<E>, interrupt: BiPredicate<E, bigint>): Semantic<E>` | 创建一个生成直到满足条件的语义流 | `supplier: Supplier<E>`, `interrupt: BiPredicate<E, bigint>` | `Semantic<E>` |
|
|
385
|
+
| `interval(period: number): Semantic<number>` | 创建一个以固定间隔发出的语义流 | `period: number` | `Semantic<number>` |
|
|
386
|
+
| `interval(period: number, delay: number): Semantic<number>` | 创建一个具有初始延迟的以固定间隔发出的语义流 | `period: number`, `delay: number` | `Semantic<number>` |
|
|
387
|
+
| `iterate<E>(generator: Generator<E>): Semantic<E>` | 从生成器函数创建语义流 | `generator: Generator<E>` | `Semantic<E>` |
|
|
388
|
+
| `promise<T>(promise: Promise<T>): Semantic<T>` | 创建一个发出 Promise 结果的语义流 | `promise: Promise<T>` | `Semantic<T>` |
|
|
389
|
+
| `range(start: number, end: number): Semantic<number>` | 创建一个发出数字范围的语义流 | `start: number`, `end: number` | `Semantic<number>` |
|
|
390
|
+
| `range(start: number, end: number, step: number): Semantic<number>` | 创建一个使用自定义步长发出范围的语义流 | `start: number`, `end: number`, `step: number` | `Semantic<number>` |
|
|
391
|
+
| `websocket(websocket: WebSocket): Semantic<MessageEvent \| CloseEvent \| Event>` | 创建一个发出 WebSocket 事件的语义流 | `websocket: WebSocket` | `Semantic<MessageEvent \| CloseEvent \| Event>` |
|
|
197
392
|
|
|
198
393
|
```typescript
|
|
199
|
-
// Semantic
|
|
394
|
+
// Semantic 工厂方法用法示例
|
|
200
395
|
|
|
201
396
|
// 从定时动画帧创建流
|
|
202
397
|
animationFrame(1000)
|
|
203
398
|
.toUnordered()
|
|
204
|
-
.forEach(frame => console.log(frame));
|
|
399
|
+
.forEach((frame): void => console.log(frame));
|
|
205
400
|
|
|
206
401
|
// 从 Blob 创建流(分块读取)
|
|
207
402
|
blob(someBlob, 1024n)
|
|
@@ -210,15 +405,15 @@ blob(someBlob, 1024n)
|
|
|
210
405
|
.then(callback) // 写入流成功
|
|
211
406
|
.catch(callback); // 写入流失败
|
|
212
407
|
|
|
213
|
-
//
|
|
408
|
+
// 创建一个空流,直到与其他流连接才会执行
|
|
214
409
|
empty<string>()
|
|
215
410
|
.toUnordered()
|
|
216
411
|
.join(); //[]
|
|
217
412
|
|
|
218
|
-
//
|
|
413
|
+
// 创建一个填充的流
|
|
219
414
|
let filledStream = fill("hello", 3); // "hello", "hello", "hello"
|
|
220
415
|
|
|
221
|
-
//
|
|
416
|
+
// 创建一个具有初始 2 秒延迟和 5 秒执行周期的定时流,基于计时器机制实现;由于系统调度精度限制,可能会经历时间漂移。
|
|
222
417
|
let intervalStream = interval(5000, 2000);
|
|
223
418
|
|
|
224
419
|
// 从可迭代对象创建流
|
|
@@ -226,47 +421,61 @@ let numberStream = from([1, 2, 3, 4, 5]);
|
|
|
226
421
|
let stringStream = from(new Set(["Alex", "Bob"]));
|
|
227
422
|
|
|
228
423
|
// 从已解析的 Promise 创建流
|
|
229
|
-
let promisedStream: Semantic<Array<number>> = Promise.resolve([1, 2, 3, 4, 5]);
|
|
424
|
+
let promisedStream: Semantic<Array<number>> = promise(Promise.resolve([1, 2, 3, 4, 5]));
|
|
230
425
|
|
|
231
|
-
//
|
|
426
|
+
// 创建一个范围流
|
|
232
427
|
let rangeStream = range(1, 10, 2); // 1, 3, 5, 7, 9
|
|
233
428
|
|
|
234
429
|
// WebSocket 事件流
|
|
235
430
|
let ws = new WebSocket("ws://localhost:8080");
|
|
236
431
|
websocket(ws)
|
|
237
|
-
.filter((event): boolean => event.type === "message"); //
|
|
238
|
-
.toUnordered() //
|
|
432
|
+
.filter((event): boolean => event.type === "message"); // 只监听消息事件
|
|
433
|
+
.toUnordered() // 通常事件不需要有序
|
|
239
434
|
.forEach((event): void => receive(event)); // 接收消息
|
|
240
435
|
```
|
|
241
436
|
|
|
242
437
|
## Semantic 类方法
|
|
243
438
|
|
|
244
439
|
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
245
|
-
|
|
246
|
-
| `concat(other)
|
|
247
|
-
| `
|
|
248
|
-
| `distinct(
|
|
249
|
-
| `
|
|
250
|
-
| `
|
|
251
|
-
| `
|
|
252
|
-
| `
|
|
253
|
-
| `
|
|
254
|
-
| `
|
|
255
|
-
| `
|
|
256
|
-
| `
|
|
257
|
-
| `
|
|
258
|
-
| `
|
|
259
|
-
| `
|
|
260
|
-
| `
|
|
261
|
-
| `
|
|
262
|
-
| `
|
|
263
|
-
| `
|
|
264
|
-
| `
|
|
265
|
-
| `
|
|
266
|
-
| `
|
|
440
|
+
|--------|-------------|-----------------|------------------|
|
|
441
|
+
| `concat(other: Semantic<E>): Semantic<E>` | 连接两个语义流 | O(m+n) | O(1) |
|
|
442
|
+
| `concat(other: Iterable<E>): Semantic<E>` | 将语义流与可迭代对象连接 | O(m+n) | O(1) |
|
|
443
|
+
| `distinct(): Semantic<E>` | 返回不同的元素 | O(n) | O(n) |
|
|
444
|
+
| `distinct<K>(keyExtractor: Functional<E, K>): Semantic<E>` | 按键返回不同的元素 | O(n) | O(n) |
|
|
445
|
+
| `distinct<K>(keyExtractor: BiFunctional<E, bigint, K>): Semantic<E>` | 按键(带索引)返回不同的元素 | O(n) | O(n) |
|
|
446
|
+
| `dropWhile(predicate: Predicate<E>): Semantic<E>` | 在断言为真时丢弃元素 | O(n) | O(1) |
|
|
447
|
+
| `dropWhile(predicate: BiPredicate<E, bigint>): Semantic<E>` | 在断言为真时丢弃元素(带索引) | O(n) | O(1) |
|
|
448
|
+
| `filter(predicate: Predicate<E>): Semantic<E>` | 按断言过滤元素 | O(n) | O(1) |
|
|
449
|
+
| `filter(predicate: BiPredicate<E, bigint>): Semantic<E>` | 按断言过滤元素(带索引) | O(n) | O(1) |
|
|
450
|
+
| `flat(mapper: Functional<E, Iterable<E>>): Semantic<E>` | 展平可迭代结果 | O(n) | O(1) |
|
|
451
|
+
| `flat(mapper: BiFunctional<E, bigint, Iterable<E>>): Semantic<E>` | 展平可迭代结果(带索引) | O(n) | O(1) |
|
|
452
|
+
| `flat(mapper: Functional<E, Semantic<E>>): Semantic<E>` | 展平语义结果 | O(n) | O(1) |
|
|
453
|
+
| `flat(mapper: BiFunctional<E, bigint, Semantic<E>>): Semantic<E>` | 展平语义结果(带索引) | O(n) | O(1) |
|
|
454
|
+
| `flatMap<R>(mapper: Functional<E, Iterable<R>>): Semantic<R>` | 映射并展平为不同类型 | O(n) | O(1) |
|
|
455
|
+
| `flatMap<R>(mapper: BiFunctional<E, bigint, Iterable<R>>): Semantic<R>` | 映射并展平(带索引) | O(n) | O(1) |
|
|
456
|
+
| `flatMap<R>(mapper: Functional<E, Semantic<R>>): Semantic<R>` | 映射并展平语义 | O(n) | O(1) |
|
|
457
|
+
| `flatMap<R>(mapper: BiFunctional<E, bigint, Semantic<R>>): Semantic<R>` | 映射并展平语义(带索引) | O(n) | O(1) |
|
|
458
|
+
| `limit(n: number): Semantic<E>` | 限制元素数量 | O(n) | O(1) |
|
|
459
|
+
| `limit(n: bigint): Semantic<E>` | 限制元素数量 (bigint) | O(n) | O(1) |
|
|
460
|
+
| `map<R>(mapper: Functional<E, R>): Semantic<R>` | 将元素映射到不同类型 | O(n) | O(1) |
|
|
461
|
+
| `map<R>(mapper: BiFunctional<E, bigint, R>): Semantic<R>` | 将元素映射(带索引) | O(n) | O(1) |
|
|
462
|
+
| `peek(consumer: Consumer<E>): Semantic<E>` | 对元素执行操作 | O(n) | O(1) |
|
|
463
|
+
| `peek(consumer: BiConsumer<E, bigint>): Semantic<E>` | 对元素执行操作(带索引) | O(n) | O(1) |
|
|
464
|
+
| `redirect(redirector: BiFunctional<E, bigint, bigint>): Semantic<E>` | 重定向元素索引 | O(n) | O(1) |
|
|
465
|
+
| `reverse(): Semantic<E>` | 反转元素索引 | O(n) | O(1) |
|
|
466
|
+
| `shuffle(): Semantic<E>` | 随机打乱元素索引 | O(n) | O(1) |
|
|
467
|
+
| `shuffle(mapper: BiFunctional<E, bigint, bigint>): Semantic<E>` | 使用映射器打乱元素索引 | O(n) | O(1) |
|
|
468
|
+
| `skip(n: number): Semantic<E>` | 跳过前 n 个元素 | O(n) | O(1) |
|
|
469
|
+
| `skip(n: bigint): Semantic<E>` | 跳过前 n 个元素 (bigint) | O(n) | O(1) |
|
|
470
|
+
| `sub(start: bigint, end: bigint): Semantic<E>` | 返回语义流子范围 | O(n) | O(1) |
|
|
471
|
+
| `takeWhile(predicate: Predicate<E>): Semantic<E>` | 在断言为真时获取元素 | O(n) | O(1) |
|
|
472
|
+
| `takeWhile(predicate: BiPredicate<E, bigint>): Semantic<E>` | 在断言为真时获取元素(带索引) | O(n) | O(1) |
|
|
473
|
+
| `translate(offset: number): Semantic<E>` | 将元素索引平移偏移量 | O(n) | O(1) |
|
|
474
|
+
| `translate(offset: bigint): Semantic<E>` | 将元素索引平移偏移量 (bigint) | O(n) | O(1) |
|
|
475
|
+
| `translate(translator: BiFunctional<E, bigint, bigint>): Semantic<E>` | 使用转换器平移元素索引 | O(n) | O(1) |
|
|
267
476
|
|
|
268
477
|
```typescript
|
|
269
|
-
//
|
|
478
|
+
// 语义流操作示例
|
|
270
479
|
let result = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
|
271
480
|
.filter((n: number): boolean => n % 2 === 0) // 过滤偶数
|
|
272
481
|
.map((n: number): number => n * 2) // 乘以 2
|
|
@@ -278,10 +487,10 @@ let result = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
|
|
278
487
|
|
|
279
488
|
// 复杂操作示例
|
|
280
489
|
let complexResult = range(1, 100, 1)
|
|
281
|
-
.flatMap((n: number): Semantics<number> => from([n, n * 2])) //
|
|
490
|
+
.flatMap((n: number): Semantics<number> => from([n, n * 2])) // 将每个元素映射为两个
|
|
282
491
|
.distinct() // 去重
|
|
283
492
|
.shuffle() // 打乱顺序
|
|
284
|
-
.takeWhile((n: number): boolean => n < 50) //
|
|
493
|
+
.takeWhile((n: number): boolean => n < 50) // 获取小于 50 的元素
|
|
285
494
|
.toOrdered() // 转换为有序收集器
|
|
286
495
|
.toArray(); // 转换为数组
|
|
287
496
|
```
|
|
@@ -290,94 +499,132 @@ let complexResult = range(1, 100, 1)
|
|
|
290
499
|
|
|
291
500
|
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
292
501
|
|------------|------------|------------|------------|
|
|
293
|
-
| `sorted()
|
|
294
|
-
| `
|
|
295
|
-
| `
|
|
296
|
-
| `
|
|
297
|
-
| `toBigintStatistics()
|
|
298
|
-
| `
|
|
299
|
-
| `
|
|
300
|
-
| `
|
|
502
|
+
| `sorted(): OrderedCollectable<E>` | 返回排序后的可收集流 | O(n log n) | O(n) |
|
|
503
|
+
| `sorted(comparator: Comparator<E>): OrderedCollectable<E>` | 使用比较器返回排序后的可收集流 | O(n log n) | O(n) |
|
|
504
|
+
| `toCollectable(): Collectable<E>` | 转换为可收集流 | O(1) | O(1) |
|
|
505
|
+
| `toCollectable<C extends Collectable<E>>(mapper: Functional<Generator<E>, C>): C` | 使用映射器转换为可收集流 | O(1) | O(1) |
|
|
506
|
+
| `toBigintStatistics(): BigIntStatistics<E>` | 转换为 bigint 统计信息 | O(1) | O(1) |
|
|
507
|
+
| `toNumericStatistics(): NumericStatistics<E>` | 转换为数字统计信息 | O(1) | O(1) |
|
|
508
|
+
| `toOrdered(): OrderedCollectable<E>` | 转换为有序可收集流 | O(1) | O(1) |
|
|
509
|
+
| `toUnordered(): UnorderedCollectable<E>` | 转换为无序可收集流 | O(1) | O(1) |
|
|
510
|
+
| `toWindow(): WindowCollectable<E>` | 转换为窗口可收集流 | O(1) | O(1) |
|
|
301
511
|
|
|
302
512
|
```typescript
|
|
303
|
-
//
|
|
304
|
-
from([6,4,3,5,2]) //
|
|
305
|
-
.sorted() //
|
|
513
|
+
// 转换为升序排序的数组
|
|
514
|
+
from([6,4,3,5,2]) // 创建一个流
|
|
515
|
+
.sorted() // 将流按升序排序
|
|
306
516
|
.toArray(); // [2, 3, 4, 5, 6]
|
|
307
517
|
|
|
308
|
-
//
|
|
309
|
-
from([6,4,3,5,2]) //
|
|
310
|
-
.
|
|
518
|
+
// 转换为降序排序的数组
|
|
519
|
+
from([6,4,3,5,2]) // 创建一个流
|
|
520
|
+
.sorted((a: number, b: number): number => b - a) // 将流按降序排序
|
|
311
521
|
.toArray(); // [6, 5, 4, 3, 2]
|
|
312
522
|
|
|
313
|
-
//
|
|
523
|
+
// 重定向为反转的数组
|
|
314
524
|
from([6,4,3,5,2])
|
|
315
|
-
.redirect((element, index): bigint => -index) //
|
|
316
|
-
.
|
|
525
|
+
.redirect((element, index): bigint => -index) // 重定向为反转顺序
|
|
526
|
+
.toOrdered() // 保持重定向的顺序
|
|
317
527
|
.toArray(); // [2, 5, 3, 4, 6]
|
|
318
528
|
|
|
319
|
-
//
|
|
529
|
+
// 忽略重定向,反转数组
|
|
320
530
|
from([6,4,3,5,2])
|
|
321
|
-
.redirect((element: number, index: bigint) => -index) //
|
|
322
|
-
.
|
|
531
|
+
.redirect((element: number, index: bigint) => -index) // 重定向为反转顺序
|
|
532
|
+
.toUnordered() // 丢弃重定向的顺序。此操作将忽略 `redirect`、`reverse`、`shuffle` 和 `translate` 操作
|
|
323
533
|
.toArray(); // [2, 5, 3, 4, 6]
|
|
324
534
|
|
|
325
|
-
//
|
|
535
|
+
// 将流反转为数组
|
|
326
536
|
from([6, 4, 3, 5, 2])
|
|
327
537
|
.reverse() // 反转流
|
|
328
|
-
.toOrdered() //
|
|
538
|
+
.toOrdered() // 保证反转的顺序
|
|
329
539
|
.toArray(); // [2, 5, 3, 4, 6]
|
|
330
540
|
|
|
331
|
-
//
|
|
541
|
+
// 覆盖已打乱的流为数组
|
|
332
542
|
from([6, 4, 3, 5, 2])
|
|
333
543
|
.shuffle() // 打乱流
|
|
334
|
-
.sorted() //
|
|
544
|
+
.sorted() // 覆盖已打乱的顺序。此操作将覆盖 `redirect`、`reverse`、`shuffle` 和 `translate` 操作
|
|
335
545
|
.toArray(); // [2, 5, 3, 4, 6]
|
|
336
546
|
|
|
337
547
|
// 转换为窗口收集器
|
|
338
548
|
from([6, 4, 3, 5, 2]).toWindow();
|
|
339
549
|
|
|
340
|
-
//
|
|
550
|
+
// 转换为数字统计信息
|
|
341
551
|
from([6, 4, 3, 5, 2]).toNumericStatistics();
|
|
342
552
|
|
|
343
|
-
// 转换为
|
|
553
|
+
// 转换为 bigint 统计信息
|
|
344
554
|
from([6n, 4n, 3n, 5n, 2n]).toBigintStatistics();
|
|
345
555
|
|
|
346
|
-
//
|
|
556
|
+
// 定义自定义收集器以收集数据
|
|
347
557
|
let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Generator<E>) => new CustomizedCollector(generator));
|
|
348
558
|
```
|
|
349
559
|
|
|
350
|
-
## Collectable
|
|
560
|
+
## Collectable 收集方法
|
|
351
561
|
|
|
352
562
|
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
353
|
-
|
|
354
|
-
| `anyMatch(predicate)` |
|
|
355
|
-
| `allMatch(predicate)` |
|
|
356
|
-
| `
|
|
357
|
-
| `
|
|
358
|
-
| `
|
|
359
|
-
| `
|
|
360
|
-
| `
|
|
361
|
-
| `
|
|
362
|
-
| `
|
|
363
|
-
| `
|
|
364
|
-
| `
|
|
365
|
-
| `
|
|
366
|
-
| `
|
|
367
|
-
| `
|
|
368
|
-
| `
|
|
369
|
-
| `
|
|
370
|
-
| `
|
|
371
|
-
| `
|
|
372
|
-
| `
|
|
373
|
-
| `
|
|
374
|
-
| `
|
|
563
|
+
|--------|-------------|-----------------|------------------|
|
|
564
|
+
| `anyMatch(predicate: Predicate<E>): boolean` | 如果有任何元素匹配断言则返回 true | O(n) | O(1) |
|
|
565
|
+
| `allMatch(predicate: Predicate<E>): boolean` | 如果所有元素都匹配断言则返回 true | O(n) | O(1) |
|
|
566
|
+
| `collect<A, R>(collector: Collector<E, A, R>): R` | 使用收集器收集元素 | O(n) | O(1) |
|
|
567
|
+
| `collect<A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R` | 使用累加器和最终器收集元素 | O(n) | O(1) |
|
|
568
|
+
| `collect<A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R` | 使用支持索引感知的累加器收集元素 | O(n) | O(1) |
|
|
569
|
+
| `collect<A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R` | 使用中断器和累加器收集 | O(n) | O(1) |
|
|
570
|
+
| `collect<A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R` | 使用支持索引感知的中断器收集 | O(n) | O(1) |
|
|
571
|
+
| `collect<A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R` | 使用支持状态感知的中断器收集 | O(n) | O(1) |
|
|
572
|
+
| `collect<A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R` | 使用支持索引感知的累加器收集 | O(n) | O(1) |
|
|
573
|
+
| `collect<A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R` | 使用两者都支持索引感知的收集 | O(n) | O(1) |
|
|
574
|
+
| `collect<A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R` | 使用支持状态和索引感知的收集 | O(n) | O(1) |
|
|
575
|
+
| `count(): bigint` | 计算元素数量 | O(n) | O(1) |
|
|
576
|
+
| `error(): void` | 将元素记录到 console.error | O(n) | O(1) |
|
|
577
|
+
| `error(accumulator: BiFunctional<string, E, string>): void` | 使用自定义累加器记录 | O(n) | O(1) |
|
|
578
|
+
| `error(accumulator: TriFunctional<string, E, bigint, string>): void` | 使用支持索引感知的累加器记录 | O(n) | O(1) |
|
|
579
|
+
| `error(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): void` | 使用前缀和后缀记录 | O(n) | O(1) |
|
|
580
|
+
| `error(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): void` | 使用支持索引感知的累加器记录 | O(n) | O(1) |
|
|
581
|
+
| `isEmpty(): boolean` | 如果没有元素则返回 true | O(1) | O(1) |
|
|
582
|
+
| `findAny(): Optional<E>` | 随机返回任意元素 | O(1) | O(1) |
|
|
583
|
+
| `findFirst(): Optional<E>` | 返回第一个元素 | O(1) | O(1) |
|
|
584
|
+
| `findLast(): Optional<E>` | 返回最后一个元素 | O(n) | O(1) |
|
|
585
|
+
| `findMaximum(): Optional<E>` | 返回最大元素 | O(n) | O(1) |
|
|
586
|
+
| `findMaximum(comparator: Comparator<E>): Optional<E>` | 使用比较器返回最大值 | O(n) | O(1) |
|
|
587
|
+
| `findMinimum(): Optional<E>` | 返回最小元素 | O(n) | O(1) |
|
|
588
|
+
| `findMinimum(comparator: Comparator<E>): Optional<E>` | 使用比较器返回最小值 | O(n) | O(1) |
|
|
589
|
+
| `forEach(action: Consumer<E>): void` | 对每个元素执行操作 | O(n) | O(1) |
|
|
590
|
+
| `forEach(action: BiConsumer<E, bigint>): void` | 对每个元素执行操作(带索引) | O(n) | O(1) |
|
|
591
|
+
| `group<K>(classifier: Functional<E, K>): Map<K, Array<E>>` | 按分类器对元素分组 | O(n) | O(n) |
|
|
592
|
+
| `groupBy<K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Map<K, Array<V>>` | 使用键值映射对元素分组 | O(n) | O(n) |
|
|
593
|
+
| `join(): string` | 将元素连接成字符串 | O(n) | O(1) |
|
|
594
|
+
| `join(delimiter: string): string` | 使用分隔符连接 | O(n) | O(1) |
|
|
595
|
+
| `join(prefix: string, delimiter: string, suffix: string): string` | 使用前缀和后缀连接 | O(n) | O(1) |
|
|
596
|
+
| `join(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): string` | 使用自定义累加器连接 | O(n) | O(1) |
|
|
597
|
+
| `join(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): string` | 使用支持索引感知的累加器连接 | O(n) | O(1) |
|
|
598
|
+
| `log(): void` | 将元素记录到 console.log | O(n) | O(1) |
|
|
599
|
+
| `log(accumulator: BiFunctional<string, E, string>): void` | 使用自定义累加器记录 | O(n) | O(1) |
|
|
600
|
+
| `log(accumulator: TriFunctional<string, E, bigint, string>): void` | 使用支持索引感知的累加器记录 | O(n) | O(1) |
|
|
601
|
+
| `log(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): void` | 使用前缀和后缀记录 | O(n) | O(1) |
|
|
602
|
+
| `log(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): void` | 使用支持索引感知的累加器记录 | O(n) | O(1) |
|
|
603
|
+
| `nonMatch(predicate: Predicate<E>): boolean` | 如果没有元素匹配则返回 true | O(n) | O(1) |
|
|
604
|
+
| `partition(count: bigint): Array<Array<E>>` | 将元素按大小分组 | O(n) | O(n) |
|
|
605
|
+
| `partitionBy(classifier: Functional<E, bigint>): Array<Array<E>>` | 按分类器对元素分区 | O(n) | O(n) |
|
|
606
|
+
| `reduce(accumulator: BiFunctional<E, E, E>): Optional<E>` | 归约元素 | O(n) | O(1) |
|
|
607
|
+
| `reduce(accumulator: TriFunctional<E, E, bigint, E>): Optional<E>` | 归约元素(带索引) | O(n) | O(1) |
|
|
608
|
+
| `reduce(identity: E, accumulator: BiFunctional<E, E, E>): E` | 带恒等值的归约 | O(n) | O(1) |
|
|
609
|
+
| `reduce(identity: E, accumulator: TriFunctional<E, E, bigint, E>): E` | 带恒等值和索引的归约 | O(n) | O(1) |
|
|
610
|
+
| `reduce<R>(identity: R, accumulator: BiFunctional<R, E, R>, finisher: Functional<R, R>): R` | 带最终器的归约 | O(n) | O(1) |
|
|
611
|
+
| `reduce<R>(identity: R, accumulator: TriFunctional<R, E, bigint, R>, finisher: Functional<R, R>): R` | 带索引和最终器的归约 | O(n) | O(1) |
|
|
612
|
+
| `semantic(): Semantic<E>` | 转换为语义流 | O(1) | O(1) |
|
|
613
|
+
| `source(): Generator<E>` | 返回生成器源 | O(1) | O(1) |
|
|
614
|
+
| `toArray(): Array<E>` | 转换为数组 | O(n) | O(n) |
|
|
615
|
+
| `toMap<K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Map<K, V>` | 转换为映射 | O(n) | O(n) |
|
|
616
|
+
| `toSet(): Set<E>` | 转换为集合 | O(n) | O(n) |
|
|
617
|
+
| `write<S = string>(stream: WritableStream<S>): Promise<WritableStream<S>>` | 将元素写入可写流 | O(n) | O(1) |
|
|
618
|
+
| `write<S = string>(stream: WritableStream<S>, accumulator: BiFunctional<WritableStream<S>, E, WritableStream<S>>): Promise<WritableStream<S>>` | 使用自定义累加器写入流 | O(n) | O(1) |
|
|
619
|
+
| `write<S = string>(stream: WritableStream<S>, accumulator: TriFunctional<WritableStream<S>, E, bigint, WritableStream<S>>): Promise<WritableStream<S>>` | 使用支持索引感知的累加器写入流 | O(n) | O(1) |
|
|
375
620
|
|
|
376
621
|
```typescript
|
|
377
622
|
// Collectable 操作示例
|
|
378
|
-
|
|
623
|
+
let data = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
|
379
624
|
.filter((n: number): boolean => n % 2 === 0)
|
|
380
|
-
.toOrdered();
|
|
625
|
+
.toOrdered();
|
|
626
|
+
|
|
627
|
+
// 匹配检查
|
|
381
628
|
console.log(data.anyMatch((n: number): boolean => n > 5)); // true
|
|
382
629
|
console.log(data.allMatch((n: number): boolean => n < 20)); // true
|
|
383
630
|
|
|
@@ -386,101 +633,376 @@ data.findFirst().ifPresent((n: number): void => console.log(n)); // 2
|
|
|
386
633
|
data.findAny().ifPresent((n: number): void => console.log(n)); // 任意元素
|
|
387
634
|
|
|
388
635
|
// 分组操作
|
|
389
|
-
|
|
390
|
-
(n: number): string =>
|
|
636
|
+
let grouped = data.groupBy(
|
|
637
|
+
(n: number): string => n > 5 ? "large" : "small",
|
|
391
638
|
(n: number): number => n * 2
|
|
392
|
-
);
|
|
639
|
+
);
|
|
640
|
+
// Map { "small" => [4, 8], "large" => [12, 16, 20] }
|
|
393
641
|
|
|
394
642
|
// 归约操作
|
|
395
|
-
|
|
643
|
+
let sum = data.reduce(0, (accumulator: number, n: number): number => accumulator + n); // 30
|
|
396
644
|
|
|
397
645
|
// 输出操作
|
|
398
|
-
data.join(", "); // "
|
|
646
|
+
data.join(", "); // "2, 4, 6, 8, 10"
|
|
647
|
+
|
|
648
|
+
// 写入流操作
|
|
649
|
+
let writableStream = new WritableStream<string>({
|
|
650
|
+
write(chunk) {
|
|
651
|
+
console.log('写入:', chunk);
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
data.write(writableStream)
|
|
655
|
+
.then(() => console.log('写入完成'))
|
|
656
|
+
.catch((err) => console.error('写入失败:', err));
|
|
399
657
|
```
|
|
400
658
|
|
|
401
|
-
##
|
|
659
|
+
## UnorderedCollectable 类方法
|
|
402
660
|
|
|
403
|
-
|
|
661
|
+
`UnorderedCollectable<E>` 是 `Collectable<E>` 的一种实现,它在处理流时**不保证**元素的顺序,从而在不需要顺序的场景下(例如,仅计算元素数量、总和、是否存在等)可以提供最佳性能。它会自动忽略 `redirect`、`reverse`、`shuffle` 和 `translate` 等会影响顺序的操作。
|
|
404
662
|
|
|
405
663
|
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
406
|
-
|
|
407
|
-
| `
|
|
408
|
-
| `
|
|
409
|
-
|
|
|
410
|
-
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
664
|
+
|--------|-------------|-----------------|------------------|
|
|
665
|
+
| `constructor(generator: Generator<E>)` | 创建无序可收集流实例 | O(1) | O(1) |
|
|
666
|
+
| `source(): Generator<E>` | 返回生成器源 | O(1) | O(1) |
|
|
667
|
+
| `: Generator<E, void, undefined>` | 返回迭代器 | O(n) | O(1) |
|
|
668
|
+
| `: AsyncGenerator<E, void, undefined>` | 返回异步迭代器 | O(n) | O(1) |
|
|
669
|
+
|
|
670
|
+
```typescript
|
|
671
|
+
// 创建并操作 UnorderedCollectable
|
|
672
|
+
let unorderedData: UnorderedCollectable<number> = from([5, 3, 8, 1, 4])
|
|
673
|
+
.filter(n => n > 2)
|
|
674
|
+
.toUnordered(); // 转换为无序收集器以获得最佳性能
|
|
675
|
+
|
|
676
|
+
console.log(unorderedData.anyMatch(n => n > 6)); // true
|
|
677
|
+
console.log(unorderedData.count()); // 4n
|
|
678
|
+
console.log(unorderedData.toArray()); // 元素顺序不一定为 [5, 3, 8, 4]
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
## OrderedCollectable 类方法
|
|
682
|
+
|
|
683
|
+
`OrderedCollectable<E>` 是 `Collectable<E>` 的一种实现,它**严格保证**元素的顺序。当您调用 `.sorted()` 方法时,会自动创建一个 `OrderedCollectable` 实例。它会尊重并维护 `redirect`、`reverse`、`shuffle` 和 `translate` 等操作产生的顺序,但 `sorted()` 操作会覆盖之前所有的顺序定义。
|
|
684
|
+
|
|
685
|
+
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
686
|
+
|--------|-------------|-----------------|------------------|
|
|
687
|
+
| `constructor(generator: Generator<E>)` | 创建有序可收集流实例 | O(n log n) | O(n) |
|
|
688
|
+
| `constructor(generator: Generator<E>, comparator: Comparator<E>)` | 使用比较器创建有序可收集流实例 | O(n log n) | O(n) |
|
|
689
|
+
| `: Generator<E, void, undefined>` | 返回迭代器 | O(n) | O(1) |
|
|
690
|
+
| `: AsyncGenerator<E, void, undefined>` | 返回异步迭代器 | O(n) | O(1) |
|
|
691
|
+
| `source(): Generator<E>` | 返回索引化后的生成器源 | O(1) | O(1) |
|
|
692
|
+
| `isEmpty(): boolean` | 如果缓冲区为空则返回 true | O(1) | O(1) |
|
|
693
|
+
|
|
694
|
+
```typescript
|
|
695
|
+
// 创建并操作 OrderedCollectable
|
|
696
|
+
let orderedData: OrderedCollectable<number> = from([5, 3, 8, 1, 4])
|
|
697
|
+
.sorted(); // 自动创建 OrderedCollectable 并进行升序排序
|
|
698
|
+
|
|
699
|
+
console.log(orderedData.toArray()); // [1, 3, 4, 5, 8]
|
|
700
|
+
|
|
701
|
+
// 使用自定义比较器进行降序排序
|
|
702
|
+
let descendingData: OrderedCollectable<number> = from([5, 3, 8, 1, 4])
|
|
703
|
+
.sorted((a, b) => b - a);
|
|
704
|
+
|
|
705
|
+
console.log(descendingData.toArray()); // [8, 5, 4, 3, 1]
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
## 统计分析类方法
|
|
709
|
+
|
|
710
|
+
### Statistics 类方法
|
|
711
|
+
|
|
712
|
+
`Statistics<E, D extends number | bigint>` 是一个泛型基类,用于对元素流进行统计分析。`NumericStatistics` 和 `BigIntStatistics` 都继承自此基类。
|
|
713
|
+
|
|
714
|
+
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
715
|
+
|--------|-------------|-----------------|------------------|
|
|
716
|
+
| `constructor(generator: Generator<E>)` | 创建一个统计实例 | O(n log n) | O(n) |
|
|
717
|
+
| `constructor(generator: Generator<E>, comparator: Comparator<E>)` | 使用比较器创建一个统计实例 | O(n log n) | O(n) |
|
|
718
|
+
| `: Generator<E, void, undefined>` | 返回迭代器 | O(n) | O(1) |
|
|
719
|
+
| `: AsyncGenerator<E, void, undefined>` | 返回异步迭代器 | O(n) | O(1) |
|
|
720
|
+
| `count(): bigint` | 返回元素数量 | O(1) | O(1) |
|
|
721
|
+
| `average(): D` | 返回平均值(算术平均) | O(n) | O(1) |
|
|
722
|
+
| `average(mapper: Functional<E, D>): D` | 返回映射值的平均值 | O(n) | O(1) |
|
|
723
|
+
| `range(): D` | 返回极差(最大值-最小值) | O(1) | O(1) |
|
|
724
|
+
| `range(mapper: Functional<E, D>): D` | 返回映射值的极差 | O(1) | O(1) |
|
|
725
|
+
| `variance(): D` | 返回方差 | O(n) | O(1) |
|
|
726
|
+
| `variance(mapper: Functional<E, D>): D` | 返回映射值的方差 | O(n) | O(1) |
|
|
727
|
+
| `standardDeviation(): D` | 返回标准差 | O(n) | O(1) |
|
|
728
|
+
| `standardDeviation(mapper: Functional<E, D>): D` | 返回映射值的标准差 | O(n) | O(1) |
|
|
729
|
+
| `mean(): D` | 返回均值(同 average) | O(n) | O(1) |
|
|
730
|
+
| `mean(mapper: Functional<E, D>): D` | 返回映射值的均值 | O(n) | O(1) |
|
|
731
|
+
| `median(): D` | 返回中位数 | O(n) | O(n) |
|
|
732
|
+
| `median(mapper: Functional<E, D>): D` | 返回映射值的中位数 | O(n) | O(n) |
|
|
733
|
+
| `mode(): D` | 返回众数 | O(n) | O(n) |
|
|
734
|
+
| `mode(mapper: Functional<E, D>): D` | 返回映射值的众数 | O(n) | O(n) |
|
|
735
|
+
| `frequency(): Map<E, bigint>` | 返回频率分布 | O(n) | O(n) |
|
|
736
|
+
| `summate(): D` | 返回元素总和 | O(n) | O(1) |
|
|
737
|
+
| `summate(mapper: Functional<E, D>): D` | 返回映射值的总和 | O(n) | O(1) |
|
|
738
|
+
| `quantile(quantile: number): D` | 返回指定分位数 | O(1) | O(1) |
|
|
739
|
+
| `quantile(quantile: number, mapper: Functional<E, D>): D` | 返回映射值的指定分位数 | O(1) | O(1) |
|
|
740
|
+
| `interquartileRange(): D` | 返回四分位距 (IQR) | O(1) | O(1) |
|
|
741
|
+
| `interquartileRange(mapper: Functional<E, D>): D` | 返回映射值的四分位距 | O(1) | O(1) |
|
|
742
|
+
| `skewness(): D` | 返回偏度 | O(n) | O(1) |
|
|
743
|
+
| `skewness(mapper: Functional<E, D>): D` | 返回映射值的偏度 | O(n) | O(1) |
|
|
744
|
+
| `kurtosis(): D` | 返回峰度 | O(n) | O(1) |
|
|
745
|
+
| `kurtosis(mapper: Functional<E, D>): D` | 返回映射值的峰度 | O(n) | O(1) |
|
|
746
|
+
|
|
747
|
+
### NumericStatistics 类方法
|
|
748
|
+
|
|
749
|
+
`NumericStatistics<E>` 继承自 `Statistics<E, number>`,专门用于处理数值类型 (`number`) 的统计分析。
|
|
750
|
+
|
|
751
|
+
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
752
|
+
|--------|-------------|-----------------|------------------|
|
|
753
|
+
| `constructor(generator: Generator<E>)` | 创建一个数字统计实例 | O(n log n) | O(n) |
|
|
754
|
+
| `constructor(generator: Generator<E>, comparator: Comparator<E>)` | 使用比较器创建一个数字统计实例 | O(n log n) | O(n) |
|
|
755
|
+
| `: Generator<E, void, undefined>` | 返回迭代器 | O(n) | O(1) |
|
|
756
|
+
| `: AsyncGenerator<E, void, undefined>` | 返回异步迭代器 | O(n) | O(1) |
|
|
757
|
+
| `count(): bigint` | 返回元素数量 | O(1) | O(1) |
|
|
758
|
+
| `average(): number` | 返回数字平均值 | O(n) | O(1) |
|
|
759
|
+
| `average(mapper: Functional<E, number>): number` | 返回映射数字值的平均值 | O(n) | O(1) |
|
|
760
|
+
| `range(): number` | 返回数字极差 | O(1) | O(1) |
|
|
761
|
+
| `range(mapper: Functional<E, number>): number` | 返回映射数字值的极差 | O(1) | O(1) |
|
|
762
|
+
| `variance(): number` | 返回数字方差 | O(n) | O(1) |
|
|
763
|
+
| `variance(mapper: Functional<E, number>): number` | 返回映射数字值的方差 | O(n) | O(1) |
|
|
764
|
+
| `standardDeviation(): number` | 返回数字标准差 | O(n) | O(1) |
|
|
765
|
+
| `standardDeviation(mapper: Functional<E, number>): number` | 返回映射数字值的标准差 | O(n) | O(1) |
|
|
766
|
+
| `mean(): number` | 返回数字均值(同 average) | O(n) | O(1) |
|
|
767
|
+
| `mean(mapper: Functional<E, number>): number` | 返回映射数字值的均值 | O(n) | O(1) |
|
|
768
|
+
| `median(): number` | 返回数字中位数 | O(n) | O(n) |
|
|
769
|
+
| `median(mapper: Functional<E, number>): number` | 返回映射数字值的中位数 | O(n) | O(n) |
|
|
770
|
+
| `mode(): number` | 返回数字众数 | O(n) | O(n) |
|
|
771
|
+
| `mode(mapper: Functional<E, number>): number` | 返回映射数字值的众数 | O(n) | O(n) |
|
|
772
|
+
| `frequency(): Map<E, bigint>` | 返回频率分布 | O(n) | O(n) |
|
|
773
|
+
| `summate(): number` | 返回数字总和 | O(n) | O(1) |
|
|
774
|
+
| `summate(mapper: Functional<E, number>): number` | 返回映射数字值的总和 | O(n) | O(1) |
|
|
775
|
+
| `quantile(quantile: number): number` | 返回指定数字分位数 | O(1) | O(1) |
|
|
776
|
+
| `quantile(quantile: number, mapper: Functional<E, number>): number` | 返回映射数字值的指定分位数 | O(1) | O(1) |
|
|
777
|
+
| `interquartileRange(): number` | 返回数字四分位距 | O(1) | O(1) |
|
|
778
|
+
| `interquartileRange(mapper: Functional<E, number>): number` | 返回映射数字值的四分位距 | O(1) | O(1) |
|
|
779
|
+
| `skewness(): number` | 返回数字偏度 | O(n) | O(1) |
|
|
780
|
+
| `skewness(mapper: Functional<E, number>): number` | 返回映射数字值的偏度 | O(n) | O(1) |
|
|
781
|
+
| `kurtosis(): number` | 返回数字峰度 | O(n) | O(1) |
|
|
782
|
+
| `kurtosis(mapper: Functional<E, number>): number` | 返回映射数字值的峰度 | O(n) | O(1) |
|
|
419
783
|
|
|
420
784
|
```typescript
|
|
421
|
-
//
|
|
422
|
-
|
|
785
|
+
// 数字统计分析示例
|
|
786
|
+
let numbers = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
|
423
787
|
.toNumericStatistics();
|
|
424
|
-
|
|
788
|
+
|
|
789
|
+
console.log("均值:", numbers.mean()); // 5.5
|
|
425
790
|
console.log("中位数:", numbers.median()); // 5.5
|
|
426
|
-
console.log("标准差:", numbers.standardDeviation()); // ~2.87
|
|
791
|
+
console.log("标准差:", numbers.standardDeviation().toFixed(2)); // ~2.87
|
|
427
792
|
console.log("总和:", numbers.summate()); // 55
|
|
428
|
-
|
|
429
|
-
//
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
793
|
+
console.log("极差:", numbers.range()); // 9
|
|
794
|
+
console.log("四分位距 (IQR):", numbers.interquartileRange()); // 5
|
|
795
|
+
console.log("下四分位数:", numbers.quantile(0.25)); // 3.25
|
|
796
|
+
console.log("偏度:", numbers.skewness().toFixed(2)); // 0
|
|
797
|
+
console.log("峰度:", numbers.kurtosis().toFixed(2)); // ~-1.2
|
|
798
|
+
|
|
799
|
+
// 对对象数组进行统计
|
|
800
|
+
let objects = from([
|
|
801
|
+
{ value: 10, weight: 2 },
|
|
802
|
+
{ value: 20, weight: 3 },
|
|
803
|
+
{ value: 30, weight: 5 }
|
|
434
804
|
]).toNumericStatistics();
|
|
435
|
-
|
|
805
|
+
|
|
806
|
+
console.log("对象值的平均值:", objects.average(obj => obj.value)); // 20
|
|
807
|
+
console.log("对象值的总和:", objects.summate(obj => obj.value)); // 60
|
|
808
|
+
console.log("对象值的中位数:", objects.median(obj => obj.value)); // 20
|
|
809
|
+
console.log("加权平均值:", objects.summate(obj => obj.value * obj.weight) / objects.summate(obj => obj.weight)); // 24.0
|
|
810
|
+
```
|
|
811
|
+
|
|
812
|
+
### BigIntStatistics 类方法
|
|
813
|
+
|
|
814
|
+
`BigIntStatistics<E>` 继承自 `Statistics<E, bigint>`,专门用于处理大整数类型 (`bigint`) 的统计分析,避免在计算过程中因数值过大而溢出。
|
|
815
|
+
|
|
816
|
+
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
817
|
+
|--------|-------------|-----------------|------------------|
|
|
818
|
+
| `constructor(generator: Generator<E>)` | 创建一个 bigint 统计实例 | O(n log n) | O(n) |
|
|
819
|
+
| `constructor(generator: Generator<E>, comparator: Comparator<E>)` | 使用比较器创建一个 bigint 统计实例 | O(n log n) | O(n) |
|
|
820
|
+
| `: Generator<E, void, undefined>` | 返回迭代器 | O(n) | O(1) |
|
|
821
|
+
| `: AsyncGenerator<E, void, undefined>` | 返回异步迭代器 | O(n) | O(1) |
|
|
822
|
+
| `count(): bigint` | 返回元素数量 | O(1) | O(1) |
|
|
823
|
+
| `average(): bigint` | 返回 bigint 平均值 | O(n) | O(1) |
|
|
824
|
+
| `average(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的平均值 | O(n) | O(1) |
|
|
825
|
+
| `range(): bigint` | 返回 bigint 极差 | O(1) | O(1) |
|
|
826
|
+
| `range(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的极差 | O(1) | O(1) |
|
|
827
|
+
| `variance(): bigint` | 返回 bigint 方差 | O(n) | O(1) |
|
|
828
|
+
| `variance(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的方差 | O(n) | O(1) |
|
|
829
|
+
| `standardDeviation(): bigint` | 返回 bigint 标准差 | O(n) | O(1) |
|
|
830
|
+
| `standardDeviation(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的标准差 | O(n) | O(1) |
|
|
831
|
+
| `mean(): bigint` | 返回 bigint 均值(同 average) | O(n) | O(1) |
|
|
832
|
+
| `mean(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的均值 | O(n) | O(1) |
|
|
833
|
+
| `median(): bigint` | 返回 bigint 中位数 | O(n) | O(n) |
|
|
834
|
+
| `median(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的中位数 | O(n) | O(n) |
|
|
835
|
+
| `mode(): bigint` | 返回 bigint 众数 | O(n) | O(n) |
|
|
836
|
+
| `mode(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的众数 | O(n) | O(n) |
|
|
837
|
+
| `frequency(): Map<E, bigint>` | 返回频率分布 | O(n) | O(n) |
|
|
838
|
+
| `summate(): bigint` | 返回 bigint 总和 | O(n) | O(1) |
|
|
839
|
+
| `summate(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的总和 | O(n) | O(1) |
|
|
840
|
+
| `quantile(quantile: number): bigint` | 返回指定 bigint 分位数 | O(1) | O(1) |
|
|
841
|
+
| `quantile(quantile: number, mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的指定分位数 | O(1) | O(1) |
|
|
842
|
+
| `interquartileRange(): bigint` | 返回 bigint 四分位距 | O(1) | O(1) |
|
|
843
|
+
| `interquartileRange(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的四分位距 | O(1) | O(1) |
|
|
844
|
+
| `skewness(): bigint` | 返回 bigint 偏度 | O(n) | O(1) |
|
|
845
|
+
| `skewness(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的偏度 | O(n) | O(1) |
|
|
846
|
+
| `kurtosis(): bigint` | 返回 bigint 峰度 | O(n) | O(1) |
|
|
847
|
+
| `kurtosis(mapper: Functional<E, bigint>): bigint` | 返回映射 bigint 值的峰度 | O(n) | O(1) |
|
|
848
|
+
|
|
849
|
+
```typescript
|
|
850
|
+
// 大整数统计分析示例
|
|
851
|
+
let bigInts = from([1000n, 2000n, 3000n, 4000n, 5000n, 6000n, 7000n, 8000n, 9000n, 10000n])
|
|
852
|
+
.toBigIntStatistics();
|
|
853
|
+
|
|
854
|
+
console.log("bigint 均值:", bigInts.mean()); // 5500n
|
|
855
|
+
console.log("bigint 总和:", bigInts.summate()); // 55000n
|
|
856
|
+
console.log("bigint 标准差:", bigInts.standardDeviation()); // 3027.65... 注意:计算为 bigint,结果为近似值
|
|
857
|
+
console.log("bigint 中位数:", bigInts.median()); // 5500n
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
## WindowCollectable 类方法
|
|
861
|
+
|
|
862
|
+
`WindowCollectable<E>` 是 `Collectable<E>` 的一种实现,专为**窗口操作**设计。它允许您将元素流划分为重叠或非重叠的窗口(片段),非常适合处理时间序列数据、批处理或滑动平均等场景。
|
|
863
|
+
|
|
864
|
+
| 方法 | 描述 | 时间复杂度 | 空间复杂度 |
|
|
865
|
+
|--------|-------------|-----------------|------------------|
|
|
866
|
+
| `constructor(generator: Generator<E>)` | 创建一个窗口可收集流实例 | O(n log n) | O(n) |
|
|
867
|
+
| `constructor(generator: Generator<E>, comparator: Comparator<E>)` | 使用比较器创建一个窗口可收集流实例 | O(n log n) | O(n) |
|
|
868
|
+
| `: Generator<E, void, undefined>` | 返回迭代器 | O(n) | O(1) |
|
|
869
|
+
| `: AsyncGenerator<E, void, undefined>` | 返回异步迭代器 | O(n) | O(1) |
|
|
870
|
+
| `slide(size: bigint): Semantic<Semantic<E>>` | 创建滑动窗口(步长为 1) | O(m × n) | O(m × n) |
|
|
871
|
+
| `slide(size: bigint, step: bigint): Semantic<Semantic<E>>` | 创建滑动窗口 | O(m × n) | O(m × n) |
|
|
872
|
+
| `tumble(size: bigint): Semantic<Semantic<E>>` | 创建滚动窗口 | O(k × n) | O(k × n) |
|
|
873
|
+
|
|
874
|
+
**参数说明**:
|
|
875
|
+
- `size`: 窗口大小(包含的元素数量)。
|
|
876
|
+
- `step`: 滑动步长(`slide` 方法专用)。
|
|
877
|
+
- 返回值:一个 `Semantic<Semantic<E>>`,其中每个内部 `Semantic<E>` 代表一个窗口。
|
|
878
|
+
|
|
879
|
+
**窗口类型**:
|
|
880
|
+
1. **滑动窗口 (Sliding Window)**: 通过 `slide(size, step)` 创建。窗口按指定步长滑动,相邻窗口之间可以有重叠。
|
|
881
|
+
2. **滚动窗口 (Tumbling Window)**: 通过 `tumble(size)` 创建。窗口是连续且不重叠的,等同于 `slide(size, size)`。
|
|
882
|
+
|
|
883
|
+
```typescript
|
|
884
|
+
// WindowCollectable 操作示例
|
|
885
|
+
let dataStream = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
886
|
+
|
|
887
|
+
// 转换为 WindowCollectable
|
|
888
|
+
let windowedData: WindowCollectable<number> = dataStream.toWindow();
|
|
889
|
+
|
|
890
|
+
// 示例 1: 大小为 3 的滑动窗口,步长为 1
|
|
891
|
+
let slidingWindows: Semantic<Semantic<number>> = windowedData.slide(3n, 1n);
|
|
892
|
+
console.log("滑动窗口 (size=3, step=1):");
|
|
893
|
+
slidingWindows.toUnordered()
|
|
894
|
+
.forEach((window, idx) => {
|
|
895
|
+
console.log(` 窗口 ${idx}:`, window.toArray());
|
|
896
|
+
});
|
|
897
|
+
// 输出:
|
|
898
|
+
// 窗口 0: [1, 2, 3]
|
|
899
|
+
// 窗口 1: [2, 3, 4]
|
|
900
|
+
// 窗口 2: [3, 4, 5]
|
|
901
|
+
// ... 以此类推
|
|
902
|
+
|
|
903
|
+
// 示例 2: 大小为 3 的滚动窗口
|
|
904
|
+
let tumblingWindows: Semantic<Semantic<number>> = windowedData.tumble(3n);
|
|
905
|
+
console.log("滚动窗口 (size=3):");
|
|
906
|
+
tumblingWindows.toUnordered()
|
|
907
|
+
.forEach((window, idx) => {
|
|
908
|
+
console.log(` 窗口 ${idx}:`, window.toArray());
|
|
909
|
+
});
|
|
910
|
+
// 输出:
|
|
911
|
+
// 窗口 0: [1, 2, 3]
|
|
912
|
+
// 窗口 1: [4, 5, 6]
|
|
913
|
+
// 窗口 2: [7, 8, 9]
|
|
914
|
+
// 最后一个不完整的窗口 [10] 通常会被丢弃(取决于实现)
|
|
915
|
+
|
|
916
|
+
// 示例 3: 计算每个窗口的平均值
|
|
917
|
+
let slidingWindowAverages = windowedData.slide(5n, 2n) // 大小为5,步长为2的滑动窗口
|
|
918
|
+
.map((windowSemantic) => {
|
|
919
|
+
return windowSemantic
|
|
920
|
+
.toNumericStatistics()
|
|
921
|
+
.average();
|
|
922
|
+
})
|
|
923
|
+
.toUnordered()
|
|
924
|
+
.toArray();
|
|
925
|
+
console.log("滑动窗口平均值 (size=5, step=2):", slidingWindowAverages);
|
|
926
|
+
// 输出: [3, 5, 7] (即 [1,2,3,4,5] 的平均值 3, [3,4,5,6,7] 的平均值 5, [5,6,7,8,9] 的平均值 7)
|
|
436
927
|
```
|
|
437
928
|
|
|
438
929
|
## 性能选择指南
|
|
439
930
|
|
|
440
|
-
|
|
931
|
+
选择合适的收集器类型对于优化应用性能至关重要。以下是基于不同使用场景的推荐选择:
|
|
932
|
+
|
|
933
|
+
### 场景 1:选择 UnorderedCollectable (性能优先)
|
|
934
|
+
当您的操作**不依赖**元素的顺序时,应始终优先使用 `toUnordered()`。这可以避免不必要的排序和索引维护开销,提供最佳性能。`UnorderedCollectable` 会自动忽略 `redirect`、`reverse`、`shuffle` 和 `translate` 等顺序操作。
|
|
441
935
|
|
|
442
936
|
```typescript
|
|
443
|
-
//
|
|
937
|
+
// 典型场景:计数、求和、检查存在性、去重、分组、写入流、日志记录等。
|
|
444
938
|
let highPerformance = data
|
|
445
|
-
.filter(
|
|
446
|
-
.map(
|
|
447
|
-
.toUnordered();
|
|
939
|
+
.filter((n) => n > 0) // 过滤
|
|
940
|
+
.map((n) => n * 2) // 映射
|
|
941
|
+
.toUnordered(); // 转换为无序收集器以获得最佳性能
|
|
942
|
+
|
|
943
|
+
let count = highPerformance.count(); // 快速计数
|
|
944
|
+
let exists = highPerformance.anyMatch((n) => n > 100); // 快速检查
|
|
945
|
+
highPerformance.forEach((n) => console.log(n)); // 快速遍历
|
|
448
946
|
```
|
|
449
947
|
|
|
450
|
-
###
|
|
948
|
+
### 场景 2:选择 OrderedCollectable (顺序必需)
|
|
949
|
+
当您的操作**依赖**元素的顺序时,必须使用 `OrderedCollectable`。调用 `.sorted()` 方法会自动创建它。它保证了结果的确定性顺序,但会带来 O(n log n) 的排序开销。
|
|
451
950
|
|
|
452
951
|
```typescript
|
|
453
|
-
//
|
|
454
|
-
let ordered = data
|
|
952
|
+
// 典型场景:排序、查找第一个/最后一个、需要确定顺序的归约、需要顺序的窗口操作等。
|
|
953
|
+
let ordered = data
|
|
954
|
+
.sorted((a, b) => b - a) // 降序排序,自动创建 OrderedCollectable
|
|
955
|
+
.limit(5); // 取最大的5个
|
|
956
|
+
|
|
957
|
+
let first = ordered.findFirst(); // 获取最大值
|
|
958
|
+
let last = ordered.findLast(); // 获取排序后的最后一个值(即第5大的值)
|
|
455
959
|
```
|
|
456
960
|
|
|
457
|
-
###
|
|
961
|
+
### 场景 3:选择 WindowCollectable (窗口操作)
|
|
962
|
+
当您需要对数据流进行**分块**或**滑动**分析时,请使用 `WindowCollectable`。它专门为处理时间窗口、数据批次或滑动聚合而设计。
|
|
458
963
|
|
|
459
964
|
```typescript
|
|
460
|
-
//
|
|
461
|
-
let
|
|
462
|
-
.toWindow()
|
|
463
|
-
|
|
965
|
+
// 典型场景:计算滑动平均、实时统计、批次处理、时间序列分析。
|
|
966
|
+
let windowedData: WindowCollectable<number> = data
|
|
967
|
+
.toWindow();
|
|
968
|
+
|
|
969
|
+
// 计算每5个元素的滚动平均值
|
|
970
|
+
let movingAverages = windowedData
|
|
971
|
+
.slide(5n, 1n) // 滑动窗口
|
|
972
|
+
.map(window => window.toNumericStatistics().average())
|
|
973
|
+
.toUnordered()
|
|
974
|
+
.toArray();
|
|
464
975
|
```
|
|
465
976
|
|
|
466
|
-
###
|
|
977
|
+
### 场景 4:选择 StatisticalAnalysis (数值计算)
|
|
978
|
+
当您需要对数值流(`number` 或 `bigint`)进行**统计分析**(如均值、方差、标准差、中位数、分位数等)时,应使用对应的统计类。它们内部已针对这些计算进行了优化。
|
|
467
979
|
|
|
468
980
|
```typescript
|
|
469
|
-
//
|
|
470
|
-
let
|
|
471
|
-
.toNumericStatistics(); //
|
|
472
|
-
let bigIntStatistics: BigintStatistics<bigint> = data
|
|
473
|
-
.toBigintStatistics(); // 大整数统计
|
|
474
|
-
```
|
|
981
|
+
// 典型场景:数据科学、财务分析、性能监控、质量评估。
|
|
982
|
+
let stats: NumericStatistics<number> = data
|
|
983
|
+
.toNumericStatistics(); // 转换为数字统计
|
|
475
984
|
|
|
476
|
-
|
|
477
|
-
|
|
985
|
+
console.log("均值:", stats.mean());
|
|
986
|
+
console.log("标准差:", stats.standardDeviation());
|
|
987
|
+
console.log("90% 分位数:", stats.quantile(0.9));
|
|
988
|
+
|
|
989
|
+
// 对于大整数数据
|
|
990
|
+
let bigIntStats: BigIntStatistics<bigint> = largeIntegerData
|
|
991
|
+
.toBigIntStatistics();
|
|
992
|
+
```
|
|
478
993
|
|
|
479
994
|
## 重要注意事项
|
|
480
995
|
|
|
481
|
-
1.
|
|
482
|
-
2.
|
|
483
|
-
3.
|
|
484
|
-
4.
|
|
996
|
+
1. **排序操作的影响**:在 `OrderedCollectable` 中,`sorted()` 操作会覆盖 `redirect`、`translate`、`shuffle`、`reverse` 等先前定义的所有顺序。最终顺序由 `sorted()` 决定。
|
|
997
|
+
2. **性能考量**:如果不需要顺序保证,请优先使用 `toUnordered()` 以获得最佳性能。排序操作(`sorted()`)需要 O(n) 的额外空间和 O(n log n) 的时间。
|
|
998
|
+
3. **内存使用**:`WindowCollectable` 和 `Statistics` 类在处理过程中可能需要缓存数据以进行计算,尤其是在进行窗口操作或排序时,请注意大数据集可能带来的内存压力。
|
|
999
|
+
4. **实时数据处理**:`Semantic` 流非常适合处理实时数据源(如 `interval`、`websocket`、`blob`),并支持异步数据源。结合 `WindowCollectable` 可以进行高效的流式分析。
|
|
1000
|
+
5. **短路操作**:利用 `anyMatch`、`allMatch`、`noneMatch`、`findFirst` 等短路操作,可以在满足条件时立即终止流的处理,从而提升效率。
|
|
1001
|
+
6. **类型安全**:该库充分利用 TypeScript 的类型系统,在编译时捕获许多潜在错误。请确保为您的映射器、断言和比较器函数提供正确的类型签名。
|
|
1002
|
+
|
|
1003
|
+
该库为 TypeScript 开发者提供了强大而灵活的流处理能力,将函数式编程的优势与类型安全保证完美结合。通过理解 `Semantic`、`Collectable` 及其各种子类(`UnorderedCollectable`、`OrderedCollectable`、`WindowCollectable`、`NumericStatistics`、`BigIntStatistics`)的职责,您可以构建出既高效又易于维护的数据处理管道。
|
|
1004
|
+
|
|
1005
|
+
---
|
|
485
1006
|
|
|
486
|
-
|
|
1007
|
+
https://github.com/eloyhere/semantic-typescript
|
|
1008
|
+
https://www.npmjs.com/package/semantic-typescript
|