@thi.ng/transducers 8.9.8 → 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 +57 -3
- package/flatten1.d.ts +3 -1
- package/mapcat.d.ts +3 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -269,6 +269,8 @@ directory are using this package:
|
|
|
269
269
|
### Basic usage patterns
|
|
270
270
|
|
|
271
271
|
```ts
|
|
272
|
+
import { comp, distinct, filter, map } from "@thi.ng/transducers";
|
|
273
|
+
|
|
272
274
|
// compose transducer
|
|
273
275
|
xform = comp(
|
|
274
276
|
filter((x) => (x & 1) > 0), // odd numbers only
|
|
@@ -276,6 +278,8 @@ xform = comp(
|
|
|
276
278
|
map((x) => x * 3) // times 3
|
|
277
279
|
);
|
|
278
280
|
|
|
281
|
+
import { transduce, push } from "@thi.ng/transducers";
|
|
282
|
+
|
|
279
283
|
// collect into array (push)
|
|
280
284
|
transduce(xform, push(), [1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
|
281
285
|
// [ 3, 9, 15 ]
|
|
@@ -284,6 +288,8 @@ transduce(xform, push(), [1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
|
|
284
288
|
transduce(xform, conj(), [1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
|
285
289
|
// Set { 3, 9, 15 }
|
|
286
290
|
|
|
291
|
+
import { iterator } from "@thi.ng/transducers";
|
|
292
|
+
|
|
287
293
|
// or apply as transforming iterator
|
|
288
294
|
// no reduction, only transformations
|
|
289
295
|
[...iterator(xform, [1, 2, 3, 4, 5])]
|
|
@@ -294,6 +300,8 @@ transduce(xform, conj(), [1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
|
|
294
300
|
[...filter((x) => /[A-Z]/.test(x), "Hello World!")]
|
|
295
301
|
// ["H", "W"]
|
|
296
302
|
|
|
303
|
+
import { step } from "@thi.ng/transducers";
|
|
304
|
+
|
|
297
305
|
// single step execution
|
|
298
306
|
// returns undefined if transducer returned no result for this input
|
|
299
307
|
// returns array if transducer step produced multiple results
|
|
@@ -347,6 +355,8 @@ asSvg(
|
|
|
347
355
|
### Fuzzy search
|
|
348
356
|
|
|
349
357
|
```ts
|
|
358
|
+
import { filterFuzzy } from "@thi.ng/transducers";
|
|
359
|
+
|
|
350
360
|
[...filterFuzzy("ho", ["hello", "hallo", "hey", "heyoka"])]
|
|
351
361
|
// ["hello", "hallo", "heyoka"]
|
|
352
362
|
[...filterFuzzy("hlo", ["hello", "hallo", "hey", "heyoka"])]
|
|
@@ -369,6 +379,8 @@ asSvg(
|
|
|
369
379
|
### Histogram generation & result grouping
|
|
370
380
|
|
|
371
381
|
```ts
|
|
382
|
+
import { frequencies, map, reduce, transduce } from "@thi.ng/transducers";
|
|
383
|
+
|
|
372
384
|
// use the `frequencies` reducer to create
|
|
373
385
|
// a map counting occurrence of each value
|
|
374
386
|
transduce(map((x) => x.toUpperCase()), frequencies(), "hello world");
|
|
@@ -388,6 +400,10 @@ frequencies(
|
|
|
388
400
|
"my camel is collapsing and needs some water".split(" ")
|
|
389
401
|
);
|
|
390
402
|
// Map { 2 => 2, 5 => 3, 10 => 1, 3 => 1, 4 => 1 }
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
```ts
|
|
406
|
+
import { groupByMap } from "@thi.ng/transducers";
|
|
391
407
|
|
|
392
408
|
// actual grouping (here: by word length)
|
|
393
409
|
groupByMap(
|
|
@@ -406,6 +422,8 @@ groupByMap(
|
|
|
406
422
|
### Pagination
|
|
407
423
|
|
|
408
424
|
```ts
|
|
425
|
+
import { page, comp, iterator, map, padLast, range } from "@thi.ng/transducers";
|
|
426
|
+
|
|
409
427
|
// extract only items for given page id & page length
|
|
410
428
|
[...page(0, 5, range(12))]
|
|
411
429
|
// [ 0, 1, 2, 3, 4 ]
|
|
@@ -432,6 +450,8 @@ parallel using the provided transducers (which can be composed as usual)
|
|
|
432
450
|
and results in a tuple or keyed object.
|
|
433
451
|
|
|
434
452
|
```ts
|
|
453
|
+
import { map, multiplex, multiplexObj, push, transduce } from "@thi.ng/transducers";
|
|
454
|
+
|
|
435
455
|
transduce(
|
|
436
456
|
multiplex(
|
|
437
457
|
map((x) => x.charAt(0)),
|
|
@@ -460,6 +480,8 @@ transduce(
|
|
|
460
480
|
### Moving average using sliding window
|
|
461
481
|
|
|
462
482
|
```ts
|
|
483
|
+
import { comp, map, mean, partition, push, reduce transduce } from "@thi.ng/transducers";
|
|
484
|
+
|
|
463
485
|
// use nested reduce to compute window averages
|
|
464
486
|
transduce(
|
|
465
487
|
comp(
|
|
@@ -470,9 +492,13 @@ transduce(
|
|
|
470
492
|
[1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10]
|
|
471
493
|
)
|
|
472
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";
|
|
473
501
|
|
|
474
|
-
// this combined transducer is also directly
|
|
475
|
-
// available as: `movingAverage(n)`
|
|
476
502
|
[...movingAverage(5, [1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10])]
|
|
477
503
|
// [ 2.6, 3.4, 4, 4.6, 5.4, 6.2, 6.8, 7.6, 8.4 ]
|
|
478
504
|
```
|
|
@@ -480,6 +506,8 @@ transduce(
|
|
|
480
506
|
### Benchmark function execution time
|
|
481
507
|
|
|
482
508
|
```ts
|
|
509
|
+
import { benchmark, mean, repeatedly, transduce } from "@thi.ng/transducers";
|
|
510
|
+
|
|
483
511
|
// function to test
|
|
484
512
|
fn = () => {
|
|
485
513
|
let x;
|
|
@@ -497,7 +525,9 @@ transduce(benchmark(), mean(), repeatedly(fn, 100));
|
|
|
497
525
|
### Apply inspectors to debug transducer pipeline
|
|
498
526
|
|
|
499
527
|
```ts
|
|
500
|
-
|
|
528
|
+
import { comp, filter, map, push, trace, transduce } from "@thi.ng/transducers";
|
|
529
|
+
|
|
530
|
+
// alternatively, use sideEffect() for arbitrary side fx
|
|
501
531
|
transduce(
|
|
502
532
|
comp(
|
|
503
533
|
trace("orig"),
|
|
@@ -525,6 +555,8 @@ The `struct` transducer is simply a composition of: `partitionOf -> partition ->
|
|
|
525
555
|
here](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/struct.ts).
|
|
526
556
|
|
|
527
557
|
```ts
|
|
558
|
+
import { struct } from "@thi.ng/transducers";
|
|
559
|
+
|
|
528
560
|
// Higher-order transducer to convert linear input into structured objects
|
|
529
561
|
// using given field specs and ordering. A single field spec is an array of
|
|
530
562
|
// 2 or 3 items: `[name, size, transform?]`. If `transform` is given, it will
|
|
@@ -549,6 +581,8 @@ here](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers/src/s
|
|
|
549
581
|
### CSV parsing
|
|
550
582
|
|
|
551
583
|
```ts
|
|
584
|
+
import { comp, map, mapcat, push, rename, transduce } from "@thi.ng/transducers";
|
|
585
|
+
|
|
552
586
|
transduce(
|
|
553
587
|
comp(
|
|
554
588
|
// split into rows
|
|
@@ -569,6 +603,8 @@ transduce(
|
|
|
569
603
|
### Early termination
|
|
570
604
|
|
|
571
605
|
```ts
|
|
606
|
+
import { comp, flatten, push, take, transduce } from "@thi.ng/transducers";
|
|
607
|
+
|
|
572
608
|
// result is realized after max. 7 values, irrespective of nesting
|
|
573
609
|
transduce(comp(flatten(), take(7)), push(), [
|
|
574
610
|
1,
|
|
@@ -580,6 +616,10 @@ transduce(comp(flatten(), take(7)), push(), [
|
|
|
580
616
|
### Scan operator
|
|
581
617
|
|
|
582
618
|
```ts
|
|
619
|
+
import {
|
|
620
|
+
comp, count, iterator, map, push, pushCopy, repeat, scan, transduce
|
|
621
|
+
} from "@thi.ng/transducers";
|
|
622
|
+
|
|
583
623
|
// this transducer uses 2 scans (a scan = inner reducer per item)
|
|
584
624
|
// 1) counts incoming values
|
|
585
625
|
// 2) forms an array of the current counter value `x` & repeated `x` times
|
|
@@ -607,6 +647,8 @@ transduce(comp(scan(count()), scan(pushCopy())), push(), [1,1,1,1])
|
|
|
607
647
|
### Weighted random choices
|
|
608
648
|
|
|
609
649
|
```ts
|
|
650
|
+
import { choices, frequencies, take, transduce } from "@thi.ng/transducers";
|
|
651
|
+
|
|
610
652
|
[...take(10, choices("abcd", [1, 0.5, 0.25, 0.125]))]
|
|
611
653
|
// [ 'a', 'a', 'b', 'a', 'a', 'b', 'a', 'c', 'd', 'b' ]
|
|
612
654
|
|
|
@@ -625,6 +667,8 @@ See
|
|
|
625
667
|
docs for details.
|
|
626
668
|
|
|
627
669
|
```ts
|
|
670
|
+
import { tween } from "@thi.ng/transducers";
|
|
671
|
+
|
|
628
672
|
[
|
|
629
673
|
...tween(
|
|
630
674
|
10,
|
|
@@ -696,6 +740,8 @@ of) transducers making use of their 1-arity completing function.
|
|
|
696
740
|
#### Reduced
|
|
697
741
|
|
|
698
742
|
```ts
|
|
743
|
+
import type { IDeref } from "@thi.ng/api";
|
|
744
|
+
|
|
699
745
|
class Reduced<T> implements IDeref<T> {
|
|
700
746
|
protected value: T;
|
|
701
747
|
constructor(val: T);
|
|
@@ -778,6 +824,8 @@ this interface can be directly passed to all functions in this package
|
|
|
778
824
|
where a `Transducer` arg is expected.
|
|
779
825
|
|
|
780
826
|
```ts
|
|
827
|
+
import { map, push, range, transduce, type IXform } from "@thi.ng/transducers";
|
|
828
|
+
|
|
781
829
|
class Mul implements IXform<number, number> {
|
|
782
830
|
|
|
783
831
|
constructor(public factor = 10) {}
|
|
@@ -790,6 +838,8 @@ class Mul implements IXform<number, number> {
|
|
|
790
838
|
transduce(new Mul(11), push(), range(4))
|
|
791
839
|
// [0, 11, 22, 33, 44]
|
|
792
840
|
|
|
841
|
+
import { comp, drop, push, range, takeNth, transduce } from "@thi.ng/transducers";
|
|
842
|
+
|
|
793
843
|
// also usable w/ comp(), iterator(), step(), run() etc.
|
|
794
844
|
transduce(
|
|
795
845
|
comp(drop(1), new Mul(11), takeNth(2)),
|
|
@@ -870,6 +920,8 @@ Similar to `run()`, consumes given iterable, presumably for any implicit
|
|
|
870
920
|
side-effects. Iterable MUST be finite!
|
|
871
921
|
|
|
872
922
|
```ts
|
|
923
|
+
import { consume, repeatedly2d } from "@thi.ng/transducers";
|
|
924
|
+
|
|
873
925
|
// here the function given to repeatedly2d() has only a side-effect, however
|
|
874
926
|
// repeatedly2d() itself is lazy. Using consume() then forces this lazy iterator/generator
|
|
875
927
|
// to be realized and so also the side-effects to be executed
|
|
@@ -889,6 +941,8 @@ With a few exceptions, most also accept an input iterable and then
|
|
|
889
941
|
directly yield a transforming iterator, e.g.
|
|
890
942
|
|
|
891
943
|
```ts
|
|
944
|
+
import { map, push, range, transduce } from "@thi.ng/transducers";
|
|
945
|
+
|
|
892
946
|
// as transducer
|
|
893
947
|
transduce(map((x) => x*10), push(), range(4))
|
|
894
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",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@thi.ng/api": "^8.9.27",
|
|
43
|
-
"@thi.ng/arrays": "^2.8.
|
|
43
|
+
"@thi.ng/arrays": "^2.8.5",
|
|
44
44
|
"@thi.ng/checks": "^3.5.1",
|
|
45
45
|
"@thi.ng/compare": "^2.2.23",
|
|
46
46
|
"@thi.ng/compose": "^2.1.66",
|
|
@@ -588,5 +588,5 @@
|
|
|
588
588
|
],
|
|
589
589
|
"year": 2016
|
|
590
590
|
},
|
|
591
|
-
"gitHead": "
|
|
591
|
+
"gitHead": "df9e312af741d87e6b450afcfea6a6e381662b1e\n"
|
|
592
592
|
}
|