semantic-typescript 0.3.3 → 0.3.7
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.d.ts +19 -24
- package/dist/collectable.js +329 -177
- package/dist/collector.d.ts +93 -16
- package/dist/collector.js +297 -107
- package/dist/factory.js +214 -159
- package/dist/guard.d.ts +3 -1
- package/dist/guard.js +14 -2
- package/dist/hook.d.ts +12 -0
- package/dist/hook.js +70 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/semantic.d.ts +2 -6
- package/dist/semantic.js +335 -185
- package/dist/statistics.d.ts +13 -17
- package/dist/statistics.js +204 -522
- package/dist/utility.d.ts +4 -0
- package/dist/utility.js +2 -1
- package/dist/window.d.ts +12 -0
- package/dist/window.js +60 -0
- package/package.json +1 -1
- package/readme.md +198 -21
package/dist/utility.d.ts
CHANGED
package/dist/utility.js
CHANGED
|
@@ -2,7 +2,7 @@ export let validate = (t) => {
|
|
|
2
2
|
return t !== null && t !== (void 0);
|
|
3
3
|
};
|
|
4
4
|
export let invalidate = (t) => {
|
|
5
|
-
return t === null || t ===
|
|
5
|
+
return t === null || t === (void 0);
|
|
6
6
|
};
|
|
7
7
|
;
|
|
8
8
|
;
|
|
@@ -15,3 +15,4 @@ export let invalidate = (t) => {
|
|
|
15
15
|
;
|
|
16
16
|
;
|
|
17
17
|
;
|
|
18
|
+
;
|
package/dist/window.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OrderedCollectable } from "./collectable";
|
|
2
|
+
import type { Semantic } from "./semantic";
|
|
3
|
+
import type { Comparator, Generator } from "./utility";
|
|
4
|
+
export declare class WindowCollectable<E> extends OrderedCollectable<E> {
|
|
5
|
+
protected readonly WindowCollectable: symbol;
|
|
6
|
+
constructor(generator: Generator<E>);
|
|
7
|
+
constructor(generator: Generator<E>, comparator: Comparator<E>);
|
|
8
|
+
[Symbol.iterator](): globalThis.Generator<E, void, undefined>;
|
|
9
|
+
[Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
|
|
10
|
+
slide(size: bigint, step?: bigint): Semantic<Semantic<E>>;
|
|
11
|
+
tumble(size: bigint): Semantic<Semantic<E>>;
|
|
12
|
+
}
|
package/dist/window.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { OrderedCollectable } from "./collectable";
|
|
2
|
+
import { useToAsyncGeneratorFunction, useToGeneratorFunction } from "./collector";
|
|
3
|
+
import { from } from "./factory";
|
|
4
|
+
import { useCompare } from "./hook";
|
|
5
|
+
import { WindowCollectableSymbol } from "./symbol";
|
|
6
|
+
export class WindowCollectable extends OrderedCollectable {
|
|
7
|
+
WindowCollectable = WindowCollectableSymbol;
|
|
8
|
+
constructor(parameter, comparator = useCompare) {
|
|
9
|
+
super(parameter, comparator);
|
|
10
|
+
}
|
|
11
|
+
*[Symbol.iterator]() {
|
|
12
|
+
try {
|
|
13
|
+
let collector = useToGeneratorFunction();
|
|
14
|
+
yield* collector.collect(this.source());
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
throw new Error("Uncaught error on Generator.");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async *[Symbol.asyncIterator]() {
|
|
21
|
+
try {
|
|
22
|
+
let collector = useToAsyncGeneratorFunction();
|
|
23
|
+
yield* collector.collect(this.source());
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw new Error("Uncaught error on AsyncGenerator.");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
slide(size, step = 1n) {
|
|
30
|
+
if (size > 0n && step > 0n) {
|
|
31
|
+
try {
|
|
32
|
+
let source = this.toArray();
|
|
33
|
+
let windows = [];
|
|
34
|
+
let windowStartIndex = 0n;
|
|
35
|
+
while (windowStartIndex < BigInt(source.length)) {
|
|
36
|
+
let windowEnd = windowStartIndex + size;
|
|
37
|
+
let window = source.slice(Number(windowStartIndex), Number(windowEnd));
|
|
38
|
+
if (window.length > 0) {
|
|
39
|
+
windows.push(window);
|
|
40
|
+
}
|
|
41
|
+
windowStartIndex += step;
|
|
42
|
+
}
|
|
43
|
+
return from(windows).map((window) => from(window));
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new Error("Invalid arguments.");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
throw new RangeError("Invalid arguments.");
|
|
50
|
+
}
|
|
51
|
+
tumble(size) {
|
|
52
|
+
try {
|
|
53
|
+
return this.slide(size, size);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
throw new Error("Invalid arguments.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
;
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"url": "https://github.com/eloyhere"
|
|
7
7
|
},
|
|
8
8
|
"description": "A modern type-safe stream processing library inspired by JavaScript Generator, Java Stream, and MySQL Index. Supports lazy evaluation, async streams, statistics, and IO-like operations.",
|
|
9
|
-
"version": "0.3.
|
|
9
|
+
"version": "0.3.7",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"readme": "readme.md",
|
|
12
12
|
"main": "dist/index.js",
|
package/readme.md
CHANGED
|
@@ -2,9 +2,59 @@
|
|
|
2
2
|
|
|
3
3
|
## Introduction
|
|
4
4
|
|
|
5
|
-
Semantic-TypeScript
|
|
5
|
+
**Semantic-TypeScript: A Paradigm-Shifting Stream Processing Library for the Modern Web**
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Semantic-TypeScript represents a significant advancement in stream processing technology, synthesising the most effective concepts from JavaScript GeneratorFunction, Java Stream, and MySQL Index paradigms. Its foundational design principle is centred on constructing exceptionally efficient data processing pipelines through sophisticated data indexing methodologies. The library delivers a rigorously type-safe, functionally pure streaming operation experience specifically engineered for contemporary front-end development.
|
|
8
|
+
|
|
9
|
+
In contrast to conventional synchronous processing architectures, Semantic-TypeScript implements a fully asynchronous processing model. During data stream generation, the temporal reception of data at the terminal is determined exclusively by the upstream invocation of the `accept` and `interrupt` callback mechanisms. This deliberate architectural choice enables the library to handle with exceptional grace:
|
|
10
|
+
|
|
11
|
+
- **Real-time data streams** with deterministic latency characteristics
|
|
12
|
+
- **Large-scale datasets** through memory-efficient processing pipelines
|
|
13
|
+
- **Asynchronous data sources** with guaranteed consistency and reliability
|
|
14
|
+
|
|
15
|
+
The library's innovative approach fundamentally reimagines how developers interact with streaming data, providing both unprecedented performance characteristics and developer ergonomics in a single, cohesive package.
|
|
16
|
+
|
|
17
|
+
### Why Choose Semantic-TypeScript?
|
|
18
|
+
|
|
19
|
+
Selecting the right library for data stream processing in a TypeScript project involves balancing performance, developer experience, and architectural fit. Semantic-TypeScript is engineered to excel across all these dimensions, offering a paradigm-shifting approach for the modern web. Here’s why it stands out.
|
|
20
|
+
|
|
21
|
+
#### 1. **A Unified, Type-Safe Paradigm for Streams**
|
|
22
|
+
Semantic-TypeScript synthesises the most effective concepts from **JavaScript GeneratorFunctions**, **Java Streams API**, and **database indexing strategies**. It provides a consistent, declarative API for processing any data sequence—be it static arrays, real-time events, or asynchronous chunks—while leveraging TypeScript's full power to ensure end-to-end type safety. This eliminates a whole class of runtime errors and transforms stream manipulation into a predictable, compiler-verified activity.
|
|
23
|
+
|
|
24
|
+
#### 2. **Uncompromising Performance with Intelligent Laziness**
|
|
25
|
+
At its core, the library is built on **lazy evaluation**. Operations like `filter`, `map`, and `flatMap` merely compose a processing pipeline; no work is done until a terminal operation (like `collect` or `toArray`) is invoked. This is coupled with **short-circuiting** capabilities (via operations like `limit`, `anyMatch`, or custom `interruptor` functions), which allows the processing to stop early when the result is known, dramatically improving efficiency for large or infinite streams.
|
|
26
|
+
|
|
27
|
+
#### 3. **Architectural Distinction: `Semantic<E>` vs. `Collectable<E>`**
|
|
28
|
+
This library introduces a crucial architectural separation that others often lack:
|
|
29
|
+
* **`Semantic<E>`**: Represents the abstract, lazy stream definition—the "blueprint" of your data transformation. It is immutable and composable.
|
|
30
|
+
* **`Collectable<E>`**: Represents a materialised, executable view of the stream, offering all terminal operations.
|
|
31
|
+
|
|
32
|
+
This separation enforces a clear mental model and allows for optimisations (e.g., an `UnorderedCollectable` can skip expensive sorting steps if order is not required).
|
|
33
|
+
|
|
34
|
+
#### 4. **The Power of the `Collector<E, A, R>` Pattern**
|
|
35
|
+
Inspired by Java, the `Collector` pattern is the engine of flexibility. It decouples the *specification* of how to accumulate stream elements from the *execution* of the stream itself. The library provides a rich set of built-in collectors (`useToArray`, `useGroupBy`, `useSummate`, etc.) for everyday tasks, while the pattern makes it trivial to implement your own complex, reusable reduction logic. This is far more powerful and composable than a fixed set of terminal methods.
|
|
36
|
+
|
|
37
|
+
#### 5. **First-Class Support for Modern Web & Async Data**
|
|
38
|
+
Semantic-TypeScript is designed for contemporary front-end and Node.js development. It offers native factory methods for modern web sources:
|
|
39
|
+
* `from(iterable)`, `range()` for static data.
|
|
40
|
+
* `interval()`, `animationFrame()` for time-based streams.
|
|
41
|
+
* `blob()` for chunked binary data processing.
|
|
42
|
+
* `websocket()` for real-time message streams.
|
|
43
|
+
Its foundational support for both `Iterable` and `AsyncIterable` protocols means it handles synchronous and asynchronous data flows with the same elegant API.
|
|
44
|
+
|
|
45
|
+
#### 6. **Beyond Basic Aggregation: Built-in Statistical Analysis**
|
|
46
|
+
Move beyond simple sums and averages. The library provides dedicated `NumericStatistics` and `BigIntStatistics` classes, offering immediate access to advanced statistical measures directly from your streams—**variance, standard deviation, median, quantiles, skewness, and kurtosis**. This turns complex data analysis into a one-liner, saving you from manual implementation or integrating another specialised library.
|
|
47
|
+
|
|
48
|
+
#### 7. **Designed for Developer Ergonomics**
|
|
49
|
+
* **Fluent, Chainable API**: Write complex data pipelines as readable, sequential chains of operations.
|
|
50
|
+
* **Comprehensive Utility Suite**: Comes with essential guards (`isOptional`, `isSemantic`), type-safe utilities (`useCompare`, `useTraverse`), and functional interfaces out of the box.
|
|
51
|
+
* **`Optional<T>` Integration**: Models the absence of a value safely, eliminating null-pointer concerns in find operations.
|
|
52
|
+
* **Performance Guidance**: The documentation clearly guides you on when to use `toUnordered()` (for maximum speed) versus `toOrdered()` (when sequence matters).
|
|
53
|
+
|
|
54
|
+
#### 8. **Robust, Community-Ready Foundation**
|
|
55
|
+
As an open-source library hosted on **GitHub** and distributed via **npm**, it is built for real-world use. The extensive documentation, complete with time/space complexity annotations and practical examples, lowers the learning curve and provides clarity for performance-sensitive applications.
|
|
56
|
+
|
|
57
|
+
**In summary, choose Semantic-TypeScript if you seek a rigorously designed, type-safe, and high-performance stream processing library that brings the power of enterprise-level data transformation patterns to the TypeScript ecosystem, without compromising on the idioms and needs of modern web development.**
|
|
8
58
|
|
|
9
59
|
## Installation
|
|
10
60
|
|
|
@@ -83,7 +133,9 @@ let comparator: Comparator<number> = (a: number, b: number): number => a - b;
|
|
|
83
133
|
| `isNumericStatistics(t: unknown): t is NumericStatistics<unknown>` | Check if it is a NumericStatistics instance | O(1) | O(1) |
|
|
84
134
|
| `isBigIntStatistics(t: unknown): t is BigIntStatistics<unknown>` | Check if it is a BigIntStatistics instance | O(1) | O(1) |
|
|
85
135
|
| `isPromise(t: unknown): t is Promise<unknown>` | Check if it is a Promise object | O(1) | O(1) |
|
|
86
|
-
| `
|
|
136
|
+
| `isAsyncFunction(t: unknown): t is AsyncFunction` | Check if it is an AsyncFunction | O(1) | O(1) |
|
|
137
|
+
| `isGeneratorFunction(t: unknown): t is GeneratorFunction` | Check if it is a GeneratorFunction | O(1) | O(1) |
|
|
138
|
+
| `isAsyncGeneratorFunction(t: unknown): t is AsyncGeneratorFunction` | Check if it is an AsyncGeneratorFunction | O(1) | O(1) |
|
|
87
139
|
|
|
88
140
|
```typescript
|
|
89
141
|
// Type guard usage examples
|
|
@@ -94,7 +146,7 @@ if (isString(value)) {
|
|
|
94
146
|
}
|
|
95
147
|
|
|
96
148
|
if (isOptional(someValue)) {
|
|
97
|
-
someValue.ifPresent((value): void => console.log(
|
|
149
|
+
someValue.ifPresent((value): void => console.log(value));
|
|
98
150
|
}
|
|
99
151
|
|
|
100
152
|
if(isIterable(value)){
|
|
@@ -112,6 +164,8 @@ if(isIterable(value)){
|
|
|
112
164
|
| `useCompare<T>(t1: T, t2: T): number` | Generic comparison function | O(1) | O(1) |
|
|
113
165
|
| `useRandom<T = number \| bigint>(index: T): T` | Pseudo-random number generator | O(log n) | O(1) |
|
|
114
166
|
| `useTraverse(t, callback)` | Deep traverse an object without cyclic references | O(n) | O(1) |
|
|
167
|
+
| `useToNumber(t: unknown): number` | Convert a value to a number | O(1) | O(1) |
|
|
168
|
+
| `useToBigInt(t: unknown): bigint` | Convert a value to a BigInt | O(1) | O(1) |
|
|
115
169
|
|
|
116
170
|
```typescript
|
|
117
171
|
// Utility function usage examples
|
|
@@ -140,6 +194,12 @@ useTraverse(o, (value, key): boolean => {
|
|
|
140
194
|
*/
|
|
141
195
|
return true; // Returns true to continue traversing.
|
|
142
196
|
});
|
|
197
|
+
|
|
198
|
+
let toBeResolved: object = {
|
|
199
|
+
[Symbol.toPrimitive]: () => 5
|
|
200
|
+
};
|
|
201
|
+
let resolvedNumber: number = useToNumber(toBeResolved); // 5
|
|
202
|
+
let resolvedBigInt: bigint = useToBigInt(toBeResolved); // 5n
|
|
143
203
|
```
|
|
144
204
|
|
|
145
205
|
## Factory Methods
|
|
@@ -160,7 +220,7 @@ let present: Optional<number> = Optional.of(42);
|
|
|
160
220
|
let nullable: Optional<string> = Optional.ofNullable<string>(null);
|
|
161
221
|
let nonNull: Optional<string> = Optional.ofNonNull("hello");
|
|
162
222
|
|
|
163
|
-
|
|
223
|
+
present.ifPresent((value: number): void => console.log(value)); // Outputs 42
|
|
164
224
|
console.log(empty.get(100)); // Outputs 100
|
|
165
225
|
```
|
|
166
226
|
|
|
@@ -175,9 +235,16 @@ console.log(empty.get(100)); // Outputs 100
|
|
|
175
235
|
| `useCollect<E, A, R>(identity, accumulator, finisher)` | Create a full collector with identity, accumulator, finisher | O(1) | O(1) |
|
|
176
236
|
| `useCollect<E, A, R>(identity, interruptor, accumulator, finisher)` | Create a shortable collector with identity, interruptor, accumulator, finisher | O(1) | O(1) |
|
|
177
237
|
| `useCount<E>()` | Create a full collector counting elements | O(n) | O(1) |
|
|
238
|
+
| `useError<E>()` | Create a collector printing an error | O(n) | O(1) |
|
|
239
|
+
| `useError<E>(accumulator)` | Create a collector printing an accumulated error | O(n) | O(1) |
|
|
240
|
+
| `useError<E>(prefix, accumulator, suffix)` | Create a collector printing an accumulated error with prefix and suffix | O(n) | O(1) |
|
|
178
241
|
| `useFindFirst<E>()` | Create a shortable collector returning the first element | O(n) | O(1) |
|
|
179
242
|
| `useFindAny<E>()` | Create a shortable collector returning any element | O(n) | O(1) |
|
|
180
243
|
| `useFindLast<E>()` | Create a full collector returning the last element | O(n) | O(1) |
|
|
244
|
+
| `useFindMaximum<E>()` | Create a full collector returning the maximum element | O(n) | O(1) |
|
|
245
|
+
| `useFindMaximum<E>(comparator?)` | Create a full collector returning the maximum element | O(n) | O(1) |
|
|
246
|
+
| `useFindMinimum<E>()` | Create a full collector returning the minimum element | O(n) | O(1) |
|
|
247
|
+
| `useFindMinimum<E>(comparator?)` | Create a full collector returning the minimum element | O(n) | O(1) |
|
|
181
248
|
| `useForEach<E>(action)` | Create a full collector executing an action for each element | O(n) | O(1) |
|
|
182
249
|
| `useNoneMatch<E>(predicate)` | Create a shortable collector returning true if no element matches the predicate | O(n) | O(1) |
|
|
183
250
|
| `useGroup<E, K>(classifier)` | Create a full collector grouping elements by classifier key | O(n) | O(n) |
|
|
@@ -204,8 +271,33 @@ console.log(empty.get(100)); // Outputs 100
|
|
|
204
271
|
| `useBigIntAverage<E>(mapper)` | Create a full collector computing bigint average with mapper | O(n) | O(1) |
|
|
205
272
|
| `useBigIntAverage<E>()` | Create a full collector computing bigint average | O(n) | O(1) |
|
|
206
273
|
| `useFrequency<E>()` | Create a full collector counting element frequencies | O(n) | O(n) |
|
|
207
|
-
| `
|
|
208
|
-
| `
|
|
274
|
+
| `useNumericSummate<E>()` | Create a full collector summing numeric elements | O(n) | O(1) |
|
|
275
|
+
| `useNumericSummate<E>(mapper)` | Create a full collector summing mapped numeric values | O(n) | O(1) |
|
|
276
|
+
| `useBigIntSummate<E>()` | Create a full collector summing numeric elements | O(n) | O(1) |
|
|
277
|
+
| `useBigIntSummate<E>(mapper)` | Create a full collector summing mapped numeric values | O(n) | O(1) |
|
|
278
|
+
| `useNumericAverage<E>()` | Create a full collector computing numeric average | O(n) | O(1) |
|
|
279
|
+
| `useNumericAverage<E>(mapper)` | Create a full collector computing numeric average with mapper | O(n) | O(1) |
|
|
280
|
+
| `useBigIntAverage<E>()` | Create a full collector computing bigint average | O(n) | O(1) |
|
|
281
|
+
| `useBigIntAverage<E>(mapper)` | Create a full collector computing bigint average with mapper | O(n) | O(1) |
|
|
282
|
+
| `useFrequency<E>()` | Create a full collector counting element frequencies | O(n) | O(n) |
|
|
283
|
+
| `useNumericMode<E>()` | Create a full collector computing numeric mode | O(n) | O(n) |
|
|
284
|
+
| `useNumericMode<E>(mapper)` | Create a full collector computing numeric mode with mapper | O(n) | O(n) |
|
|
285
|
+
| `useBigIntMode<E>()` | Create a full collector computing bigint mode | O(n) | O(n) |
|
|
286
|
+
| `useBigIntMode<E>(mapper)` | Create a full collector computing bigint mode with mapper | O(n) | O(n) |
|
|
287
|
+
| `useNumericVariance<E>()` | Create a full collector computing numeric variance | O(n) | O(1) |
|
|
288
|
+
| `useNumericVariance<E>(mapper)` | Create a full collector computing numeric variance with mapper | O(n) | O(1) |
|
|
289
|
+
| `useBigIntVariance<E>()` | Create a full collector computing bigint variance | O(n) | O(1) |
|
|
290
|
+
| `useBigIntVariance<E>(mapper)` | Create a full collector computing bigint variance with mapper | O(n) | O(1) |
|
|
291
|
+
| `useNumericStandardDeviation<E>()` | Create a full collector computing numeric standard deviation | O(n) | O(1) |
|
|
292
|
+
| `useNumericStandardDeviation<E>(mapper)` | Create a full collector computing numeric standard deviation with mapper | O(n) | O(1) |
|
|
293
|
+
| `useBigIntStandardDeviation<E>()` | Create a full collector computing bigint standard deviation | O(n) | O(1) |
|
|
294
|
+
| `useBigIntStandardDeviation<E>(mapper)` | Create a full collector computing bigint standard deviation with mapper | O(n) | O(1) |
|
|
295
|
+
| `useNumericMedian<E>()` | Create a full collector computing numeric median | O(n) | O(1) |
|
|
296
|
+
| `useNumericMedian<E>(mapper)` | Create a full collector computing numeric median with mapper | O(n) | O(1) |
|
|
297
|
+
| `useBigIntMedian<E>()` | Create a full collector computing bigint median | O(n) | O(1) |
|
|
298
|
+
| `useBigIntMedian<E>(mapper)` | Create a full collector computing bigint median with mapper | O(n) | O(1) |
|
|
299
|
+
| `useToGeneratorFunction<E>()` | Create a full collector converting a stream to a generator function | O(n) | O(1) |
|
|
300
|
+
| `useToAsyncGeneratorFunction<E>()` | Create a full collector converting a stream to an async generator function | O(n) | O(1) |
|
|
209
301
|
|
|
210
302
|
```typescript
|
|
211
303
|
// Collector conversion examples
|
|
@@ -264,6 +356,7 @@ average.collect([1,2,3,4,5]); // Averages from an iterable object
|
|
|
264
356
|
| `from<E>(iterable)` | Create a stream from an iterable object | O(1) | O(1) |
|
|
265
357
|
| `interval(period, delay?)` | Create a timed interval stream | O(1)* | O(1) |
|
|
266
358
|
| `iterate<E>(generator)` | Create a stream from a generator | O(1) | O(1) |
|
|
359
|
+
| `promise<E>(promise)` | Create a stream from a promise | O(1) | O(1) |
|
|
267
360
|
| `range(start, end, step)` | Create a numerical range stream | O(n) | O(1) |
|
|
268
361
|
| `websocket(websocket)` | Create a stream from a WebSocket | O(1) | O(1) |
|
|
269
362
|
|
|
@@ -273,7 +366,7 @@ average.collect([1,2,3,4,5]); // Averages from an iterable object
|
|
|
273
366
|
// Create a stream from a timed animation frame
|
|
274
367
|
animationFrame(1000)
|
|
275
368
|
.toUnordered()
|
|
276
|
-
.forEach(frame => console.log(frame));
|
|
369
|
+
.forEach((frame): void => console.log(frame));
|
|
277
370
|
|
|
278
371
|
// Create a stream from a Blob (chunked reading)
|
|
279
372
|
blob(someBlob, 1024n)
|
|
@@ -298,7 +391,7 @@ let numberStream = from([1, 2, 3, 4, 5]);
|
|
|
298
391
|
let stringStream = from(new Set(["Alex", "Bob"]));
|
|
299
392
|
|
|
300
393
|
// Create a stream from a resolved Promise
|
|
301
|
-
let promisedStream: Semantic<Array<number>> = Promise.resolve([1, 2, 3, 4, 5]);
|
|
394
|
+
let promisedStream: Semantic<Array<number>> = promise(Promise.resolve([1, 2, 3, 4, 5]));
|
|
302
395
|
|
|
303
396
|
// Create a range stream
|
|
304
397
|
let rangeStream = range(1, 10, 2); // 1, 3, 5, 7, 9
|
|
@@ -364,6 +457,7 @@ let complexResult = range(1, 100, 1)
|
|
|
364
457
|
| Method | Description | Time Complexity | Space Complexity |
|
|
365
458
|
|------------|------------|------------|------------|
|
|
366
459
|
| `sorted()` | Convert to ordered collector | O(n log n) | O(n) |
|
|
460
|
+
| `sorted(comparator)` | Convert to ordered collector with comparator | O(n log n) | O(n) |
|
|
367
461
|
| `toUnordered()` | Convert to unordered collector | O(1) | O(1) |
|
|
368
462
|
| `toOrdered()` | Convert to ordered collector | O(1) | O(1) |
|
|
369
463
|
| `toNumericStatistics()` | Convert to numeric statistics | O(n) | O(1) |
|
|
@@ -437,6 +531,10 @@ let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Genera
|
|
|
437
531
|
| `findAny()` | Find any element in the collectable | O(n) | O(1) |
|
|
438
532
|
| `findFirst()` | Find the first element in the collectable | O(n) | O(1) |
|
|
439
533
|
| `findLast()` | Find the last element in the collectable | O(n) | O(1) |
|
|
534
|
+
| `findMaximum()` | Find the maximum element in the collectable | O(n) | O(1) |
|
|
535
|
+
| `findMaximum(comparator)` | Find the maximum element in the collectable using a comparator | O(n) | O(1) |
|
|
536
|
+
| `findMinimum()` | Find the minimum element in the collectable | O(n) | O(1) |
|
|
537
|
+
| `findMinimum(comparator)` | Find the minimum element in the collectable using a comparator | O(n) | O(1) |
|
|
440
538
|
| `forEach(action)` | Iterate over all elements with a consumer or bi-consumer | O(n) | O(1) |
|
|
441
539
|
| `group(classifier)` | Group elements by a classifier function | O(n) | O(n) |
|
|
442
540
|
| `groupBy(keyExtractor, valueExtractor)` | Group elements by key and value extractors | O(n) | O(n) |
|
|
@@ -454,6 +552,7 @@ let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Genera
|
|
|
454
552
|
| `reduce(identity, accumulator)` | Reduce elements with an initial value and accumulator | O(n) | O(1) |
|
|
455
553
|
| `reduce(identity, accumulator, finisher)` | Reduce elements with initial value, accumulator, and finisher | O(n) | O(1) |
|
|
456
554
|
| `semantic()` | Convert the collectable to a semantic object | O(1) | O(1) |
|
|
555
|
+
| `source()` | Get the source of the collectable | O(1) | O(1) |
|
|
457
556
|
| `toArray()` | Convert elements to an array | O(n) | O(n) |
|
|
458
557
|
| `toMap(keyExtractor, valueExtractor)` | Convert elements to a Map via key and value extractors | O(n) | O(n) |
|
|
459
558
|
| `toSet()` | Convert elements to a Set | O(n) | O(n) |
|
|
@@ -488,24 +587,61 @@ let sum = data.reduce(0, (accumulator: number, n: number): number => accumulator
|
|
|
488
587
|
data.join(", "); // "[2, 4, 6, 8, 10]"
|
|
489
588
|
```
|
|
490
589
|
|
|
590
|
+
## Unordered Collectable Methods
|
|
591
|
+
|
|
592
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
593
|
+
|------------|------------|------------|------------|
|
|
594
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
595
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
596
|
+
|
|
597
|
+
## Ordered Collectable Methods
|
|
598
|
+
|
|
599
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
600
|
+
|------------|------------|------------|------------|
|
|
601
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
602
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
603
|
+
|
|
491
604
|
## Statistical Analysis Methods
|
|
492
605
|
|
|
606
|
+
### Statisttics Methods
|
|
607
|
+
|
|
608
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
609
|
+
|------------|------------|------------|------------|
|
|
610
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
611
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
612
|
+
| `count()` | Count the number of elements | O(n) | O(1) |
|
|
613
|
+
| `frequency()` | Frequency distribution | O(n) | O(n) |
|
|
614
|
+
|
|
493
615
|
### NumericStatistics Methods
|
|
494
616
|
|
|
495
617
|
| Method | Description | Time Complexity | Space Complexity |
|
|
496
618
|
|------|------|------------|------------|
|
|
497
|
-
|
|
|
498
|
-
|
|
|
499
|
-
| `
|
|
500
|
-
| `
|
|
501
|
-
| `
|
|
502
|
-
| `
|
|
503
|
-
| `
|
|
504
|
-
| `
|
|
505
|
-
| `
|
|
506
|
-
| `
|
|
507
|
-
| `
|
|
508
|
-
| `
|
|
619
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
620
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
621
|
+
| `average()` | Calculate the average of elements | O(n) | O(1) |
|
|
622
|
+
| `average(mapper)` | Calculate the average of elements using a mapper | O(n) | O(1) |
|
|
623
|
+
| `range()` | Calculate the range of elements | O(n) | O(1) |
|
|
624
|
+
| `range(mapper)` | Calculate the range of elements using a mapper | O(n) | O(1) |
|
|
625
|
+
| `variance()` | Calculate the variance of elements | O(n) | O(1) |
|
|
626
|
+
| `variance(mapper)` | Calculate the variance of elements using a mapper | O(n) | O(1) |
|
|
627
|
+
| `standardDeviation()` | Calculate the standard deviation of elements | O(n) | O(1) |
|
|
628
|
+
| `standardDeviation(mapper)` | Calculate the standard deviation of elements using a mapper | O(n) | O(1) |
|
|
629
|
+
| `mean()` | Calculate the mean of elements | O(n) | O(1) |
|
|
630
|
+
| `mean(mapper)` | Calculate the mean of elements using a mapper | O(n) | O(1) |
|
|
631
|
+
| `median()` | Calculate the median of elements | O(n) | O(1) |
|
|
632
|
+
| `median(mapper)` | Calculate the median of elements using a mapper | O(n) | O(1) |
|
|
633
|
+
| `mode()` | Calculate the mode of elements | O(n) | O(1) |
|
|
634
|
+
| `mode(mapper)` | Calculate the mode of elements using a mapper | O(n) | O(1) |
|
|
635
|
+
| `summate()` | Calculate the sum of elements | O(n) | O(1) |
|
|
636
|
+
| `summate(mapper)` | Calculate the sum of elements using a mapper | O(n) | O(1) |
|
|
637
|
+
| `quantile(quantile)` | Calculate the quantile of elements | O(n) | O(1) |
|
|
638
|
+
| `quantile(quantile, mapper)` | Calculate the quantile of elements using a mapper | O(n) | O(1) |
|
|
639
|
+
| `interquartileRange()` | Calculate the interquartile range of elements | O(n) | O(1) |
|
|
640
|
+
| `interquartileRange(mapper)` | Calculate the interquartile range of elements using a mapper | O(n) | O(1) |
|
|
641
|
+
| `skewness()` | Calculate the percentile of elements | O(n) | O(1) |
|
|
642
|
+
| `skewness(mapper)` | Calculate the percentile of elements using a mapper | O(n) | O(1) |
|
|
643
|
+
| `kurtosis()` | Calculate the kurtosis of elements | O(n) | O(1) |
|
|
644
|
+
| `kurtosis(mapper)` | Calculate the kurtosis of elements using a mapper | O(n) | O(1) |
|
|
509
645
|
|
|
510
646
|
```typescript
|
|
511
647
|
// Statistical analysis examples
|
|
@@ -527,6 +663,47 @@ let objects = from([
|
|
|
527
663
|
console.log("Mapped mean:", objects.mean(obj => obj.value)); // 20
|
|
528
664
|
```
|
|
529
665
|
|
|
666
|
+
### BigintStatistics Methods
|
|
667
|
+
|
|
668
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
669
|
+
|------------|------------|------------|------------|
|
|
670
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
671
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
672
|
+
| `average()` | Calculate the average of elements | O(n) | O(1) |
|
|
673
|
+
| `average(mapper)` | Calculate the average of elements using a mapper | O(n) | O(1) |
|
|
674
|
+
| `range()` | Calculate the range of elements | O(n) | O(1) |
|
|
675
|
+
| `range(mapper)` | Calculate the range of elements using a mapper | O(n) | O(1) |
|
|
676
|
+
| `variance()` | Calculate the variance of elements | O(n) | O(1) |
|
|
677
|
+
| `variance(mapper)` | Calculate the variance of elements using a mapper | O(n) | O(1) |
|
|
678
|
+
| `standardDeviation()` | Calculate the standard deviation of elements | O(n) | O(1) |
|
|
679
|
+
| `standardDeviation(mapper)` | Calculate the standard deviation of elements using a mapper | O(n) | O(1) |
|
|
680
|
+
| `mean()` | Calculate the mean of elements | O(n) | O(1) |
|
|
681
|
+
| `mean(mapper)` | Calculate the mean of elements using a mapper | O(n) | O(1) |
|
|
682
|
+
| `median()` | Calculate the median of elements | O(n) | O(1) |
|
|
683
|
+
| `median(mapper)` | Calculate the median of elements using a mapper | O(n) | O(1) |
|
|
684
|
+
| `mode()` | Calculate the mode of elements | O(n) | O(1) |
|
|
685
|
+
| `mode(mapper)` | Calculate the mode of elements using a mapper | O(n) | O(1) |
|
|
686
|
+
| `summate()` | Calculate the sum of elements | O(n) | O(1) |
|
|
687
|
+
| `summate(mapper)` | Calculate the sum of elements using a mapper | O(n) | O(1) |
|
|
688
|
+
| `quantile(quantile)` | Calculate the quantile of elements | O(n) | O(1) |
|
|
689
|
+
| `quantile(quantile, mapper)` | Calculate the quantile of elements using a mapper | O(n) | O(1) |
|
|
690
|
+
| `interquartileRange()` | Calculate the interquartile range of elements | O(n) | O(1) |
|
|
691
|
+
| `interquartileRange(mapper)` | Calculate the interquartile range of elements using a mapper | O(n) | O(1) |
|
|
692
|
+
| `skewness()` | Calculate the percentile of elements | O(n) | O(1) |
|
|
693
|
+
| `skewness(mapper)` | Calculate the percentile of elements using a mapper | O(n) | O(1) |
|
|
694
|
+
| `kurtosis()` | Calculate the kurtosis of elements | O(n) | O(1) |
|
|
695
|
+
| `kurtosis(mapper)` | Calculate the kurtosis of elements using a mapper | O(n) | O(1) |
|
|
696
|
+
|
|
697
|
+
### Window Collectable Methods
|
|
698
|
+
|
|
699
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
700
|
+
|------------|------------|------------|------------|
|
|
701
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
702
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
703
|
+
| `slide(size)` | Slide a window of a given size | O(n) | O(1) |
|
|
704
|
+
| `slide(size, step)` | Slide a window of a given size and step | O(n) | O(1) |
|
|
705
|
+
| `tumble(size)` | Tumble a window of a given size | O(n) | O(1) |
|
|
706
|
+
|
|
530
707
|
## Performance Selection Guide
|
|
531
708
|
|
|
532
709
|
### Choose Unordered Collector (Performance First)
|