semantic-typescript 0.0.7 → 0.1.4
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/semantic.d.ts +22 -5
- package/dist/semantic.js +106 -52
- package/package.json +53 -8
- package/readme.cn.md +360 -269
- package/readme.de.md +356 -264
- package/readme.es.md +357 -264
- package/readme.fr.md +357 -264
- package/readme.jp.md +362 -270
- package/readme.kr.md +361 -269
- package/readme.md +361 -270
- package/readme.ru.md +428 -0
- package/readme.tw.md +354 -263
package/readme.md
CHANGED
|
@@ -1,335 +1,426 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Semantic-TypeScript Stream Processing Library
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Provides composable, functional-style constructs for working with collections, streams, and sequences — with support for sorting, filtering, grouping, statistics, and more.
|
|
3
|
+
## Introduction
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
Semantic-TypeScript is a modern stream processing library inspired by JavaScript GeneratorFunction, Java Stream, and MySQL Index. Its core design philosophy is based on building efficient data processing pipelines using data indexing, providing a type-safe, functional-style streaming operation experience for front-end development.
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
Unlike traditional synchronous processing, Semantic employs an asynchronous processing model. When creating a data stream, the time when the terminal receives data entirely depends on when the upstream calls the `accept` and `interrupt` callback functions. This design allows the library to elegantly handle real-time data streams, large datasets, and asynchronous data sources.
|
|
9
8
|
|
|
10
|
-
##
|
|
11
|
-
|
|
12
|
-
- ✅ **Type-safe generics** throughout
|
|
13
|
-
- ✅ **Functional programming** style (map, filter, reduce, etc.)
|
|
14
|
-
- ✅ **Semantic data streams** (`Semantic<E>`) for lazy evaluation
|
|
15
|
-
- ✅ **Collectors** for transforming streams into concrete structures
|
|
16
|
-
- ✅ **Ordered & Unordered Collectables** — with `toUnordered()` being the **fastest** (no sorting)
|
|
17
|
-
- ✅ **Sorting support** via `sorted()`, `toOrdered()`, comparators
|
|
18
|
-
- ✅ **Statistical analysis** (`Statistics`, `NumericStatistics`, `BigIntStatistics`)
|
|
19
|
-
- ✅ **Optional<T>** monad for safe nullable handling
|
|
20
|
-
- ✅ **Iterators & Generators** based design — suitable for large or asynchronous data
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
|
|
24
|
-
## 📦 Installation
|
|
9
|
+
## Installation
|
|
25
10
|
|
|
26
11
|
```bash
|
|
27
12
|
npm install semantic-typescript
|
|
28
13
|
```
|
|
29
14
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
|
41
|
-
|
|
42
|
-
| `
|
|
43
|
-
| `
|
|
44
|
-
| `
|
|
45
|
-
| `
|
|
46
|
-
| `
|
|
47
|
-
| `
|
|
48
|
-
| `
|
|
49
|
-
| `
|
|
50
|
-
| `
|
|
51
|
-
| `
|
|
52
|
-
|
|
53
|
-
|
|
15
|
+
## Basic Types
|
|
16
|
+
|
|
17
|
+
| Type | Description |
|
|
18
|
+
|------|------|
|
|
19
|
+
| `Invalid<T>` | Type extending null or undefined |
|
|
20
|
+
| `Valid<T>` | Type excluding null and undefined |
|
|
21
|
+
| `MaybeInvalid<T>` | Type that may be null or undefined |
|
|
22
|
+
| `Primitive` | Collection of primitive types |
|
|
23
|
+
| `MaybePrimitive<T>` | Type that may be a primitive type |
|
|
24
|
+
| `OptionalSymbol` | Symbol identifier for the Optional class |
|
|
25
|
+
| `SemanticSymbol` | Symbol identifier for the Semantic class |
|
|
26
|
+
| `CollectorsSymbol` | Symbol identifier for the Collector class |
|
|
27
|
+
| `CollectableSymbol` | Symbol identifier for the Collectable class |
|
|
28
|
+
| `OrderedCollectableSymbol` | Symbol identifier for the OrderedCollectable class |
|
|
29
|
+
| `WindowCollectableSymbol` | Symbol identifier for the WindowCollectable class |
|
|
30
|
+
| `StatisticsSymbol` | Symbol identifier for the Statistics class |
|
|
31
|
+
| `NumericStatisticsSymbol` | Symbol identifier for the NumericStatistics class |
|
|
32
|
+
| `BigIntStatisticsSymbol` | Symbol identifier for the BigIntStatistics class |
|
|
33
|
+
| `UnorderedCollectableSymbol` | Symbol identifier for the UnorderedCollectable class |
|
|
34
|
+
| `Runnable` | Function with no parameters and no return value |
|
|
35
|
+
| `Supplier<R>` | Function with no parameters returning R |
|
|
36
|
+
| `Functional<T, R>` | Single-parameter transformation function |
|
|
37
|
+
| `Predicate<T>` | Single-parameter predicate function |
|
|
38
|
+
| `BiFunctional<T, U, R>` | Two-parameter transformation function |
|
|
39
|
+
| `BiPredicate<T, U>` | Two-parameter predicate function |
|
|
40
|
+
| `Comparator<T>` | Comparison function |
|
|
41
|
+
| `TriFunctional<T, U, V, R>` | Three-parameter transformation function |
|
|
42
|
+
| `Consumer<T>` | Single-parameter consumer function |
|
|
43
|
+
| `BiConsumer<T, U>` | Two-parameter consumer function |
|
|
44
|
+
| `TriConsumer<T, U, V>` | Three-parameter consumer function |
|
|
45
|
+
| `Generator<T>` | Generator function |
|
|
54
46
|
|
|
55
47
|
```typescript
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
const opt = Optional.ofNullable(value);
|
|
61
|
-
|
|
62
|
-
const result = opt
|
|
63
|
-
.filter(v => v > 5)
|
|
64
|
-
.map(v => v * 2)
|
|
65
|
-
.getOrDefault(0);
|
|
66
|
-
|
|
67
|
-
console.log(result); // 20 or 0
|
|
48
|
+
// Type usage examples
|
|
49
|
+
const predicate: Predicate<number> = (n) => n > 0;
|
|
50
|
+
const mapper: Functional<string, number> = (str) => str.length;
|
|
51
|
+
const comparator: Comparator<number> = (a, b) => a - b;
|
|
68
52
|
```
|
|
69
53
|
|
|
70
|
-
|
|
54
|
+
## Type Guards
|
|
55
|
+
|
|
56
|
+
| Function | Description | Time Complexity | Space Complexity |
|
|
57
|
+
|------|------|------------|------------|
|
|
58
|
+
| `validate<T>(t: MaybeInvalid<T>): t is T` | Validate value is not null or undefined | O(1) | O(1) |
|
|
59
|
+
| `invalidate<T>(t: MaybeInvalid<T>): t is null \| undefined` | Validate value is null or undefined | O(1) | O(1) |
|
|
60
|
+
| `isBoolean(t: unknown): t is boolean` | Check if it is a boolean | O(1) | O(1) |
|
|
61
|
+
| `isString(t: unknown): t is string` | Check if it is a string | O(1) | O(1) |
|
|
62
|
+
| `isNumber(t: unknown): t is number` | Check if it is a number | O(1) | O(1) |
|
|
63
|
+
| `isFunction(t: unknown): t is Function` | Check if it is a function | O(1) | O(1) |
|
|
64
|
+
| `isObject(t: unknown): t is object` | Check if it is an object | O(1) | O(1) |
|
|
65
|
+
| `isSymbol(t: unknown): t is symbol` | Check if it is a Symbol | O(1) | O(1) |
|
|
66
|
+
| `isBigint(t: unknown): t is bigint` | Check if it is a BigInt | O(1) | O(1) |
|
|
67
|
+
| `isPrimitive(t: unknown): t is Primitive` | Check if it is a primitive type | O(1) | O(1) |
|
|
68
|
+
| `isIterable(t: unknown): t is Iterable<unknown>` | Check if it is an iterable object | O(1) | O(1) |
|
|
69
|
+
| `isOptional(t: unknown): t is Optional<unknown>` | Check if it is an Optional instance | O(1) | O(1) |
|
|
70
|
+
| `isSemantic(t: unknown): t is Semantic<unknown>` | Check if it is a Semantic instance | O(1) | O(1) |
|
|
71
|
+
| `isCollector(t: unknown): t is Collector<unknown, unknown, unknown>` | Check if it is a Collector instance | O(1) | O(1) |
|
|
72
|
+
| `isCollectable(t: unknown): t is Collectable<unknown>` | Check if it is a Collectable instance | O(1) | O(1) |
|
|
73
|
+
| `isOrderedCollectable(t: unknown): t is OrderedCollectable<unknown>` | Check if it is an OrderedCollectable instance | O(1) | O(1) |
|
|
74
|
+
| `isWindowCollectable(t: unknown): t is WindowCollectable<unknown>` | Check if it is a WindowCollectable instance | O(1) | O(1) |
|
|
75
|
+
| `isUnorderedCollectable(t: unknown): t is UnorderedCollectable<unknown>` | Check if it is an UnorderedCollectable instance | O(1) | O(1) |
|
|
76
|
+
| `isStatistics(t: unknown): t is Statistics<unknown, number \| bigint>` | Check if it is a Statistics instance | O(1) | O(1) |
|
|
77
|
+
| `isNumericStatistics(t: unknown): t is NumericStatistics<unknown>` | Check if it is a NumericStatistics instance | O(1) | O(1) |
|
|
78
|
+
| `isBigIntStatistics(t: unknown): t is BigIntStatistics<unknown>` | Check if it is a BigIntStatistics instance | O(1) | O(1) |
|
|
71
79
|
|
|
72
|
-
|
|
80
|
+
```typescript
|
|
81
|
+
// Type guard usage examples
|
|
82
|
+
const value: unknown = "hello";
|
|
73
83
|
|
|
74
|
-
|
|
84
|
+
if (isString(value)) {
|
|
85
|
+
console.log(value.length); // Type-safe, value inferred as string
|
|
86
|
+
}
|
|
75
87
|
|
|
76
|
-
|
|
88
|
+
if (isOptional(someValue)) {
|
|
89
|
+
someValue.ifPresent(val => console.log(val));
|
|
90
|
+
}
|
|
91
|
+
```
|
|
77
92
|
|
|
78
|
-
|
|
93
|
+
## Utility Functions
|
|
79
94
|
|
|
80
|
-
| Function
|
|
81
|
-
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
84
|
-
| `fill(element, count)` | Repeat an element N times | `fill('a', 3n)` |
|
|
85
|
-
| `iterate(gen)` | Use a custom generator function | `iterate(genFn)` |
|
|
95
|
+
| Function | Description | Time Complexity | Space Complexity |
|
|
96
|
+
|------|------|------------|------------|
|
|
97
|
+
| `useCompare<T>(t1: T, t2: T): number` | Generic comparison function | O(1) | O(1) |
|
|
98
|
+
| `useRandom<T = number \| bigint>(index: T): T` | Pseudo-random number generator | O(log n) | O(1) |
|
|
86
99
|
|
|
87
|
-
|
|
100
|
+
```typescript
|
|
101
|
+
// Utility function usage examples
|
|
102
|
+
const numbers = [3, 1, 4, 1, 5];
|
|
103
|
+
numbers.sort(useCompare); // [1, 1, 3, 4, 5]
|
|
88
104
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
| `filter(fn)` | Retain elements passing predicate | `.filter(x => x > 10)` |
|
|
93
|
-
| `limit(n)` | Limit to first N elements | `.limit(5)` |
|
|
94
|
-
| `skip(n)` | Skip first N elements | `.skip(2)` |
|
|
95
|
-
| `distinct()` | Remove duplicates (uses Set by default) | `.distinct()` |
|
|
96
|
-
| `sorted()` | Sort elements (natural ordering) | `.sorted()` |
|
|
97
|
-
| `sorted(comparator)`| Custom sorting | `.sorted((a, b) => a - b)` |
|
|
98
|
-
| `toOrdered()` | Sort and return `OrderedCollectable` | `.toOrdered()` |
|
|
99
|
-
| `toUnordered()` | **No sorting** – fastest possible collectable | `.toUnordered()` ✅ |
|
|
100
|
-
| `collect(collector)`| Aggregate using a `Collector` | `.collect(Collector.full(...))` |
|
|
101
|
-
| `toArray()` | Convert to Array | `.toArray()` |
|
|
102
|
-
| `toSet()` | Convert to Set | `.toSet()` |
|
|
103
|
-
| `toMap(keyFn, valFn)`| Convert to Map | `.toMap(x => x.id, x => x)` |
|
|
105
|
+
const randomNum = useRandom(42); // Seed-based random number
|
|
106
|
+
const randomBigInt = useRandom(1000n); // BigInt random number
|
|
107
|
+
```
|
|
104
108
|
|
|
105
|
-
|
|
109
|
+
## Factory Methods
|
|
106
110
|
|
|
107
|
-
###
|
|
111
|
+
### Optional Factory Methods
|
|
108
112
|
|
|
109
|
-
|
|
113
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
114
|
+
|------|------|------------|------------|
|
|
115
|
+
| `Optional.empty<T>()` | Create an empty Optional | O(1) | O(1) |
|
|
116
|
+
| `Optional.of<T>(value)` | Create an Optional containing a value | O(1) | O(1) |
|
|
117
|
+
| `Optional.ofNullable<T>(value)` | Create a potentially empty Optional | O(1) | O(1) |
|
|
118
|
+
| `Optional.ofNonNull<T>(value)` | Create a non-null Optional | O(1) | O(1) |
|
|
110
119
|
|
|
111
120
|
```typescript
|
|
112
|
-
|
|
121
|
+
// Optional usage examples
|
|
122
|
+
const emptyOpt = Optional.empty<number>();
|
|
123
|
+
const presentOpt = Optional.of(42);
|
|
124
|
+
const nullableOpt = Optional.ofNullable<string>(null);
|
|
125
|
+
const nonNullOpt = Optional.ofNonNull("hello");
|
|
126
|
+
|
|
127
|
+
presentOpt.ifPresent(val => console.log(val)); // Outputs 42
|
|
128
|
+
console.log(emptyOpt.orElse(100)); // Outputs 100
|
|
113
129
|
```
|
|
114
130
|
|
|
115
|
-
|
|
116
|
-
Ideal when order is irrelevant and maximum speed is desired.
|
|
117
|
-
|
|
118
|
-
---
|
|
131
|
+
### Collector Factory Methods
|
|
119
132
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
133
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
134
|
+
|------|------|------------|------------|
|
|
135
|
+
| `Collector.full(identity, accumulator, finisher)` | Create a full collector | O(1) | O(1) |
|
|
136
|
+
| `Collector.shortable(identity, interruptor, accumulator, finisher)` | Create an interruptible collector | O(1) | O(1) |
|
|
123
137
|
|
|
124
138
|
```typescript
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
|
|
139
|
+
// Collector usage examples
|
|
140
|
+
const sumCollector = Collector.full(
|
|
141
|
+
() => 0,
|
|
142
|
+
(sum, num) => sum + num,
|
|
143
|
+
result => result
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const numbers = from([1, 2, 3, 4, 5]);
|
|
147
|
+
const total = numbers.toUnoredered().collect(sumCollector); // 15
|
|
128
148
|
```
|
|
129
149
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
---
|
|
150
|
+
### Semantic Factory Methods
|
|
133
151
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
152
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
153
|
+
|------|------|------------|------------|
|
|
154
|
+
| `blob(blob, chunkSize)` | Create a stream from a Blob | O(n) | O(chunkSize) |
|
|
155
|
+
| `empty<E>()` | Create an empty stream | O(1) | O(1) |
|
|
156
|
+
| `fill<E>(element, count)` | Create a filled stream | O(n) | O(1) |
|
|
157
|
+
| `from<E>(iterable)` | Create a stream from an iterable object | O(1) | O(1) |
|
|
158
|
+
| `interval(period, delay?)` | Create a timed interval stream | O(1)* | O(1) |
|
|
159
|
+
| `iterate<E>(generator)` | Create a stream from a generator | O(1) | O(1) |
|
|
160
|
+
| `range(start, end, step)` | Create a numerical range stream | O(n) | O(1) |
|
|
161
|
+
| `websocket(websocket)` | Create a stream from a WebSocket | O(1) | O(1) |
|
|
139
162
|
|
|
140
163
|
```typescript
|
|
141
|
-
|
|
142
|
-
|
|
164
|
+
// Semantic factory method usage examples
|
|
165
|
+
|
|
166
|
+
// Create a stream from a Blob (chunked reading)
|
|
167
|
+
blob(someBlob, 1024n)
|
|
168
|
+
.toUnordered()
|
|
169
|
+
.write(WritableStream)
|
|
170
|
+
.then(callback) // Write stream successful
|
|
171
|
+
.catch(writeFi); // Write stream failed
|
|
172
|
+
|
|
173
|
+
// Create an empty stream, won't execute until concatenated with other streams
|
|
174
|
+
empty<string>()
|
|
175
|
+
.toUnordered()
|
|
176
|
+
.join(); //[]
|
|
177
|
+
|
|
178
|
+
// Create a filled stream
|
|
179
|
+
const filledStream = fill("hello", 3); // "hello", "hello", "hello"
|
|
180
|
+
|
|
181
|
+
// Create a timed stream with initial 2-second delay and 5-second execution period, implemented based on timer mechanism; may experience time drift due to system scheduling precision limitations.
|
|
182
|
+
const intervalStream = interval(5000, 2000);
|
|
183
|
+
|
|
184
|
+
// Create a stream from an iterable object
|
|
185
|
+
const numberStream = from([1, 2, 3, 4, 5]);
|
|
186
|
+
const stringStream = from(new Set(["Alex", "Bob"]));
|
|
187
|
+
|
|
188
|
+
// Create a range stream
|
|
189
|
+
const rangeStream = range(1, 10, 2); // 1, 3, 5, 7, 9
|
|
190
|
+
|
|
191
|
+
// WebSocket event stream
|
|
192
|
+
const ws = new WebSocket("ws://localhost:8080");
|
|
193
|
+
websocket(ws)
|
|
194
|
+
.filter((event)=> event.type === "message"); // Only listen to message events
|
|
195
|
+
.toUnordered() // Generally not ordered for events
|
|
196
|
+
.forEach((event)=> receive(event)); // Receive messages
|
|
143
197
|
```
|
|
144
198
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
|
161
|
-
|
|
162
|
-
| `
|
|
163
|
-
| `
|
|
164
|
-
| `
|
|
165
|
-
| `
|
|
166
|
-
| `
|
|
167
|
-
| `
|
|
168
|
-
| `
|
|
169
|
-
| `
|
|
170
|
-
|
|
171
|
-
---
|
|
172
|
-
|
|
173
|
-
### 7. `OrderedCollectable<E>` – Sorted Data
|
|
174
|
-
|
|
175
|
-
If you require elements to be **automatically sorted**, use this.
|
|
176
|
-
|
|
177
|
-
Accepts a **custom comparator** or uses natural order.
|
|
199
|
+
## Semantic Class Methods
|
|
200
|
+
|
|
201
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
202
|
+
|------|------|------------|------------|
|
|
203
|
+
| `concat(other)` | Concatenate two streams | O(n) | O(1) |
|
|
204
|
+
| `distinct()` | Remove duplicates | O(n) | O(n) |
|
|
205
|
+
| `distinct(comparator)` | Remove duplicates using a comparator | O(n²) | O(n) |
|
|
206
|
+
| `dropWhile(predicate)` | Discard elements satisfying condition | O(n) | O(1) |
|
|
207
|
+
| `filter(predicate)` | Filter elements | O(n) | O(1) |
|
|
208
|
+
| `flat(mapper)` | Flattening map | O(n × m) | O(1) |
|
|
209
|
+
| `flatMap(mapper)` | Flattening map to new type | O(n × m) | O(1) |
|
|
210
|
+
| `limit(n)` | Limit number of elements | O(n) | O(1) |
|
|
211
|
+
| `map(mapper)` | Map transformation | O(n) | O(1) |
|
|
212
|
+
| `peek(consumer)` | Peek at elements | O(n) | O(1) |
|
|
213
|
+
| `redirect(redirector)` | Redirect index | O(n) | O(1) |
|
|
214
|
+
| `reverse()` | Reverse stream | O(n) | O(1) |
|
|
215
|
+
| `shuffle()` | Randomly shuffle | O(n) | O(1) |
|
|
216
|
+
| `shuffle(mapper)` | Shuffle using a mapper | O(n) | O(1) |
|
|
217
|
+
| `skip(n)` | Skip first n elements | O(n) | O(1) |
|
|
218
|
+
| `sorted()` | Sort | O(n log n) | O(n) |
|
|
219
|
+
| `sorted(comparator)` | Sort using a comparator | O(n log n) | O(n) |
|
|
220
|
+
| `sub(start, end)` | Get substream | O(n) | O(1) |
|
|
221
|
+
| `takeWhile(predicate)` | Get elements satisfying condition | O(n) | O(1) |
|
|
222
|
+
| `translate(offset)` | Translate index | O(n) | O(1) |
|
|
223
|
+
| `translate(translator)` | Translate index using a translator | O(n) | O(1) |
|
|
178
224
|
|
|
179
225
|
```typescript
|
|
180
|
-
|
|
181
|
-
const
|
|
226
|
+
// Semantic operation examples
|
|
227
|
+
const result = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
|
228
|
+
.filter(n => n % 2 === 0) // Filter even numbers
|
|
229
|
+
.map(n => n * 2) // Multiply by 2
|
|
230
|
+
.skip(1) // Skip the first
|
|
231
|
+
.limit(3) // Limit to 3 elements
|
|
232
|
+
.toArray(); // Convert to array
|
|
233
|
+
// Result: [8, 12, 20]
|
|
234
|
+
|
|
235
|
+
// Complex operation example
|
|
236
|
+
const complexResult = range(1, 100, 1)
|
|
237
|
+
.flatMap(n => from([n, n * 2])) // Map each element to two
|
|
238
|
+
.distinct() // Remove duplicates
|
|
239
|
+
.shuffle() // Shuffle order
|
|
240
|
+
.takeWhile(n => n < 50) // Take elements less than 50
|
|
241
|
+
.toOrdered() // Convert to ordered collector
|
|
242
|
+
.toArray(); // Convert to array
|
|
182
243
|
```
|
|
183
244
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
---
|
|
245
|
+
## Collector Conversion Methods
|
|
187
246
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
247
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
248
|
+
|------|------|------------|------------|
|
|
249
|
+
| `toUnoredered()` | Convert to unordered collector (performance first) | O(1) | O(1) |
|
|
250
|
+
| `toOrdered()` | Convert to ordered collector | O(1) | O(1) |
|
|
251
|
+
| `sorted()` | Sort and convert to ordered collector | O(n log n) | O(n) |
|
|
252
|
+
| `toWindow()` | Convert to window collector | O(1) | O(1) |
|
|
253
|
+
| `toNumericStatistics()` | Convert to numerical statistics | O(1) | O(1) |
|
|
254
|
+
| `toBigintStatistics()` | Convert to big integer statistics | O(1) | O(1) |
|
|
191
255
|
|
|
192
256
|
```typescript
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
257
|
+
// Collector conversion examples
|
|
258
|
+
const numbers = from([3, 1, 4, 1, 5, 9, 2, 6, 5]);
|
|
259
|
+
|
|
260
|
+
// Performance first: use unordered collector
|
|
261
|
+
const unordered = numbers
|
|
262
|
+
.filter(n => n > 3)
|
|
263
|
+
.toUnoredered();
|
|
264
|
+
|
|
265
|
+
// Need sorting: use ordered collector
|
|
266
|
+
const ordered = numbers
|
|
267
|
+
.sorted()
|
|
268
|
+
.toOrdered();
|
|
269
|
+
|
|
270
|
+
// Statistical analysis: use statistical collector
|
|
271
|
+
const stats = numbers
|
|
272
|
+
.toNumericStatistics();
|
|
273
|
+
|
|
274
|
+
console.log(stats.mean()); // Average value
|
|
275
|
+
console.log(stats.median()); // Median value
|
|
276
|
+
console.log(stats.standardDeviation()); // Standard deviation
|
|
277
|
+
|
|
278
|
+
// Window operations
|
|
279
|
+
const windowed = numbers
|
|
280
|
+
.toWindow()
|
|
281
|
+
.tumble(3n); // Every 3 elements form a window
|
|
282
|
+
|
|
283
|
+
windowed.forEach(window => {
|
|
284
|
+
console.log(window.toArray()); // Contents of each window
|
|
285
|
+
});
|
|
196
286
|
```
|
|
197
287
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
|
215
|
-
|
|
216
|
-
| `
|
|
217
|
-
| `
|
|
218
|
-
| `
|
|
219
|
-
| `
|
|
220
|
-
| `
|
|
221
|
-
| `
|
|
222
|
-
| `
|
|
223
|
-
| `standardDeviation()` | Standard deviation | `.standardDeviation()` |
|
|
224
|
-
| `summate()` | Sum of elements | `.summate()` |
|
|
225
|
-
| `quantile(q)` | Value at q-th percentile (0–1) | `.quantile(0.5)` → median |
|
|
226
|
-
| `frequency()` | Frequency map | `.frequency()` |
|
|
227
|
-
|
|
228
|
-
---
|
|
229
|
-
|
|
230
|
-
## 🧪 Full Example
|
|
288
|
+
## Collectable Collection Methods
|
|
289
|
+
|
|
290
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
291
|
+
|------|------|------------|------------|
|
|
292
|
+
| `anyMatch(predicate)` | Whether any element matches | O(n) | O(1) |
|
|
293
|
+
| `allMatch(predicate)` | Whether all elements match | O(n) | O(1) |
|
|
294
|
+
| `count()` | Element count | O(n) | O(1) |
|
|
295
|
+
| `isEmpty()` | Whether it is empty | O(1) | O(1) |
|
|
296
|
+
| `findAny()` | Find any element | O(n) | O(1) |
|
|
297
|
+
| `findFirst()` | Find the first element | O(n) | O(1) |
|
|
298
|
+
| `findLast()` | Find the last element | O(n) | O(1) |
|
|
299
|
+
| `forEach(action)` | Iterate over all elements | O(n) | O(1) |
|
|
300
|
+
| `group(classifier)` | Group by classifier | O(n) | O(n) |
|
|
301
|
+
| `groupBy(keyExtractor, valueExtractor)` | Group by key-value extractor | O(n) | O(n) |
|
|
302
|
+
| `join()` | Join as string | O(n) | O(n) |
|
|
303
|
+
| `join(delimiter)` | Join using a delimiter | O(n) | O(n) |
|
|
304
|
+
| `nonMatch(predicate)` | Whether no elements match | O(n) | O(1) |
|
|
305
|
+
| `partition(count)` | Partition by count | O(n) | O(n) |
|
|
306
|
+
| `partitionBy(classifier)` | Partition by classifier | O(n) | O(n) |
|
|
307
|
+
| `reduce(accumulator)` | Reduction operation | O(n) | O(1) |
|
|
308
|
+
| `reduce(identity, accumulator)` | Reduction with initial value | O(n) | O(1) |
|
|
309
|
+
| `toArray()` | Convert to array | O(n) | O(n) |
|
|
310
|
+
| `toMap(keyExtractor, valueExtractor)` | Convert to Map | O(n) | O(n) |
|
|
311
|
+
| `toSet()` | Convert to Set | O(n) | O(n) |
|
|
312
|
+
| `write(stream)` | Write to stream | O(n) | O(1) |
|
|
231
313
|
|
|
232
314
|
```typescript
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
//
|
|
239
|
-
|
|
240
|
-
console.log(
|
|
241
|
-
|
|
242
|
-
//
|
|
243
|
-
|
|
244
|
-
console.log(
|
|
245
|
-
|
|
246
|
-
//
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
315
|
+
// Collectable operation examples
|
|
316
|
+
const data = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
|
317
|
+
.filter(n => n % 2 === 0)
|
|
318
|
+
.toOrdered();
|
|
319
|
+
|
|
320
|
+
// Match checks
|
|
321
|
+
console.log(data.anyMatch(n => n > 5)); // true
|
|
322
|
+
console.log(data.allMatch(n => n < 20)); // true
|
|
323
|
+
|
|
324
|
+
// Find operations
|
|
325
|
+
data.findFirst().ifPresent(n => console.log(n)); // 2
|
|
326
|
+
data.findAny().ifPresent(n => console.log(n)); // Any element
|
|
327
|
+
|
|
328
|
+
// Grouping operations
|
|
329
|
+
const grouped = data.groupBy(
|
|
330
|
+
n => n > 5 ? "large" : "small",
|
|
331
|
+
n => n * 2
|
|
332
|
+
);
|
|
333
|
+
// {small: [4, 8], large: [12, 16, 20]}
|
|
334
|
+
|
|
335
|
+
// Reduction operations
|
|
336
|
+
const sum = data.reduce(0, (acc, n) => acc + n); // 30
|
|
337
|
+
|
|
338
|
+
// Output operations
|
|
339
|
+
data.join(", "); // "2, 4, 6, 8, 10"
|
|
253
340
|
```
|
|
254
341
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
|
262
|
-
|
|
263
|
-
| `
|
|
264
|
-
| `
|
|
265
|
-
| `
|
|
266
|
-
| `
|
|
267
|
-
| `
|
|
268
|
-
| `
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
You may create custom **generators** for infinite or controlled data streams:
|
|
342
|
+
## Statistical Analysis Methods
|
|
343
|
+
|
|
344
|
+
### NumericStatistics Methods
|
|
345
|
+
|
|
346
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
347
|
+
|------|------|------------|------------|
|
|
348
|
+
| `range()` | Range | O(n) | O(1) |
|
|
349
|
+
| `variance()` | Variance | O(n) | O(1) |
|
|
350
|
+
| `standardDeviation()` | Standard deviation | O(n) | O(1) |
|
|
351
|
+
| `mean()` | Mean | O(n) | O(1) |
|
|
352
|
+
| `median()` | Median | O(n log n) | O(n) |
|
|
353
|
+
| `mode()` | Mode | O(n) | O(n) |
|
|
354
|
+
| `frequency()` | Frequency distribution | O(n) | O(n) |
|
|
355
|
+
| `summate()` | Summation | O(n) | O(1) |
|
|
356
|
+
| `quantile(quantile)` | Quantile | O(n log n) | O(n) |
|
|
357
|
+
| `interquartileRange()` | Interquartile range | O(n log n) | O(n) |
|
|
358
|
+
| `skewness()` | Skewness | O(n) | O(1) |
|
|
359
|
+
| `kurtosis()` | Kurtosis | O(n) | O(1) |
|
|
275
360
|
|
|
276
361
|
```typescript
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
362
|
+
// Statistical analysis examples
|
|
363
|
+
const numbers = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
|
364
|
+
.toNumericStatistics();
|
|
365
|
+
|
|
366
|
+
console.log("Mean:", numbers.mean()); // 5.5
|
|
367
|
+
console.log("Median:", numbers.median()); // 5.5
|
|
368
|
+
console.log("Standard deviation:", numbers.standardDeviation()); // ~2.87
|
|
369
|
+
console.log("Sum:", numbers.summate()); // 55
|
|
370
|
+
|
|
371
|
+
// Statistical analysis using mappers
|
|
372
|
+
const objects = from([
|
|
373
|
+
{ value: 10 },
|
|
374
|
+
{ value: 20 },
|
|
375
|
+
{ value: 30 }
|
|
376
|
+
]).toNumericStatistics();
|
|
377
|
+
|
|
378
|
+
console.log("Mapped mean:", objects.mean(obj => obj.value)); // 20
|
|
285
379
|
```
|
|
286
380
|
|
|
287
|
-
|
|
381
|
+
## Performance Selection Guide
|
|
288
382
|
|
|
383
|
+
### Choose Unordered Collector (Performance First)
|
|
289
384
|
```typescript
|
|
290
|
-
|
|
385
|
+
// When order guarantee is not needed, use unordered collector for best performance
|
|
386
|
+
const highPerformance = data
|
|
387
|
+
.filter(predicate)
|
|
388
|
+
.map(mapper)
|
|
389
|
+
.toUnoredered(); // Best performance
|
|
291
390
|
```
|
|
292
391
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
---
|
|
300
|
-
|
|
301
|
-
## 🙌 Contributing
|
|
302
|
-
|
|
303
|
-
Pull requests, issues, and suggestions are most welcome!
|
|
304
|
-
|
|
305
|
-
---
|
|
306
|
-
|
|
307
|
-
## 🚀 Quick Start Summary
|
|
308
|
-
|
|
309
|
-
| Task | Method |
|
|
310
|
-
|------|--------|
|
|
311
|
-
| Safely handle nulls | `Optional<T>` |
|
|
312
|
-
| Create a stream | `from([...])`, `range()`, `fill()` |
|
|
313
|
-
| Transform data | `map()`, `filter()` |
|
|
314
|
-
| Sort data | `sorted()`, `toOrdered()` |
|
|
315
|
-
| No sort (fastest) | `toUnordered()` ✅ |
|
|
316
|
-
| Group / aggregate | `toMap()`, `group()`, `Collector` |
|
|
317
|
-
| Statistics | `NumericStatistics`, `mean()`, `median()`, etc. |
|
|
392
|
+
### Choose Ordered Collector (Order Required)
|
|
393
|
+
```typescript
|
|
394
|
+
// When element order needs to be maintained, use ordered collector
|
|
395
|
+
const ordered = data.sorted(comparator);
|
|
396
|
+
```
|
|
318
397
|
|
|
319
|
-
|
|
398
|
+
### Choose Window Collector (Window Operations)
|
|
399
|
+
```typescript
|
|
400
|
+
// When window operations are needed
|
|
401
|
+
const windowed = data
|
|
402
|
+
.toWindow()
|
|
403
|
+
.slide(5n, 2n); // Sliding window
|
|
404
|
+
```
|
|
320
405
|
|
|
321
|
-
|
|
406
|
+
### Choose Statistical Analysis (Numerical Calculations)
|
|
407
|
+
```typescript
|
|
408
|
+
// When statistical analysis is needed
|
|
409
|
+
const stats = data
|
|
410
|
+
.toNumericStatistics(); // Numerical statistics
|
|
322
411
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
412
|
+
const bigIntStats = data
|
|
413
|
+
.toBigintStatistics(); // Big integer statistics
|
|
414
|
+
```
|
|
326
415
|
|
|
327
|
-
|
|
416
|
+
[GitHub](https://github.com/eloyhere/semantic-typescript)
|
|
417
|
+
[NPMJS](https://www.npmjs.com/package/semantic-typescript)
|
|
328
418
|
|
|
329
|
-
|
|
419
|
+
## Important Notes
|
|
330
420
|
|
|
331
|
-
|
|
421
|
+
1. **Impact of Sorting Operations**: In ordered collectors, the `sorted()` operation overrides the effects of `redirect`, `translate`, `shuffle`, `reverse`
|
|
422
|
+
2. **Performance Considerations**: If order guarantee is not needed, prioritise using `toUnoredered()` for better performance
|
|
423
|
+
3. **Memory Usage**: Sorting operations require O(n) additional space
|
|
424
|
+
4. **Real-time Data**: Semantic streams are suitable for processing real-time data and support asynchronous data sources
|
|
332
425
|
|
|
333
|
-
|
|
334
|
-
- `toUnordered()` → **No sort, fastest**
|
|
335
|
-
- All others (e.g. `sorted()`, `toOrdered()`) → **Sort data**
|
|
426
|
+
This library provides TypeScript developers with powerful and flexible streaming capabilities, combining the benefits of functional programming with type safety guarantees.
|