@thi.ng/transducers 8.9.7 → 8.9.9
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 +1 -1
- package/README.md +58 -15
- package/flatten1.d.ts +3 -1
- package/mapcat.d.ts +3 -1
- package/package.json +10 -10
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
1
|
<!-- This file is generated - DO NOT EDIT! -->
|
|
2
2
|
<!-- Please see: https://github.com/thi-ng/umbrella/blob/develop/CONTRIBUTING.md#changes-to-readme-files -->
|
|
3
|
-
> [!IMPORTANT]
|
|
4
|
-
> ‼️ Announcing the thi.ng user survey 2024 📋
|
|
5
|
-
>
|
|
6
|
-
> [Please participate in the survey here!](https://forms.gle/XacbSDEmQMPZg8197)\
|
|
7
|
-
> (open until end of February)
|
|
8
|
-
>
|
|
9
|
-
> **To achieve a better sample size, I'd highly appreciate if you could
|
|
10
|
-
> circulate the link to this survey in your own networks.**
|
|
11
|
-
>
|
|
12
|
-
> [Discussion](https://github.com/thi-ng/umbrella/discussions/447)
|
|
13
|
-
|
|
14
3
|
# 
|
|
15
4
|
|
|
16
5
|
[](https://www.npmjs.com/package/@thi.ng/transducers)
|
|
@@ -22,7 +11,7 @@
|
|
|
22
11
|
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
|
|
23
12
|
> and anti-framework.
|
|
24
13
|
>
|
|
25
|
-
> 🚀
|
|
14
|
+
> 🚀 Please help me to work full-time on these projects by [sponsoring me on
|
|
26
15
|
> GitHub](https://github.com/sponsors/postspectacular). Thank you! ❤️
|
|
27
16
|
|
|
28
17
|
- [About](#about)
|
|
@@ -280,6 +269,8 @@ directory are using this package:
|
|
|
280
269
|
### Basic usage patterns
|
|
281
270
|
|
|
282
271
|
```ts
|
|
272
|
+
import { comp, distinct, filter, map } from "@thi.ng/transducers";
|
|
273
|
+
|
|
283
274
|
// compose transducer
|
|
284
275
|
xform = comp(
|
|
285
276
|
filter((x) => (x & 1) > 0), // odd numbers only
|
|
@@ -287,6 +278,8 @@ xform = comp(
|
|
|
287
278
|
map((x) => x * 3) // times 3
|
|
288
279
|
);
|
|
289
280
|
|
|
281
|
+
import { transduce, push } from "@thi.ng/transducers";
|
|
282
|
+
|
|
290
283
|
// collect into array (push)
|
|
291
284
|
transduce(xform, push(), [1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
|
292
285
|
// [ 3, 9, 15 ]
|
|
@@ -295,6 +288,8 @@ transduce(xform, push(), [1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
|
|
295
288
|
transduce(xform, conj(), [1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
|
296
289
|
// Set { 3, 9, 15 }
|
|
297
290
|
|
|
291
|
+
import { iterator } from "@thi.ng/transducers";
|
|
292
|
+
|
|
298
293
|
// or apply as transforming iterator
|
|
299
294
|
// no reduction, only transformations
|
|
300
295
|
[...iterator(xform, [1, 2, 3, 4, 5])]
|
|
@@ -305,6 +300,8 @@ transduce(xform, conj(), [1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
|
|
305
300
|
[...filter((x) => /[A-Z]/.test(x), "Hello World!")]
|
|
306
301
|
// ["H", "W"]
|
|
307
302
|
|
|
303
|
+
import { step } from "@thi.ng/transducers";
|
|
304
|
+
|
|
308
305
|
// single step execution
|
|
309
306
|
// returns undefined if transducer returned no result for this input
|
|
310
307
|
// returns array if transducer step produced multiple results
|
|
@@ -358,6 +355,8 @@ asSvg(
|
|
|
358
355
|
### Fuzzy search
|
|
359
356
|
|
|
360
357
|
```ts
|
|
358
|
+
import { filterFuzzy } from "@thi.ng/transducers";
|
|
359
|
+
|
|
361
360
|
[...filterFuzzy("ho", ["hello", "hallo", "hey", "heyoka"])]
|
|
362
361
|
// ["hello", "hallo", "heyoka"]
|
|
363
362
|
[...filterFuzzy("hlo", ["hello", "hallo", "hey", "heyoka"])]
|
|
@@ -380,6 +379,8 @@ asSvg(
|
|
|
380
379
|
### Histogram generation & result grouping
|
|
381
380
|
|
|
382
381
|
```ts
|
|
382
|
+
import { frequencies, map, reduce, transduce } from "@thi.ng/transducers";
|
|
383
|
+
|
|
383
384
|
// use the `frequencies` reducer to create
|
|
384
385
|
// a map counting occurrence of each value
|
|
385
386
|
transduce(map((x) => x.toUpperCase()), frequencies(), "hello world");
|
|
@@ -399,6 +400,10 @@ frequencies(
|
|
|
399
400
|
"my camel is collapsing and needs some water".split(" ")
|
|
400
401
|
);
|
|
401
402
|
// Map { 2 => 2, 5 => 3, 10 => 1, 3 => 1, 4 => 1 }
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
```ts
|
|
406
|
+
import { groupByMap } from "@thi.ng/transducers";
|
|
402
407
|
|
|
403
408
|
// actual grouping (here: by word length)
|
|
404
409
|
groupByMap(
|
|
@@ -417,6 +422,8 @@ groupByMap(
|
|
|
417
422
|
### Pagination
|
|
418
423
|
|
|
419
424
|
```ts
|
|
425
|
+
import { page, comp, iterator, map, padLast, range } from "@thi.ng/transducers";
|
|
426
|
+
|
|
420
427
|
// extract only items for given page id & page length
|
|
421
428
|
[...page(0, 5, range(12))]
|
|
422
429
|
// [ 0, 1, 2, 3, 4 ]
|
|
@@ -443,6 +450,8 @@ parallel using the provided transducers (which can be composed as usual)
|
|
|
443
450
|
and results in a tuple or keyed object.
|
|
444
451
|
|
|
445
452
|
```ts
|
|
453
|
+
import { map, multiplex, multiplexObj, push, transduce } from "@thi.ng/transducers";
|
|
454
|
+
|
|
446
455
|
transduce(
|
|
447
456
|
multiplex(
|
|
448
457
|
map((x) => x.charAt(0)),
|
|
@@ -471,6 +480,8 @@ transduce(
|
|
|
471
480
|
### Moving average using sliding window
|
|
472
481
|
|
|
473
482
|
```ts
|
|
483
|
+
import { comp, map, mean, partition, push, reduce transduce } from "@thi.ng/transducers";
|
|
484
|
+
|
|
474
485
|
// use nested reduce to compute window averages
|
|
475
486
|
transduce(
|
|
476
487
|
comp(
|
|
@@ -481,9 +492,13 @@ transduce(
|
|
|
481
492
|
[1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10]
|
|
482
493
|
)
|
|
483
494
|
// [ 2.6, 3.4, 4, 4.6, 5.4, 6.2, 6.8, 7.6, 8.4 ]
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
This combined transducer is also directly available as:
|
|
498
|
+
|
|
499
|
+
```ts
|
|
500
|
+
import { movingAverage } from "@thi.ng/transducers";
|
|
484
501
|
|
|
485
|
-
// this combined transducer is also directly
|
|
486
|
-
// available as: `movingAverage(n)`
|
|
487
502
|
[...movingAverage(5, [1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10])]
|
|
488
503
|
// [ 2.6, 3.4, 4, 4.6, 5.4, 6.2, 6.8, 7.6, 8.4 ]
|
|
489
504
|
```
|
|
@@ -491,6 +506,8 @@ transduce(
|
|
|
491
506
|
### Benchmark function execution time
|
|
492
507
|
|
|
493
508
|
```ts
|
|
509
|
+
import { benchmark, mean, repeatedly, transduce } from "@thi.ng/transducers";
|
|
510
|
+
|
|
494
511
|
// function to test
|
|
495
512
|
fn = () => {
|
|
496
513
|
let x;
|
|
@@ -508,7 +525,9 @@ transduce(benchmark(), mean(), repeatedly(fn, 100));
|
|
|
508
525
|
### Apply inspectors to debug transducer pipeline
|
|
509
526
|
|
|
510
527
|
```ts
|
|
511
|
-
|
|
528
|
+
import { comp, filter, map, push, trace, transduce } from "@thi.ng/transducers";
|
|
529
|
+
|
|
530
|
+
// alternatively, use sideEffect() for arbitrary side fx
|
|
512
531
|
transduce(
|
|
513
532
|
comp(
|
|
514
533
|
trace("orig"),
|
|
@@ -536,6 +555,8 @@ The `struct` transducer is simply a composition of: `partitionOf -> partition ->
|
|
|
536
555
|
here](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/struct.ts).
|
|
537
556
|
|
|
538
557
|
```ts
|
|
558
|
+
import { struct } from "@thi.ng/transducers";
|
|
559
|
+
|
|
539
560
|
// Higher-order transducer to convert linear input into structured objects
|
|
540
561
|
// using given field specs and ordering. A single field spec is an array of
|
|
541
562
|
// 2 or 3 items: `[name, size, transform?]`. If `transform` is given, it will
|
|
@@ -560,6 +581,8 @@ here](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/s
|
|
|
560
581
|
### CSV parsing
|
|
561
582
|
|
|
562
583
|
```ts
|
|
584
|
+
import { comp, map, mapcat, push, rename, transduce } from "@thi.ng/transducers";
|
|
585
|
+
|
|
563
586
|
transduce(
|
|
564
587
|
comp(
|
|
565
588
|
// split into rows
|
|
@@ -580,6 +603,8 @@ transduce(
|
|
|
580
603
|
### Early termination
|
|
581
604
|
|
|
582
605
|
```ts
|
|
606
|
+
import { comp, flatten, push, take, transduce } from "@thi.ng/transducers";
|
|
607
|
+
|
|
583
608
|
// result is realized after max. 7 values, irrespective of nesting
|
|
584
609
|
transduce(comp(flatten(), take(7)), push(), [
|
|
585
610
|
1,
|
|
@@ -591,6 +616,10 @@ transduce(comp(flatten(), take(7)), push(), [
|
|
|
591
616
|
### Scan operator
|
|
592
617
|
|
|
593
618
|
```ts
|
|
619
|
+
import {
|
|
620
|
+
comp, count, iterator, map, push, pushCopy, repeat, scan, transduce
|
|
621
|
+
} from "@thi.ng/transducers";
|
|
622
|
+
|
|
594
623
|
// this transducer uses 2 scans (a scan = inner reducer per item)
|
|
595
624
|
// 1) counts incoming values
|
|
596
625
|
// 2) forms an array of the current counter value `x` & repeated `x` times
|
|
@@ -618,6 +647,8 @@ transduce(comp(scan(count()), scan(pushCopy())), push(), [1,1,1,1])
|
|
|
618
647
|
### Weighted random choices
|
|
619
648
|
|
|
620
649
|
```ts
|
|
650
|
+
import { choices, frequencies, take, transduce } from "@thi.ng/transducers";
|
|
651
|
+
|
|
621
652
|
[...take(10, choices("abcd", [1, 0.5, 0.25, 0.125]))]
|
|
622
653
|
// [ 'a', 'a', 'b', 'a', 'a', 'b', 'a', 'c', 'd', 'b' ]
|
|
623
654
|
|
|
@@ -636,6 +667,8 @@ See
|
|
|
636
667
|
docs for details.
|
|
637
668
|
|
|
638
669
|
```ts
|
|
670
|
+
import { tween } from "@thi.ng/transducers";
|
|
671
|
+
|
|
639
672
|
[
|
|
640
673
|
...tween(
|
|
641
674
|
10,
|
|
@@ -707,6 +740,8 @@ of) transducers making use of their 1-arity completing function.
|
|
|
707
740
|
#### Reduced
|
|
708
741
|
|
|
709
742
|
```ts
|
|
743
|
+
import type { IDeref } from "@thi.ng/api";
|
|
744
|
+
|
|
710
745
|
class Reduced<T> implements IDeref<T> {
|
|
711
746
|
protected value: T;
|
|
712
747
|
constructor(val: T);
|
|
@@ -789,6 +824,8 @@ this interface can be directly passed to all functions in this package
|
|
|
789
824
|
where a `Transducer` arg is expected.
|
|
790
825
|
|
|
791
826
|
```ts
|
|
827
|
+
import { map, push, range, transduce, type IXform } from "@thi.ng/transducers";
|
|
828
|
+
|
|
792
829
|
class Mul implements IXform<number, number> {
|
|
793
830
|
|
|
794
831
|
constructor(public factor = 10) {}
|
|
@@ -801,6 +838,8 @@ class Mul implements IXform<number, number> {
|
|
|
801
838
|
transduce(new Mul(11), push(), range(4))
|
|
802
839
|
// [0, 11, 22, 33, 44]
|
|
803
840
|
|
|
841
|
+
import { comp, drop, push, range, takeNth, transduce } from "@thi.ng/transducers";
|
|
842
|
+
|
|
804
843
|
// also usable w/ comp(), iterator(), step(), run() etc.
|
|
805
844
|
transduce(
|
|
806
845
|
comp(drop(1), new Mul(11), takeNth(2)),
|
|
@@ -881,6 +920,8 @@ Similar to `run()`, consumes given iterable, presumably for any implicit
|
|
|
881
920
|
side-effects. Iterable MUST be finite!
|
|
882
921
|
|
|
883
922
|
```ts
|
|
923
|
+
import { consume, repeatedly2d } from "@thi.ng/transducers";
|
|
924
|
+
|
|
884
925
|
// here the function given to repeatedly2d() has only a side-effect, however
|
|
885
926
|
// repeatedly2d() itself is lazy. Using consume() then forces this lazy iterator/generator
|
|
886
927
|
// to be realized and so also the side-effects to be executed
|
|
@@ -900,6 +941,8 @@ With a few exceptions, most also accept an input iterable and then
|
|
|
900
941
|
directly yield a transforming iterator, e.g.
|
|
901
942
|
|
|
902
943
|
```ts
|
|
944
|
+
import { map, push, range, transduce } from "@thi.ng/transducers";
|
|
945
|
+
|
|
903
946
|
// as transducer
|
|
904
947
|
transduce(map((x) => x*10), push(), range(4))
|
|
905
948
|
// [ 0, 10, 20, 30 ]
|
package/flatten1.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ import type { Transducer } from "./api.js";
|
|
|
5
5
|
* 1st level of nesting in input. See {@link mapcat}.
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
|
-
* ```
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { flatten1 } from "@thi.ng/transducers";
|
|
10
|
+
*
|
|
9
11
|
* [...flatten1([[1], [2, 2], [3, 3, 3]])]
|
|
10
12
|
* // [ 1, 2, 2, 3, 3, 3 ]
|
|
11
13
|
*
|
package/mapcat.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/transducers",
|
|
3
|
-
"version": "8.9.
|
|
3
|
+
"version": "8.9.9",
|
|
4
4
|
"description": "Lightweight transducer implementations for ES6 / TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -39,14 +39,14 @@
|
|
|
39
39
|
"test": "bun test"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@thi.ng/api": "^8.9.
|
|
43
|
-
"@thi.ng/arrays": "^2.8.
|
|
44
|
-
"@thi.ng/checks": "^3.5.
|
|
45
|
-
"@thi.ng/compare": "^2.2.
|
|
46
|
-
"@thi.ng/compose": "^2.1.
|
|
47
|
-
"@thi.ng/errors": "^2.4.
|
|
48
|
-
"@thi.ng/math": "^5.10.
|
|
49
|
-
"@thi.ng/random": "^3.6.
|
|
42
|
+
"@thi.ng/api": "^8.9.27",
|
|
43
|
+
"@thi.ng/arrays": "^2.8.5",
|
|
44
|
+
"@thi.ng/checks": "^3.5.1",
|
|
45
|
+
"@thi.ng/compare": "^2.2.23",
|
|
46
|
+
"@thi.ng/compose": "^2.1.66",
|
|
47
|
+
"@thi.ng/errors": "^2.4.19",
|
|
48
|
+
"@thi.ng/math": "^5.10.4",
|
|
49
|
+
"@thi.ng/random": "^3.6.34"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@microsoft/api-extractor": "^7.40.1",
|
|
@@ -588,5 +588,5 @@
|
|
|
588
588
|
],
|
|
589
589
|
"year": 2016
|
|
590
590
|
},
|
|
591
|
-
"gitHead": "
|
|
591
|
+
"gitHead": "df9e312af741d87e6b450afcfea6a6e381662b1e\n"
|
|
592
592
|
}
|