@thi.ng/transducers
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -13,3 +13,3 @@ import { IDeref, Predicate } from "@thi.ng/api"; | ||
} | ||
export declare function reduced<T>(x: T): any; | ||
export declare function reduced(x: any): any; | ||
export declare function isReduced(x: any): boolean; | ||
@@ -21,4 +21,7 @@ export declare function ensureReduced(x: any): Reduced<any>; | ||
export declare function identity<T>(x: T): T; | ||
export declare function reduce<A, B>([i, _, r]: Reducer<A, B>, acc: A, xs: Iterable<B>): any; | ||
export declare function transduce<A, B, C>(tx: Transducer<A, B>, rfn: Reducer<C, B>, acc: C, xs: Iterable<A>): any; | ||
export declare function step<A, B>(tx: Transducer<A, B>): (x: A) => B; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, xs: Iterable<B>): A; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, acc: A, xs: Iterable<B>): A; | ||
export declare function transduce<A, B, C>(tx: Transducer<A, B>, rfn: Reducer<C, B>, xs: Iterable<A>): C; | ||
export declare function transduce<A, B, C>(tx: Transducer<A, B>, rfn: Reducer<C, B>, acc: C, xs: Iterable<A>): C; | ||
export declare function iterator<A, B>(tx: Transducer<A, B>, xs: Iterable<A>): IterableIterator<B>; | ||
@@ -28,20 +31,21 @@ export declare function map<A, B>(fn: (x: A) => B): Transducer<A, B>; | ||
export declare function mapcat<A, B>(fn: (x: A) => Iterable<B>): Transducer<A, B>; | ||
export declare function filter<A>(pred: Predicate<A>): Transducer<A, A>; | ||
export declare function throttle<A>(delay: number): Transducer<A, A>; | ||
export declare function distinct<A>(mapfn?: (x: A) => any): Transducer<A, A>; | ||
export declare function dedupe<A>(equiv?: (a: A, b: A) => boolean): Transducer<A, A>; | ||
export declare function filter<T>(pred: Predicate<T>): Transducer<T, T>; | ||
export declare function throttle<T>(delay: number): Transducer<T, T>; | ||
export declare function delayed<T>(t: number): Transducer<T, Promise<T>>; | ||
export declare function distinct<T>(mapfn?: ((x: T) => any)): Transducer<T, T>; | ||
export declare function dedupe<T>(equiv?: (a: T, b: T) => boolean): Transducer<T, T>; | ||
export declare function interpose<A, B>(sep: B | (() => B)): Transducer<A, A | B>; | ||
export declare function interleave<A, B>(sep: B | (() => B)): Transducer<A, A | B>; | ||
export declare function take<A>(n: number): Transducer<A, A>; | ||
export declare function takeWhile<A>(pred: Predicate<A>): Transducer<A, A>; | ||
export declare function takeNth<A>(n: number): Transducer<A, A>; | ||
export declare function drop<A>(n: number): Transducer<A, A>; | ||
export declare function dropWhile<A>(pred: Predicate<A>): Transducer<A, A>; | ||
export declare function dropNth<A>(n: number): Transducer<A, A>; | ||
export declare function repeat<A>(n: number): Transducer<A, A>; | ||
export declare function sample<A>(prob: number): Transducer<A, A>; | ||
export declare function partition<A>(size: number, step?: number, all?: boolean): Transducer<A, A[]>; | ||
export declare function partitionBy<A>(fn: (x: A) => any): Transducer<A, A>; | ||
export declare function chunkSort<A>(n: number, key?: ((x) => any)): Transducer<A, A>; | ||
export declare function streamSort<T>(n: number, key: (x) => any): Transducer<T, T>; | ||
export declare function take<T>(n: number): Transducer<T, T>; | ||
export declare function takeWhile<T>(pred: Predicate<T>): Transducer<T, T>; | ||
export declare function takeNth<T>(n: number): Transducer<T, T>; | ||
export declare function drop<T>(n: number): Transducer<T, T>; | ||
export declare function dropWhile<T>(pred: Predicate<T>): Transducer<T, T>; | ||
export declare function dropNth<T>(n: number): Transducer<T, T>; | ||
export declare function repeat<T>(n: number): Transducer<T, T>; | ||
export declare function sample<T>(prob: number): Transducer<T, T>; | ||
export declare function partition<T>(size: number, step?: number, all?: boolean): Transducer<T, T[]>; | ||
export declare function partitionBy<T>(fn: (x: T) => any): Transducer<T, T[]>; | ||
export declare function chunkSort<T>(n: number, key?: ((x) => any)): Transducer<T, T>; | ||
export declare function streamSort<T>(n: number, key?: ((x) => any)): Transducer<T, T>; | ||
export declare const push: Reducer<any[], any>; | ||
@@ -53,1 +57,3 @@ export declare const conj: Reducer<Set<any>, any>; | ||
export declare const mul: Reducer<number, number>; | ||
export declare const frequencies: Reducer<Map<any, number>, any>; | ||
export declare const noop: Reducer<any, any>; |
54
index.js
@@ -75,3 +75,21 @@ "use strict"; | ||
exports.identity = identity; | ||
function reduce([i, _, r], acc, xs) { | ||
function step(tx) { | ||
const rfn = tx(exports.noop)[2]; | ||
return (x) => rfn(undefined, x); | ||
} | ||
exports.step = step; | ||
function reduce(...args) { | ||
let acc, xs; | ||
switch (args.length) { | ||
case 3: | ||
xs = args[2]; | ||
acc = args[1]; | ||
break; | ||
case 2: | ||
xs = args[1]; | ||
break; | ||
default: | ||
throw new Error(`illegal arity ${args.length}`); | ||
} | ||
const [i, _, r] = args[0]; | ||
acc = acc == null ? i() : acc; | ||
@@ -87,4 +105,16 @@ for (let x of xs) { | ||
exports.reduce = reduce; | ||
function transduce(tx, rfn, acc, xs) { | ||
const _rfn = tx(rfn); | ||
function transduce(...args) { | ||
let acc, xs; | ||
switch (args.length) { | ||
case 4: | ||
xs = args[3]; | ||
acc = args[2]; | ||
break; | ||
case 3: | ||
xs = args[2]; | ||
break; | ||
default: | ||
throw new Error(`illegal arity ${args.length}`); | ||
} | ||
const _rfn = args[0](args[1]); | ||
return unreduced(_rfn[1](reduce(_rfn, acc, xs))); | ||
@@ -163,3 +193,7 @@ } | ||
exports.throttle = throttle; | ||
function distinct(mapfn) { | ||
function delayed(t) { | ||
return map((x) => new Promise((resolve) => setTimeout(() => resolve(x), t))); | ||
} | ||
exports.delayed = delayed; | ||
function distinct(mapfn = identity) { | ||
return (rfn) => { | ||
@@ -389,3 +423,3 @@ const r = rfn[2]; | ||
} | ||
function streamSort(n, key) { | ||
function streamSort(n, key = identity) { | ||
return ([i, c, r]) => { | ||
@@ -443,1 +477,11 @@ const buf = []; | ||
]; | ||
exports.frequencies = [ | ||
() => new Map(), | ||
(acc) => acc, | ||
(acc, x) => acc.set(x, acc.has(x) ? acc.get(x) + 1 : 1) | ||
]; | ||
exports.noop = [ | ||
() => undefined, | ||
(acc) => acc, | ||
(_, x) => x, | ||
]; |
{ | ||
"name": "@thi.ng/transducers", | ||
"version": "0.3.0", | ||
"description": "Simplified transducer implementations", | ||
"version": "0.3.1", | ||
"description": "Lightweight transducer implementations for ES6 / TypeScript", | ||
"main": "index.js", | ||
@@ -11,3 +11,3 @@ "typings": "index.d.ts", | ||
"scripts": { | ||
"build": "rm -rf build && tsc --declaration && cp package.json build && cp *.md build && (cd build && node)", | ||
"build": "rm -rf build && tsc --declaration && cp package.json build && cp *.md build", | ||
"test": "tsc -p test && mocha build.test/test/*.js", | ||
@@ -14,0 +14,0 @@ "clean": "rm -rf build build.test doc node_modules && yarn install", |
140
README.md
# @thi.ng/transducers | ||
Lightweight transducer implementations for ES6. | ||
[![npm (scoped)](https://img.shields.io/npm/v/@thi.ng/transducers.svg)](https://www.npmjs.com/package/@thi.ng/transducers) | ||
Lightweight transducer implementations for ES6 / TypeScript (6KB minified). | ||
## Installation | ||
``` | ||
yarn add @thi.ng/api | ||
yarn run build | ||
yarn run test | ||
``` | ||
## Usage | ||
```js | ||
import * as tx from "@thi.ng/transducers"; | ||
xform = tx.comp( | ||
tx.filter(x => (x & 1) > 0), // odd numbers only | ||
tx.distinct(), // distinct numbers only | ||
tx.map(x=> x * 3) // times 3 | ||
); | ||
tx.transduce(xform, tx.push, [1, 2, 3, 4, 5, 4, 3, 2, 1]); | ||
// [ 3, 9, 15 ] | ||
tx.transduce(xform, tx.conj, [1, 2, 3, 4, 5, 4, 3, 2, 1]); | ||
// Set { 3, 9, 15 } | ||
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 } | ||
tx.reduce(tx.frequencies, "hello world") | ||
// Map { 'h' => 1, 'e' => 1, 'l' => 3, 'o' => 2, ' ' => 1, 'w' => 1, 'r' => 1, 'd' => 1 } | ||
for(let x of tx.iterator(xform, [1, 2, 3, 4])) { | ||
console.log(x); | ||
} | ||
f = tx.step(tx.dedupe()); | ||
f(1); // 1 | ||
f(2); // 2 | ||
f(2); // undefined | ||
f(3); // 3 | ||
f(3); // undefined | ||
f(3); // undefined | ||
f(1); // 1 | ||
``` | ||
### API | ||
TODO | ||
#### `reduce<A, B>(rfn: Reducer<A, B>, acc: A, xs: Iterable<B>): A` | ||
#### `transduce<A, B, C>(tx: Transducer<A, B>, rfn: Reducer<C, B>, acc: C, xs: Iterable<A>): C` | ||
#### `iterator<A, B>(tx: Transducer<A, B>, xs: Iterable<A>): IterableIterator<B>` | ||
#### `reduced(x: any): any` | ||
#### `isReduced(x: any): boolean` | ||
#### `ensureReduced(x: any): Reduced<any>` | ||
#### `unreduced(x: any): any` | ||
#### `comp(f1, f2, ...)` | ||
#### `compR(rfn: Reducer<any, any>, fn: (acc, x) => any): Reducer<any, any>` | ||
### Transducers | ||
#### `map<A, B>(fn: (x: A) => B): Transducer<A, B>` | ||
#### `mapIndexed<A, B>(fn: (i: number, x: A) => B): Transducer<A, B>` | ||
#### `mapcat<A, B>(fn: (x: A) => Iterable<B>): Transducer<A, B>` | ||
#### `filter<T>(pred: Predicate<T>): Transducer<T, T>` | ||
#### `throttle<T>(delay: number): Transducer<T, T>` | ||
#### `delayed<T>(t: number): Transducer<T, Promise<T>>` | ||
#### `distinct<T>(mapfn?: (x: T) => any): Transducer<T, T>` | ||
#### `dedupe<T>(equiv?: (a: T, b: T) => boolean): Transducer<T, T>` | ||
#### `interpose<A, B>(sep: B | (() => B)): Transducer<A, A | B>` | ||
#### `interleave<A, B>(sep: B | (() => B)): Transducer<A, A | B>` | ||
#### `take<T>(n: number): Transducer<T, T>` | ||
#### `takeWhile<T>(pred: Predicate<T>): Transducer<T, T>` | ||
#### `takeNth<T>(n: number): Transducer<T, T>` | ||
#### `drop<T>(n: number): Transducer<T, T>` | ||
#### `dropWhile<T>(pred: Predicate<T>): Transducer<T, T>` | ||
#### `dropNth<T>(n: number): Transducer<T, T>` | ||
#### `repeat<T>(n: number): Transducer<T, T>` | ||
#### `sample<T>(prob: number): Transducer<T, T>` | ||
#### `partition<T>(size: number, step?: number, all?: boolean): Transducer<T, T[]>` | ||
#### `partitionBy<T>(fn: (x: T) => any): Transducer<T, T[]>` | ||
#### `chunkSort<T>(n: number, key?: (x) => any): Transducer<T, T>` | ||
#### `streamSort<T>(n: number, key?: (x) => any): Transducer<T, T>` | ||
### Reducers | ||
#### `push: Reducer<any[], any>` | ||
#### `conj: Reducer<Set<any>, any>` | ||
#### `assocObj: Reducer<any, [PropertyKey, any]>` | ||
#### `assocMap: Reducer<Map<any, any>, [any, any]>` | ||
#### `add: Reducer<number, number>` | ||
#### `mul: Reducer<number, number>` | ||
#### `frequencies: Reducer<Map<any, number>, any` | ||
## Authors | ||
- Karsten Schmidt | ||
## License | ||
© 2016-2018 Karsten Schmidt // Apache Software License 2.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
19810
537
142