@thi.ng/transducers 8.1.3 → 8.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -1
- package/package.json +2 -2
- package/stream-shuffle.d.ts +29 -5
- package/stream-shuffle.js +22 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2021-12-
|
|
3
|
+
- **Last updated**: 2021-12-14T09:12:13Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,16 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [8.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/transducers@8.2.0) (2021-12-14)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- update/fix/extend streamShuffle() ([dc26203](https://github.com/thi-ng/umbrella/commit/dc26203))
|
|
17
|
+
- add StreamShuffleOpts, add IRandom opt
|
|
18
|
+
- fix args for internal shuffle() calls
|
|
19
|
+
- optimize number of shuffle calls
|
|
20
|
+
- optimize final buffer drain
|
|
21
|
+
|
|
12
22
|
## [8.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/transducers@8.1.0) (2021-11-17)
|
|
13
23
|
|
|
14
24
|
#### 🚀 Features
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/transducers",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.2.0",
|
|
4
4
|
"description": "Lightweight transducer implementations for ES6 / TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -567,5 +567,5 @@
|
|
|
567
567
|
],
|
|
568
568
|
"year": 2016
|
|
569
569
|
},
|
|
570
|
-
"gitHead": "
|
|
570
|
+
"gitHead": "1ba92c6b9509e74e509b4c0b875fc380a97bbbc1\n"
|
|
571
571
|
}
|
package/stream-shuffle.d.ts
CHANGED
|
@@ -1,20 +1,44 @@
|
|
|
1
|
+
import type { IRandom } from "@thi.ng/random";
|
|
1
2
|
import type { Transducer } from "./api.js";
|
|
3
|
+
export interface StreamShuffleOpts {
|
|
4
|
+
/**
|
|
5
|
+
* Sliding window size
|
|
6
|
+
*/
|
|
7
|
+
n: number;
|
|
8
|
+
/**
|
|
9
|
+
* Max. shuffle ops per new input (in [0..n] range)
|
|
10
|
+
*
|
|
11
|
+
* @defaultValue same as `n`
|
|
12
|
+
*/
|
|
13
|
+
max?: number;
|
|
14
|
+
/**
|
|
15
|
+
* PRNG instance to use for shuffling
|
|
16
|
+
*
|
|
17
|
+
* @defaultValue SYSTEM
|
|
18
|
+
*/
|
|
19
|
+
rnd?: IRandom;
|
|
20
|
+
}
|
|
2
21
|
/**
|
|
3
|
-
* Transducer. Creates internal sliding window of `n` values and
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
22
|
+
* Transducer. Creates internal sliding window of `n` values and performs
|
|
23
|
+
* `maxSwaps` random shuffle operations for each new value and yields values in
|
|
24
|
+
* shuffled order. By default `maxSwaps` is the same as the chosen chunk size.
|
|
25
|
+
* If given a {@link StreamShuffleOpts} options object, further configurations
|
|
26
|
+
* are possible.
|
|
7
27
|
*
|
|
8
28
|
* @example
|
|
9
29
|
* ```ts
|
|
10
30
|
* [...streamShuffle(5, range(10))]
|
|
11
31
|
* // [ 3, 2, 5, 0, 8, 7, 1, 6, 4, 9 ]
|
|
32
|
+
*
|
|
33
|
+
* [...streamShuffle({ n: 5, rnd: new XsAdd(12345) }, range(10))]
|
|
34
|
+
* [ 0, 4, 3, 7, 8, 1, 5, 2, 6, 9 ]
|
|
12
35
|
* ```
|
|
13
36
|
*
|
|
14
37
|
* @param n - sliding window size
|
|
15
38
|
* @param maxSwaps - number of swaps per input
|
|
16
39
|
*/
|
|
17
40
|
export declare function streamShuffle<T>(n: number, maxSwaps?: number): Transducer<T, T>;
|
|
18
|
-
export declare function streamShuffle<T>(
|
|
41
|
+
export declare function streamShuffle<T>(opts: StreamShuffleOpts): Transducer<T, T>;
|
|
42
|
+
export declare function streamShuffle<T>(opts: number | StreamShuffleOpts, src: Iterable<T>): IterableIterator<T>;
|
|
19
43
|
export declare function streamShuffle<T>(n: number, maxSwaps: number, src: Iterable<T>): IterableIterator<T>;
|
|
20
44
|
//# sourceMappingURL=stream-shuffle.d.ts.map
|
package/stream-shuffle.js
CHANGED
|
@@ -1,26 +1,42 @@
|
|
|
1
1
|
import { shuffle } from "@thi.ng/arrays/shuffle";
|
|
2
|
+
import { isPlainObject } from "@thi.ng/checks";
|
|
3
|
+
import { SYSTEM } from "@thi.ng/random/system";
|
|
2
4
|
import { __iter, iterator } from "./iterator.js";
|
|
3
5
|
import { isReduced } from "./reduced.js";
|
|
4
6
|
export function streamShuffle(...args) {
|
|
5
7
|
return (__iter(streamShuffle, args, iterator) ||
|
|
6
8
|
(([init, complete, reduce]) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
let n;
|
|
10
|
+
let maxSwaps;
|
|
11
|
+
let rnd = SYSTEM;
|
|
12
|
+
const opts = args[0];
|
|
13
|
+
if (isPlainObject(opts)) {
|
|
14
|
+
n = opts.n;
|
|
15
|
+
maxSwaps = opts.max || n;
|
|
16
|
+
opts.rnd && (rnd = opts.rnd);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
n = args[0];
|
|
20
|
+
maxSwaps = args[1] || n;
|
|
21
|
+
}
|
|
9
22
|
const buf = [];
|
|
10
23
|
return [
|
|
11
24
|
init,
|
|
12
25
|
(acc) => {
|
|
13
|
-
|
|
14
|
-
shuffle(buf, maxSwaps);
|
|
15
|
-
|
|
26
|
+
if (buf.length) {
|
|
27
|
+
shuffle(buf, Math.min(maxSwaps, buf.length), rnd);
|
|
28
|
+
for (let i = 0, n = buf.length; i < n && !isReduced(acc); i++) {
|
|
29
|
+
acc = reduce(acc, buf[i]);
|
|
30
|
+
}
|
|
16
31
|
}
|
|
32
|
+
buf.length = 0;
|
|
17
33
|
acc = complete(acc);
|
|
18
34
|
return acc;
|
|
19
35
|
},
|
|
20
36
|
(acc, x) => {
|
|
21
37
|
buf.push(x);
|
|
22
|
-
shuffle(buf, maxSwaps);
|
|
23
38
|
if (buf.length === n) {
|
|
39
|
+
shuffle(buf, Math.min(maxSwaps, n), rnd);
|
|
24
40
|
acc = reduce(acc, buf.shift());
|
|
25
41
|
}
|
|
26
42
|
return acc;
|