@thi.ng/transducers
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -37,2 +37,5 @@ import { Comparator, IDeref, Predicate } from "@thi.ng/api"; | ||
export declare function delayed<T>(t: number): Transducer<T, Promise<T>>; | ||
export declare function bench(): Transducer<any, number>; | ||
export declare function sideEffect<T>(fn: (x: T) => void): Transducer<T, T>; | ||
export declare function inspect<T>(prefix?: string): Transducer<T, T>; | ||
export declare function distinct<T>(mapfn?: ((x: T) => any)): Transducer<T, T>; | ||
@@ -39,0 +42,0 @@ export declare function dedupe<T>(equiv?: (a: T, b: T) => boolean): Transducer<T, T>; |
23
index.js
@@ -257,2 +257,22 @@ "use strict"; | ||
exports.delayed = delayed; | ||
function bench() { | ||
return (rfn) => { | ||
const r = rfn[2]; | ||
let prev; | ||
return compR(rfn, (acc, _) => { | ||
let t = Date.now(), x = prev ? t - prev : 0; | ||
prev = t; | ||
r(acc, x); | ||
}); | ||
}; | ||
} | ||
exports.bench = bench; | ||
function sideEffect(fn) { | ||
return map((x) => (fn(x), x)); | ||
} | ||
exports.sideEffect = sideEffect; | ||
function inspect(prefix = "") { | ||
return sideEffect((x) => console.log(prefix, x)); | ||
} | ||
exports.inspect = inspect; | ||
function distinct(mapfn = identity) { | ||
@@ -271,6 +291,7 @@ return (rfn) => { | ||
exports.distinct = distinct; | ||
const SEMAPHORE = Symbol("SEMAPHORE"); | ||
function dedupe(equiv) { | ||
return (rfn) => { | ||
const r = rfn[2]; | ||
let prev = {}; | ||
let prev = SEMAPHORE; | ||
return compR(rfn, equiv ? | ||
@@ -277,0 +298,0 @@ (acc, x) => { |
{ | ||
"name": "@thi.ng/transducers", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Lightweight transducer implementations for ES6 / TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,7 +5,11 @@ # @thi.ng/transducers | ||
Lightweight transducer implementations for ES6 / TypeScript (7.6KB minified). | ||
Lightweight transducer implementations for ES6 / TypeScript (8KB minified, full lib). | ||
The library provides 28 transducers and 14 reducers for composing data | ||
The library provides 31 transducers and 14 reducers for composing data | ||
transformation pipelines (more to come). | ||
Please see the [@thi.ng/iterators](https://github.com/thi-ng/iterators) & | ||
[@thi.ng/csp](https://github.com/thi-ng/csp) partner modules for related | ||
functionality, supplementing features of this library. | ||
## Installation | ||
@@ -28,9 +32,12 @@ | ||
// collect as array (tx.push) | ||
tx.transduce(xform, tx.push, [1, 2, 3, 4, 5, 4, 3, 2, 1]); | ||
// [ 3, 9, 15 ] | ||
// re-use xform, but collect as set (tx.conj) | ||
tx.transduce(xform, tx.conj, [1, 2, 3, 4, 5, 4, 3, 2, 1]); | ||
// Set { 3, 9, 15 } | ||
for(let x of tx.iterator(xform, [1, 2, 3, 4])) { | ||
// apply as transforming iterator | ||
for(let x of tx.iterator(xform, [1, 2, 3, 4, 5])) { | ||
console.log(x); | ||
@@ -42,5 +49,29 @@ } | ||
// apply inspectors to debug transducer pipeline | ||
// alternatively, use tx.sideEffect() for any side fx | ||
tx.transduce( | ||
tx.comp( | ||
tx.inspect("orig"), | ||
tx.map(x => x + 1), | ||
tx.inspect("mapped"), | ||
tx.filter(x => (x & 1) > 0) | ||
), | ||
tx.push, | ||
[1, 2, 3, 4] | ||
); | ||
// orig 1 | ||
// mapped 2 | ||
// orig 2 | ||
// mapped 3 | ||
// orig 3 | ||
// mapped 4 | ||
// orig 4 | ||
// mapped 5 | ||
// [ 3, 5 ] | ||
tx.transduce(tx.map(x => x.toUpperCase()), tx.frequencies, "hello world") | ||
// Map { 'H' => 1, 'E' => 1, 'L' => 3, 'O' => 2, ' ' => 1, 'W' => 1, 'R' => 1, 'D' => 1 } | ||
// reduction only (no transform) | ||
tx.reduce(tx.frequencies, "hello world") | ||
@@ -125,3 +156,3 @@ // Map { 'h' => 1, 'e' => 1, 'l' => 3, 'o' => 2, ' ' => 1, 'w' => 1, 'r' => 1, 'd' => 1 } | ||
() => [], | ||
// completion | ||
// completion (nothing to do in this case) | ||
(acc) => acc, | ||
@@ -133,3 +164,4 @@ // step | ||
Currently only `partition` & `partitionBy` make use of the 1-arity completing function. | ||
Currently only `partition`, `partitionBy`, `streamSort`, `streamShuffle` make | ||
use of the 1-arity completing function. | ||
@@ -186,3 +218,3 @@ #### Reduced | ||
return (rfn: Reducer<any, T>) => { | ||
// stateful initialization | ||
// state initialization | ||
let prev = {}; | ||
@@ -238,2 +270,8 @@ return [ | ||
#### `bench(): Transducer<any, number>` | ||
#### `sideEffect<T>(fn: (x: T) => void): Transducer<T, T>` | ||
#### `inspect<T>(prefix?: string): Transducer<T, T>` | ||
#### `distinct<T>(mapfn?: (x: T) => any): Transducer<T, T>` | ||
@@ -240,0 +278,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30452
705
334