@thi.ng/transducers
Advanced tools
Comparing version 9.0.5 to 9.0.6
@@ -6,4 +6,4 @@ import type { Reducer } from "./api.js"; | ||
export declare function add(init?: number): Reducer<number, number>; | ||
export declare function add(xs: Iterable<number>): number; | ||
export declare function add(init: number, xs: Iterable<number>): number; | ||
export declare function add(src: Iterable<number>): number; | ||
export declare function add(init: number, src: Iterable<number>): number; | ||
//# sourceMappingURL=add.d.ts.map |
@@ -1,4 +0,4 @@ | ||
import { __mathop } from "./internal/mathop.js"; | ||
import { __mathOp } from "./internal/mathop.js"; | ||
function add(...args) { | ||
return __mathop(add, (acc, x) => acc + x, 0, args); | ||
return __mathOp(add, (acc, x) => acc + x, 0, args); | ||
} | ||
@@ -5,0 +5,0 @@ export { |
17
api.d.ts
@@ -33,2 +33,3 @@ import type { Comparator, Fn, Fn0, IObjectOf } from "@thi.ng/api"; | ||
export type Reducer<A, B> = [Fn0<B>, Fn<B, B>, ReductionFn<A, B>]; | ||
export type MaybeReduced<T> = Reduced<T> | T; | ||
/** | ||
@@ -41,5 +42,5 @@ * Interface for types able to provide some internal functionality (or | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/ixform.ts | ||
* import { | ||
* comp, drop, map, push, range, transduce | ||
* comp, drop, map, push, range, takeNth, transduce, | ||
* type IXform | ||
@@ -51,10 +52,12 @@ * } from "@thi.ng/transducers"; | ||
* | ||
* xform() { return map((x) => this.factor * x); } | ||
* xform() { return map((x: number) => this.factor * x); } | ||
* } | ||
* | ||
* transduce(new Mul(11), push(), range(4)) | ||
* console.log( | ||
* transduce(new Mul(11), push(), range(4)) | ||
* ); | ||
* // [0, 11, 22, 33, 44] | ||
* | ||
* // also usable w/ comp() | ||
* transduce( | ||
* const res = transduce( | ||
* comp( | ||
@@ -67,3 +70,5 @@ * drop(1), | ||
* range(4) | ||
* ) | ||
* ); | ||
* | ||
* console.log(res); | ||
* // [11, 33] | ||
@@ -70,0 +75,0 @@ * ``` |
@@ -5,6 +5,6 @@ import type { Pair } from "@thi.ng/api"; | ||
* Reducer accepting key-value pairs / tuples and transforming / adding | ||
* them to an ES6 Map. | ||
* them to an ES6 Map. Also see {@link assocObj}. | ||
*/ | ||
export declare function assocMap<A, B>(): Reducer<Pair<A, B>, Map<A, B>>; | ||
export declare function assocMap<A, B>(xs: Iterable<Pair<A, B>>): Map<A, B>; | ||
export declare function assocMap<A, B>(src: Iterable<Pair<A, B>>): Map<A, B>; | ||
//# sourceMappingURL=assoc-map.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function assocMap(xs) { | ||
return xs ? reduce(assocMap(), xs) : reducer( | ||
function assocMap(src) { | ||
return src ? reduce(assocMap(), src) : reducer( | ||
() => /* @__PURE__ */ new Map(), | ||
@@ -5,0 +5,0 @@ (acc, [k, v]) => acc.set(k, v) |
@@ -5,6 +5,6 @@ import type { IObjectOf, Pair } from "@thi.ng/api"; | ||
* Reducer accepting key-value pairs / tuples and updating / adding them | ||
* to an object. | ||
* to an object. Also see {@link assocMap}. | ||
*/ | ||
export declare function assocObj<T>(): Reducer<Pair<PropertyKey, T>, IObjectOf<T>>; | ||
export declare function assocObj<T>(xs: Iterable<Pair<PropertyKey, T>>): IObjectOf<T>; | ||
export declare function assocObj<T>(src: Iterable<Pair<PropertyKey, T>>): IObjectOf<T>; | ||
//# sourceMappingURL=assoc-obj.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function assocObj(xs) { | ||
return xs ? reduce(assocObj(), xs) : reducer( | ||
function assocObj(src) { | ||
return src ? reduce(assocObj(), src) : reducer( | ||
() => ({}), | ||
@@ -5,0 +5,0 @@ (acc, [k, v]) => (acc[k] = v, acc) |
@@ -8,6 +8,8 @@ import type { IObjectOf } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/auto-obj.ts | ||
* import { autoObj } from "@thi.ng/transducers"; | ||
* | ||
* autoObj("id", ["foo", "bar", "baz"]) | ||
* console.log( | ||
* autoObj("id", ["foo", "bar", "baz"]) | ||
* ); | ||
* // { id0: "foo", id1: "bar", id2: "baz" } | ||
@@ -19,3 +21,3 @@ * ``` | ||
export declare function autoObj<T>(prefix: string): Reducer<T, IObjectOf<T>>; | ||
export declare function autoObj<T>(prefix: string, xs: Iterable<T>): IObjectOf<T>; | ||
export declare function autoObj<T>(prefix: string, src: Iterable<T>): IObjectOf<T>; | ||
//# sourceMappingURL=auto-obj.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function autoObj(prefix, xs) { | ||
function autoObj(prefix, src) { | ||
let id = 0; | ||
return xs ? reduce(autoObj(prefix), xs) : reducer( | ||
return src ? reduce(autoObj(prefix), src) : reducer( | ||
() => ({}), | ||
@@ -6,0 +6,0 @@ (acc, v) => (acc[prefix + id++] = v, acc) |
@@ -8,9 +8,15 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/benchmark.ts | ||
* import { fromInterval, trace } from "@thi.ng/rstream"; | ||
* import { benchmark, comp, movingAverage } from "@thi.ng/transducers"; | ||
* | ||
* fromInterval(1000).subscribe( | ||
* fromInterval(16).subscribe( | ||
* trace(), | ||
* { xform: comp(benchmark(), movingAverage(60)) } | ||
* ) | ||
* ); | ||
* // 16.766666666666666 | ||
* // 17.05 | ||
* // 17.033333333333335 | ||
* // 17.033333333333335 | ||
* // ... | ||
* ``` | ||
@@ -17,0 +23,0 @@ */ |
32
cat.d.ts
import type { Nullable } from "@thi.ng/api"; | ||
import type { Transducer } from "./api.js"; | ||
import type { MaybeReduced, Transducer } from "./api.js"; | ||
/** | ||
@@ -18,3 +18,3 @@ * Transducer to concatenate iterable values. Iterates over each input and emits | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/cat.ts | ||
* import { | ||
@@ -24,16 +24,22 @@ * cat, comp, iterator, map, mapcat, mapIndexed, reduced | ||
* | ||
* [...iterator(comp(map((x) => [x, x]), cat()), [1, 2, 3, 4])] | ||
* console.log( | ||
* [...iterator(comp(map((x) => [x, x]), cat()), [1, 2, 3, 4])] | ||
* ); | ||
* // [ 1, 1, 2, 2, 3, 3, 4, 4 ] | ||
* | ||
* [...iterator( | ||
* comp( | ||
* mapIndexed((i, x) => [[i], [x, x]]), | ||
* cat(), | ||
* cat() | ||
* ), | ||
* "abc" | ||
* )] | ||
* console.log( | ||
* [...iterator( | ||
* comp( | ||
* mapIndexed((i, x) => [[i], [x, x]]), | ||
* cat<(number | string)[]>(), | ||
* cat() | ||
* ), | ||
* "abc" | ||
* )] | ||
* ); | ||
* // [ 0, 'a', 'a', 1, 'b', 'b', 2, 'c', 'c' ] | ||
* | ||
* [...mapcat((x)=>(x > 1 ? reduced([x, x]) : [x, x]), [1, 2, 3, 4])] | ||
* console.log( | ||
* [...mapcat((x)=>(x > 1 ? reduced([x, x]) : [x, x]), [1, 2, 3, 4])] | ||
* ); | ||
* // [ 1, 1, 2, 2 ] | ||
@@ -44,3 +50,3 @@ * ``` | ||
*/ | ||
export declare const cat: <T>() => Transducer<Nullable<Iterable<T>>, T>; | ||
export declare const cat: <T>() => Transducer<MaybeReduced<Nullable<Iterable<T>>>, T>; | ||
//# sourceMappingURL=cat.d.ts.map |
@@ -7,3 +7,3 @@ import { compR } from "./compr.js"; | ||
if (x) { | ||
for (let y of unreduced(x)) { | ||
for (let y of unreduced(x) || []) { | ||
acc = r(acc, y); | ||
@@ -10,0 +10,0 @@ if (isReduced(acc)) { |
# Change Log | ||
- **Last updated**: 2024-05-08T18:24:31Z | ||
- **Last updated**: 2024-06-21T19:34:38Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -12,2 +12,10 @@ | ||
### [9.0.6](https://github.com/thi-ng/umbrella/tree/@thi.ng/transducers@9.0.6) (2024-06-21) | ||
#### ♻️ Refactoring | ||
- rename various rest args to be more semantically meaningful ([8088a56](https://github.com/thi-ng/umbrella/commit/8088a56)) | ||
- enforce uniform naming convention of internal functions ([56992b2](https://github.com/thi-ng/umbrella/commit/56992b2)) | ||
- dedupe min/maxCompare() impls, add tests ([22fc6e3](https://github.com/thi-ng/umbrella/commit/22fc6e3)) | ||
### [9.0.2](https://github.com/thi-ng/umbrella/tree/@thi.ng/transducers@9.0.2) (2024-04-20) | ||
@@ -14,0 +22,0 @@ |
@@ -12,7 +12,18 @@ import type { IRandom } from "@thi.ng/random"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/choices.ts | ||
* import { choices, frequencies, take, transduce } from "@thi.ng/transducers"; | ||
* | ||
* transduce(take(1000), frequencies(), choices("abcd", [1, 0.5, 0.25, 0.125])) | ||
* // Map { 'c' => 132, 'a' => 545, 'b' => 251, 'd' => 72 } | ||
* const res = transduce( | ||
* take(1000), | ||
* frequencies(), | ||
* choices("abcd", [1, 0.5, 0.25, 0.125]) | ||
* ); | ||
* | ||
* console.log(res); | ||
* // Map(4) { | ||
* // "a": 544, | ||
* // "b": 263, | ||
* // "c": 131, | ||
* // "d": 62, | ||
* // } | ||
* ``` | ||
@@ -19,0 +30,0 @@ * |
@@ -8,15 +8,19 @@ import type { Nullable } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/concat.ts | ||
* import { concat } from "@thi.ng/transducers"; | ||
* | ||
* [...concat([1, 2, 3], null, [4, 5])] | ||
* console.log( | ||
* [...concat([1, 2, 3], null, [4, 5])] | ||
* ); | ||
* // [ 1, 2, 3, 4, 5 ] | ||
* | ||
* [...concat([1, 2, 3, undefined], null, [4, 5])] | ||
* console.log( | ||
* [...concat([1, 2, 3, undefined], null, [4, 5])] | ||
* ); | ||
* // [ 1, 2, 3, undefined, 4, 5 ] | ||
* ``` | ||
* | ||
* @param xs - | ||
* @param sources - | ||
*/ | ||
export declare function concat<T>(...xs: Nullable<Iterable<T>>[]): IterableIterator<T>; | ||
export declare function concat<T>(...sources: Nullable<Iterable<T>>[]): IterableIterator<T>; | ||
//# sourceMappingURL=concat.d.ts.map |
import { ensureIterable } from "@thi.ng/arrays/ensure-iterable"; | ||
function* concat(...xs) { | ||
for (let x of xs) { | ||
x != null && (yield* ensureIterable(x)); | ||
function* concat(...sources) { | ||
for (let src of sources) { | ||
src != null && (yield* ensureIterable(src)); | ||
} | ||
@@ -6,0 +6,0 @@ } |
@@ -6,3 +6,3 @@ import type { Reducer } from "./api.js"; | ||
export declare function conj<T>(): Reducer<T, Set<T>>; | ||
export declare function conj<T>(xs: Iterable<T>): Set<T>; | ||
export declare function conj<T>(src: Iterable<T>): Set<T>; | ||
//# sourceMappingURL=conj.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function conj(xs) { | ||
return xs ? reduce(conj(), xs) : reducer( | ||
function conj(src) { | ||
return src ? reduce(conj(), src) : reducer( | ||
() => /* @__PURE__ */ new Set(), | ||
@@ -5,0 +5,0 @@ (acc, x) => acc.add(x) |
@@ -10,6 +10,10 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/consume.ts | ||
* import { consume, repeatedly2d } from "@thi.ng/transducers"; | ||
* | ||
* consume(repeatedly2d((x, y) => console.log("output:", [x, y]), 2, 3)); | ||
* // iterators are lazy, no logging will actually be performed yet | ||
* const iter = repeatedly2d((x, y) => console.log("output:", [x, y]), 2, 3); | ||
* | ||
* // force evaluation, discard any results | ||
* consume(iter); | ||
* // output: [ 0, 0 ] | ||
@@ -16,0 +20,0 @@ * // output: [ 1, 0 ] |
@@ -18,7 +18,7 @@ import type { Predicate2 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/converge.ts | ||
* import { converge, iterate } from "@thi.ng/transducers"; | ||
* | ||
* // process as long as difference to prev value is >= 0.01 | ||
* [...converge( | ||
* const res = [...converge( | ||
* // predicate | ||
@@ -28,3 +28,5 @@ * (a, b) => Math.abs(a - b) < 0.01, | ||
* iterate((x, i) => x + Math.pow(2, -i), 0) | ||
* )] | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ 0, 0.5, 0.75, 0.875, 0.9375, 0.96875, 0.984375, 0.9921875 ] | ||
@@ -31,0 +33,0 @@ * ``` |
@@ -18,3 +18,3 @@ import { illegalArgs } from "@thi.ng/errors/illegal-arguments"; | ||
}; | ||
const kernelLookup1d = (src, x, width, wrap, border) => wrap ? ({ 0: w, 1: ox }) => { | ||
const __kernelLookup1d = (src, x, width, wrap, border) => wrap ? ({ 0: w, 1: ox }) => { | ||
const xx = x < -ox ? width + ox : x >= width - ox ? ox - 1 : x + ox; | ||
@@ -25,3 +25,3 @@ return w * src[xx]; | ||
}; | ||
const kernelLookup2d = (src, x, y, width, height, wrap, border) => wrap ? ({ 0: w, 1: { 0: ox, 1: oy } }) => { | ||
const __kernelLookup2d = (src, x, y, width, height, wrap, border) => wrap ? ({ 0: w, 1: { 0: ox, 1: oy } }) => { | ||
const xx = x < -ox ? width + ox : x >= width - ox ? ox - 1 : x + ox; | ||
@@ -33,3 +33,3 @@ const yy = y < -oy ? height + oy : y >= height - oy ? oy - 1 : y + oy; | ||
}; | ||
const kernelError = () => illegalArgs(`no kernel or kernel config`); | ||
const __kernelError = () => illegalArgs(`no kernel or kernel config`); | ||
function convolve1d(opts, indices) { | ||
@@ -45,3 +45,3 @@ if (indices) { | ||
if (!kernel) { | ||
!(opts.weights && opts.kwidth) && kernelError(); | ||
!(opts.weights && opts.kwidth) && __kernelError(); | ||
kernel = buildKernel1d(opts.weights, opts.kwidth); | ||
@@ -51,3 +51,3 @@ } | ||
(p) => transduce( | ||
map(kernelLookup1d(src, p, width, wrap, border)), | ||
map(__kernelLookup1d(src, p, width, wrap, border)), | ||
rfn(), | ||
@@ -68,3 +68,3 @@ kernel | ||
if (!kernel) { | ||
!(opts.weights && opts.kwidth && opts.kheight) && kernelError(); | ||
!(opts.weights && opts.kwidth && opts.kheight) && __kernelError(); | ||
kernel = buildKernel2d(opts.weights, opts.kwidth, opts.kheight); | ||
@@ -74,3 +74,3 @@ } | ||
(p) => transduce( | ||
map(kernelLookup2d(src, p[0], p[1], width, height, wrap, border)), | ||
map(__kernelLookup2d(src, p[0], p[1], width, height, wrap, border)), | ||
rfn(), | ||
@@ -77,0 +77,0 @@ kernel |
@@ -10,5 +10,5 @@ import type { Reducer } from "./api.js"; | ||
export declare function count(offset?: number, step?: number): Reducer<any, number>; | ||
export declare function count(xs: Iterable<any>): number; | ||
export declare function count(offset: number, xs: Iterable<any>): number; | ||
export declare function count(offset: number, step: number, xs: Iterable<any>): number; | ||
export declare function count(src: Iterable<any>): number; | ||
export declare function count(offset: number, src: Iterable<any>): number; | ||
export declare function count(offset: number, step: number, src: Iterable<any>): number; | ||
//# sourceMappingURL=count.d.ts.map |
@@ -17,8 +17,10 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/curve.ts | ||
* import { curve } from "@thi.ng/transducers"; | ||
* | ||
* [...curve(50, 100, 10, 2)] | ||
* console.log( | ||
* [...curve(50, 100, 10)] | ||
* ); | ||
* // [ | ||
* // 50, | ||
* // 50.000, | ||
* // 73.193, | ||
@@ -33,3 +35,3 @@ * // 85.649, | ||
* // 99.913, | ||
* // 100 | ||
* // 100.000 | ||
* // ] | ||
@@ -36,0 +38,0 @@ * ``` |
@@ -10,11 +10,15 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/cycle.ts | ||
* import { cycle, range, take } from "@thi.ng/transducers"; | ||
* | ||
* // take 5 from infinite sequence | ||
* [...take(5, cycle([1, 2, 3]))] | ||
* console.log( | ||
* [...take(5, cycle([1, 2, 3]))] | ||
* ); | ||
* // [1, 2, 3, 1, 2] | ||
* | ||
* // only produce 2 cycles | ||
* [...cycle(range(3), 2)] | ||
* console.log( | ||
* [...cycle(range(3), 2)] | ||
* ); | ||
* // [ 0, 1, 2, 0, 1, 2 ] | ||
@@ -21,0 +25,0 @@ * ``` |
@@ -11,6 +11,8 @@ import type { Predicate2 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/dedupe.ts | ||
* import { dedupe } from "@thi.ng/transducers"; | ||
* | ||
* [...dedupe([1, 1, 2, 3, 3, 3, 1])] | ||
* console.log( | ||
* [...dedupe([1, 1, 2, 3, 3, 3, 1])] | ||
* ); | ||
* // [ 1, 2, 3, 1 ] | ||
@@ -17,0 +19,0 @@ * ``` |
@@ -21,7 +21,7 @@ import type { TransformSpec } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* import { deepTransform } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/deep-transform.ts | ||
* import { deepTransform, type TransformSpec } from "@thi.ng/transducers"; | ||
* | ||
* // source object to be transformed | ||
* src = { | ||
* const src = { | ||
* meta: { | ||
@@ -37,3 +37,3 @@ * author: { name: "Alice", email: "a@b.com" }, | ||
* // deep transformation spec | ||
* spec = [ | ||
* const spec: TransformSpec = [ | ||
* // root transform (called last) | ||
@@ -55,10 +55,11 @@ * ({ type, meta, title, body }) => ["div", { class: type }, title, meta, body], | ||
* // build transformer & apply to src | ||
* deepTransform(spec)(src); | ||
* | ||
* console.log( | ||
* deepTransform(spec)(src) | ||
* ); | ||
* // [ "div", | ||
* // { class: "article" }, | ||
* // { class: "post" }, | ||
* // [ "h1", "Hello world" ], | ||
* // [ "div.meta", | ||
* // [ "a", { href: "mailto:a@.b.com" }, "Alice" ], | ||
* // "./2/2003, 12:34:56 PM)" ], | ||
* // "(1/2/2003, 12:34:56 PM)" ], | ||
* // "Ratione necessitatibus doloremque itaque." ] | ||
@@ -65,0 +66,0 @@ * ``` |
@@ -20,6 +20,8 @@ import type { Fn, Fn0 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/distinct.ts | ||
* import { distinct } from "@thi.ng/transducers"; | ||
* | ||
* [...distinct({ key: (x) => x.id }, [{id: 1, x: 2}, {id: 1, x: 3}])] | ||
* console.log( | ||
* [...distinct({ key: (x) => x.id }, [{id: 1, x: 2}, {id: 1, x: 3}])] | ||
* ); | ||
* // [ { id: 1, x: 2 } ] | ||
@@ -26,0 +28,0 @@ * ``` |
@@ -7,3 +7,3 @@ import type { Reducer } from "./api.js"; | ||
export declare function div(init: number): Reducer<number, number>; | ||
export declare function div(init: number, xs: Iterable<number>): number; | ||
export declare function div(init: number, src: Iterable<number>): number; | ||
//# sourceMappingURL=div.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function div(init, xs) { | ||
return xs ? reduce(div(init), xs) : reducer( | ||
function div(init, src) { | ||
return src ? reduce(div(init), src) : reducer( | ||
() => init, | ||
@@ -5,0 +5,0 @@ (acc, x) => acc / x |
15
dup.d.ts
@@ -10,8 +10,13 @@ /** | ||
* @example | ||
* ```ts | ||
* import { dup } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/dup.ts | ||
* import { dup, range } from "@thi.ng/transducers"; | ||
* | ||
* dup("hello"); // "hellohello" | ||
* dup([1, 2, 3]); // [1, 2, 3, 1, 2, 3] | ||
* dup(range(3)); // IterableIterator<number> | ||
* console.log(dup("hello")); | ||
* // "hellohello" | ||
* | ||
* console.log(dup([1, 2, 3])); | ||
* // [ 1, 2, 3, 1, 2, 3 ] | ||
* | ||
* console.log([...dup(range(3))]); | ||
* // [ 0, 1, 2, 0, 1, 2 ] | ||
* ``` | ||
@@ -18,0 +23,0 @@ * |
@@ -12,6 +12,8 @@ import type { Predicate } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/every.ts | ||
* import { every } from "@thi.ng/transducers"; | ||
* | ||
* every((x)=> x > 0, [1,2,-1,3]); | ||
* console.log( | ||
* every((x)=> x > 0, [1,2,-1,3]) | ||
* ); | ||
* // false | ||
@@ -23,4 +25,4 @@ * ``` | ||
export declare function every<T>(pred?: Predicate<T>): Reducer<boolean, T>; | ||
export declare function every<T>(xs: Iterable<T>): boolean; | ||
export declare function every<T>(pred: Predicate<T>, xs: Iterable<T>): boolean; | ||
export declare function every<T>(src: Iterable<T>): boolean; | ||
export declare function every<T>(pred: Predicate<T>, src: Iterable<T>): boolean; | ||
//# sourceMappingURL=every.d.ts.map |
@@ -5,5 +5,3 @@ import { $$reduce, reducer } from "./reduce.js"; | ||
const res = $$reduce(every, args); | ||
if (res !== void 0) { | ||
return res; | ||
} | ||
if (res !== void 0) return res; | ||
const pred = args[0]; | ||
@@ -10,0 +8,0 @@ return reducer( |
@@ -10,13 +10,21 @@ /** | ||
* | ||
* Also see {@link padSides}, {@link wrapSides}. | ||
* | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/extend-sides.ts | ||
* import { extendSides } from "@thi.ng/transducers"; | ||
* | ||
* [...extendSides([1, 2, 3])] | ||
* console.log( | ||
* [...extendSides([1, 2, 3])] | ||
* ); | ||
* // [ 1, 1, 2, 3, 3] | ||
* | ||
* [...extendSides([1, 2, 3], 3)] | ||
* console.log( | ||
* [...extendSides([1, 2, 3], 3)] | ||
* ); | ||
* // [ 1, 1, 1, 1, 2, 3, 3, 3, 3 ] | ||
* | ||
* [...extendSides([1, 2, 3], 0, 3)] | ||
* console.log( | ||
* [...extendSides([1, 2, 3], 0, 3)] | ||
* ); | ||
* // [ 1, 2, 3, 3, 3, 3 ] | ||
@@ -23,0 +31,0 @@ * ``` |
@@ -11,4 +11,4 @@ import type { NumericArray } from "@thi.ng/api"; | ||
export declare function fill<T>(start?: number): Reducer<T, T[]>; | ||
export declare function fill<T>(xs: Iterable<T>): T[]; | ||
export declare function fill<T>(start: number, xs: Iterable<T>): T[]; | ||
export declare function fill<T>(src: Iterable<T>): T[]; | ||
export declare function fill<T>(start: number, src: Iterable<T>): T[]; | ||
/** | ||
@@ -20,4 +20,4 @@ * Like {@link fill} reducer, but for numeric arrays (incl. typed arrays). | ||
export declare function fillN(start?: number): Reducer<number, NumericArray>; | ||
export declare function fillN(xs: Iterable<number>): NumericArray; | ||
export declare function fillN(start: number, xs: Iterable<number>): NumericArray; | ||
export declare function fillN(src: Iterable<number>): NumericArray; | ||
export declare function fillN(start: number, src: Iterable<number>): NumericArray; | ||
//# sourceMappingURL=fill.d.ts.map |
import { $$reduce, reducer } from "./reduce.js"; | ||
function fill(...args) { | ||
const res = $$reduce(fill, args); | ||
if (res !== void 0) { | ||
return res; | ||
} | ||
if (res !== void 0) return res; | ||
let start = args[0] || 0; | ||
@@ -8,0 +6,0 @@ return reducer( |
@@ -26,6 +26,8 @@ import type { Fn, Predicate2 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/filter-fuzzy.ts | ||
* import { filterFuzzy } from "@thi.ng/transducers"; | ||
* | ||
* [...filterFuzzy("ho", ["hello", "hallo", "hey", "heyoka"])] | ||
* console.log( | ||
* [...filterFuzzy("ho", ["hello", "hallo", "hey", "heyoka"])] | ||
* ); | ||
* // ["hello", "hallo", "heyoka"] | ||
@@ -32,0 +34,0 @@ * ``` |
@@ -16,4 +16,5 @@ import type { DeepArrayValue, Fn, Nullable } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* import { flattenWith } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/flatten-with.ts | ||
* import { isPlainObject, isNotStringAndIterable } from "@thi.ng/checks"; | ||
* import { flattenWith, pairs } from "@thi.ng/transducers"; | ||
* | ||
@@ -29,3 +30,5 @@ * // custom predicate which converts objects into key/val tuples, | ||
* | ||
* [...flattenWith(pred, [{ a: 1, b: 2 }, [[{ c: 3 }]]])] | ||
* console.log( | ||
* [...flattenWith(pred, [{ a: 1, b: 2 }, [[{ c: 3 }]]])] | ||
* ); | ||
* // [ 'a', 1, 'b', 2, 'c', 3 ] | ||
@@ -32,0 +35,0 @@ * ``` |
@@ -12,9 +12,13 @@ import type { DeepArrayValue } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/flatten.ts | ||
* import { flatten } from "@thi.ng/transducers"; | ||
* | ||
* [...flatten([[1, [2, 3]], ["abc", "cde"]])] | ||
* console.log( | ||
* [...flatten([[1, [2, 3]], ["abc", "cde"]])] | ||
* ); | ||
* // [1, 2, 3, "abc", "def"] | ||
* | ||
* [...flatten("abc")] | ||
* console.log( | ||
* [...flatten("abc")] | ||
* ); | ||
* // [ 'abc' ] | ||
@@ -21,0 +25,0 @@ * ``` |
@@ -8,14 +8,20 @@ import type { Nullable } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* import { flatten1 } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/flatten1.ts | ||
* import { flatten1, mapcat } from "@thi.ng/transducers"; | ||
* | ||
* [...flatten1([[1], [2, 2], [3, 3, 3]])] | ||
* console.log( | ||
* [...flatten1([[1], [2, 2], [3, 3, 3]])] | ||
* ); | ||
* // [ 1, 2, 2, 3, 3, 3 ] | ||
* | ||
* // same as: | ||
* [...mapcat((x) => x, [[1], [2, 2], [3, 3, 3]])] | ||
* console.log( | ||
* [...mapcat((x) => x, [[1], [2, 2], [3, 3, 3]])] | ||
* ); | ||
* // [ 1, 2, 2, 3, 3, 3 ] | ||
* | ||
* // nullish inputs will be removed | ||
* [...flatten1([[1], null, [3, 3, 3]])] | ||
* console.log( | ||
* [...flatten1([[1], null, [3, 3, 3]])] | ||
* ); | ||
* // [1, 3, 3, 3] | ||
@@ -22,0 +28,0 @@ * ``` |
import type { Fn } from "@thi.ng/api"; | ||
import type { Reducer } from "./api.js"; | ||
export declare function frequencies<A>(): Reducer<A, Map<A, number>>; | ||
export declare function frequencies<A>(xs: Iterable<A>): Map<A, number>; | ||
export declare function frequencies<A>(src: Iterable<A>): Map<A, number>; | ||
export declare function frequencies<A, B>(key: Fn<A, B>): Reducer<A, Map<B, number>>; | ||
export declare function frequencies<A, B>(key: Fn<A, B>, xs: Iterable<A>): Map<B, number>; | ||
export declare function frequencies<A, B>(key: Fn<A, B>, src: Iterable<A>): Map<B, number>; | ||
//# sourceMappingURL=frequencies.d.ts.map |
@@ -20,15 +20,17 @@ import type { Fn, Fn0, IObjectOf } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/group-binary.ts | ||
* import { groupBinary, reduce } from "@thi.ng/transducers"; | ||
* | ||
* tree = reduce( | ||
* groupBinary(4, x => x.id & 0xf), | ||
* [{id: 3}, {id: 8}, {id: 15}, {id: 0}] | ||
* ) | ||
* const tree = reduce( | ||
* groupBinary<{ id: number }>(4, x => x.id & 0xf), | ||
* [{ id: 3 }, { id: 8 }, { id: 15 }, { id: 0 }] | ||
* ); | ||
* | ||
* tree.l.l.l.l | ||
* console.log(tree.l.l.l.l); | ||
* // [ { id: 0 } ] | ||
* tree.r.r.r.r | ||
* | ||
* console.log(tree.r.r.r.r); | ||
* // [ { id: 15 } ] | ||
* tree.l.l.r.r | ||
* | ||
* console.log(tree.l.l.r.r); | ||
* // [ { id: 3 } ] | ||
@@ -40,14 +42,14 @@ * ``` | ||
* @example | ||
* ```ts | ||
* import { groupBinary, identity, push, reduce } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/group-binary-2.ts | ||
* import { groupBinary, reduce } from "@thi.ng/transducers"; | ||
* | ||
* tree = reduce( | ||
* groupBinary(4, identity, () => [], push(), 0, 1), | ||
* const tree = reduce( | ||
* groupBinary<number>(4, (x) => x, undefined, undefined, 0, 1), | ||
* [1, 2, 3, 4, 5, 6, 7] | ||
* ) | ||
* ); | ||
* | ||
* tree[0][1][0][1] // 0101 == 5 in binary | ||
* console.log(tree[0][1][0][1]); // 0101 == 5 in binary | ||
* // [ 5 ] | ||
* | ||
* tree[0][1][1] // 011* == branch | ||
* console.log(tree[0][1][1]); // 011* == branch | ||
* // [ [ 6 ], [ 7 ] ] | ||
@@ -59,17 +61,25 @@ * ``` | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/group-binary-3.ts | ||
* import { frequencies, groupBinary, reduce } from "@thi.ng/transducers"; | ||
* | ||
* tree = reduce( | ||
* groupBinary(3, (x: string) => x.length, null, frequencies()), | ||
* const tree = reduce( | ||
* groupBinary<string>(3, (x) => x.length, undefined, frequencies()), | ||
* "aa bbb dddd ccccc bbb eeee fff".split(" ") | ||
* ) | ||
* // [ [ undefined, | ||
* // [ Map { 'aa' => 1 }, | ||
* // Map { 'bbb' => 2, 'fff' => 1 } ] ], | ||
* // [ [ Map { 'dddd' => 1, 'eeee' => 1 }, | ||
* // Map { 'ccccc' => 1 } ] ] ] | ||
* ); | ||
* | ||
* tree[0][1][1] | ||
* // Map { 'bbb' => 2, 'fff' => 1 } | ||
* console.log(tree); | ||
* // { | ||
* // l: { | ||
* // r: { | ||
* // l: Map(1) { "aa": 1 }, | ||
* // r: Map(2) { "bbb": 2, "fff": 1 }, | ||
* // }, | ||
* // }, | ||
* // r: { | ||
* // l: { | ||
* // l: Map(2) { "dddd": 1, "eeee": 1 }, | ||
* // r: Map(1) { "ccccc": 1 }, | ||
* // }, | ||
* // }, | ||
* // } | ||
* ``` | ||
@@ -76,0 +86,0 @@ * |
import { groupByObj } from "./group-by-obj.js"; | ||
import { push } from "./push.js"; | ||
const branchPred = (key, b, l, r) => (x) => key(x) & b ? r : l; | ||
const __branchPred = (key, b, l, r) => (x) => key(x) & b ? r : l; | ||
const groupBinary = (bits, key, branch, leaf, left = "l", right = "r") => { | ||
const init = branch || (() => ({})); | ||
let rfn = groupByObj({ | ||
key: branchPred(key, 1, left, right), | ||
key: __branchPred(key, 1, left, right), | ||
group: leaf || push() | ||
@@ -12,3 +12,3 @@ }); | ||
rfn = groupByObj({ | ||
key: branchPred(key, i, left, right), | ||
key: __branchPred(key, i, left, right), | ||
group: [init, rfn[1], rfn[2]] | ||
@@ -15,0 +15,0 @@ }); |
import type { GroupByOpts, Reducer } from "./api.js"; | ||
export declare function groupByMap<SRC, KEY, GROUP>(opts?: Partial<GroupByOpts<SRC, KEY, GROUP>>): Reducer<SRC, Map<KEY, GROUP>>; | ||
export declare function groupByMap<SRC, GROUP>(xs: Iterable<SRC>): Map<SRC, GROUP>; | ||
export declare function groupByMap<SRC, KEY, GROUP>(opts: Partial<GroupByOpts<SRC, KEY, GROUP>>, xs: Iterable<SRC>): Map<KEY, GROUP>; | ||
export declare function groupByMap<SRC, GROUP>(src: Iterable<SRC>): Map<SRC, GROUP>; | ||
export declare function groupByMap<SRC, KEY, GROUP>(opts: Partial<GroupByOpts<SRC, KEY, GROUP>>, src: Iterable<SRC>): Map<KEY, GROUP>; | ||
//# sourceMappingURL=group-by-map.d.ts.map |
import type { IObjectOf } from "@thi.ng/api"; | ||
import type { GroupByOpts, Reducer } from "./api.js"; | ||
/** | ||
* Reducer. Groups inputs, optionally by given key fn and custom reducer. By | ||
* default the values are used as key directly and {@link push} is used as | ||
* default reducer. | ||
* | ||
* @example | ||
* ```ts tangle:../export/group-by-obj.ts | ||
* import { groupByObj } from "@thi.ng/transducers"; | ||
* | ||
* console.log( | ||
* groupByObj( | ||
* // group items by first char | ||
* { key: (x) => x[0] }, | ||
* ["alma", "charlie", "brontë", "anna", "cora", "aurora"] | ||
* ) | ||
* ); | ||
* // { | ||
* // a: [ "alma", "anna", "aurora" ], | ||
* // c: [ "charlie", "cora" ], | ||
* // b: [ "brontë" ], | ||
* // } | ||
* ``` | ||
* | ||
* @example | ||
* ```ts tangle:../export/group-by-obj-2.ts | ||
* import { conj, groupByObj, repeatedly } from "@thi.ng/transducers"; | ||
* | ||
* console.log( | ||
* groupByObj( | ||
* { | ||
* // bin items by multiples of 10 | ||
* key: (x) => Math.floor(x / 10) * 10, | ||
* // keep only uniques (using conj reducer) | ||
* group: conj(), | ||
* }, | ||
* repeatedly(() => Math.floor(Math.random() * 100), 20) | ||
* ) | ||
* ); | ||
* // { | ||
* // "0": Set(1) { 8 }, | ||
* // "10": Set(1) { 13 }, | ||
* // "20": Set(2) { 24, 22 }, | ||
* // "30": Set(2) { 38, 36 }, | ||
* // "50": Set(5) { 54, 53, 52, 56, 59 }, | ||
* // "60": Set(2) { 63, 60 }, | ||
* // "70": Set(4) { 79, 71, 74, 78 }, | ||
* // "80": Set(2) { 85, 81 }, | ||
* // } | ||
* ``` | ||
* | ||
* @param opts | ||
*/ | ||
export declare function groupByObj<SRC, GROUP>(opts?: Partial<GroupByOpts<SRC, PropertyKey, GROUP>>): Reducer<SRC, IObjectOf<GROUP>>; | ||
export declare function groupByObj<SRC>(xs: Iterable<SRC>): IObjectOf<SRC[]>; | ||
export declare function groupByObj<SRC, GROUP>(opts: Partial<GroupByOpts<SRC, PropertyKey, GROUP>>, xs: Iterable<SRC>): IObjectOf<GROUP>; | ||
export declare function groupByObj<SRC>(src: Iterable<SRC>): IObjectOf<SRC[]>; | ||
export declare function groupByObj<SRC, GROUP>(opts: Partial<GroupByOpts<SRC, PropertyKey, GROUP>>, src: Iterable<SRC>): IObjectOf<GROUP>; | ||
//# sourceMappingURL=group-by-obj.d.ts.map |
@@ -13,3 +13,13 @@ import type { FnAny } from "@thi.ng/api"; | ||
*/ | ||
export declare const __mathop: (rfn: FnAny<Reducer<number, number>>, fn: ReductionFn<number, number>, initDefault: number, args: any[]) => any; | ||
export declare const __mathOp: (rfn: FnAny<Reducer<number, number>>, fn: ReductionFn<number, number>, initDefault: number, args: any[]) => any; | ||
/** | ||
* Shared impl for {@link minCompare} & {@link maxCompare}. | ||
* | ||
* @param rfn | ||
* @param args | ||
* @param sign | ||
* | ||
* @internal | ||
*/ | ||
export declare const __compareOp: (rfn: FnAny<Reducer<any, any>>, args: any[], sign: 1 | -1) => any; | ||
//# sourceMappingURL=mathop.d.ts.map |
@@ -0,12 +1,19 @@ | ||
import { compare } from "@thi.ng/compare/compare"; | ||
import { $$reduce, reducer } from "../reduce.js"; | ||
const __mathop = (rfn, fn, initDefault, args) => { | ||
const __mathOp = (rfn, fn, initDefault, args) => { | ||
const res = $$reduce(rfn, args); | ||
if (res !== void 0) { | ||
return res; | ||
} | ||
if (res !== void 0) return res; | ||
const init = args[0] || initDefault; | ||
return reducer(() => init, fn); | ||
}; | ||
const __compareOp = (rfn, args, sign) => { | ||
const res = $$reduce(rfn, args); | ||
if (res !== void 0) return res; | ||
const init = args[0]; | ||
const cmp = args[1] || compare; | ||
return reducer(init, (acc, x) => sign * cmp(acc, x) >= 0 ? acc : x); | ||
}; | ||
export { | ||
__mathop | ||
__compareOp, | ||
__mathOp | ||
}; |
@@ -24,6 +24,6 @@ import type { Fn2 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/interpolate.ts | ||
* import { interpolate } from "@thi.ng/transducers"; | ||
* | ||
* [...interpolate( | ||
* const res = [...interpolate( | ||
* ([a, b], t) => a + (b - a) * t, | ||
@@ -33,4 +33,5 @@ * 2, | ||
* [0, 1, 0, 2] | ||
* )] | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ 0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, | ||
@@ -37,0 +38,0 @@ * // 1, 0.875, 0.75, 0.625, 0.5, 0.375, 0.25, 0.125, |
@@ -13,9 +13,13 @@ import type { Fn2 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/iterate.ts | ||
* import { iterate } from "@thi.ng/transducers"; | ||
* | ||
* [...iterate((x) => x * 2, 1, 5)] | ||
* console.log( | ||
* [...iterate((x) => x * 2, 1, 5)] | ||
* ); | ||
* // [ 1, 2, 4, 8, 16 ] | ||
* | ||
* [...iterate((x, i) => x * 10 + i, 0, 8)] | ||
* console.log( | ||
* [...iterate((x, i) => x * 10 + i, 0, 8)] | ||
* ); | ||
* // [ 0, 1, 12, 123, 1234, 12345, 123456, 1234567 ] | ||
@@ -22,0 +26,0 @@ * ``` |
@@ -8,5 +8,5 @@ import type { FnAny } from "@thi.ng/api"; | ||
* @param xform - | ||
* @param xs - | ||
* @param src - | ||
*/ | ||
export declare function iterator<A, B>(xform: TxLike<A, B>, xs: Iterable<A>): IterableIterator<B>; | ||
export declare function iterator<A, B>(xform: TxLike<A, B>, src: Iterable<A>): IterableIterator<B>; | ||
/** | ||
@@ -20,5 +20,5 @@ * Optimized version of {@link iterator} for transducers which are | ||
* @param xform - | ||
* @param xs - | ||
* @param src - | ||
*/ | ||
export declare function iterator1<A, B>(xform: TxLike<A, B>, xs: Iterable<A>): IterableIterator<B>; | ||
export declare function iterator1<A, B>(xform: TxLike<A, B>, src: Iterable<A>): IterableIterator<B>; | ||
/** | ||
@@ -25,0 +25,0 @@ * Helper function used by various transducers to wrap themselves as |
@@ -6,7 +6,7 @@ import { NO_OP, SEMAPHORE } from "@thi.ng/api/api"; | ||
import { isReduced, unreduced } from "./reduced.js"; | ||
function* iterator(xform, xs) { | ||
function* iterator(xform, src) { | ||
const rfn = ensureTransducer(xform)(push()); | ||
const complete = rfn[1]; | ||
const reduce = rfn[2]; | ||
for (let x of xs) { | ||
for (let x of src) { | ||
const y = reduce([], x); | ||
@@ -23,5 +23,5 @@ if (isReduced(y)) { | ||
} | ||
function* iterator1(xform, xs) { | ||
function* iterator1(xform, src) { | ||
const reduce = ensureTransducer(xform)([NO_OP, NO_OP, (_, x) => x])[2]; | ||
for (let x of xs) { | ||
for (let x of src) { | ||
let y = reduce(SEMAPHORE, x); | ||
@@ -28,0 +28,0 @@ if (isReduced(y)) { |
@@ -14,6 +14,12 @@ import type { Reducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/juxtr.ts | ||
* import { add, juxtR, reduce, reductions, str } from "@thi.ng/transducers"; | ||
* | ||
* reduce(juxtR(add(), reductions(add()), str("-")), [1, 2, 3, 4]); | ||
* console.log( | ||
* reduce( | ||
* // use 3 reducers in parallel | ||
* juxtR(add(), reductions(add()), str("-")), | ||
* [1, 2, 3, 4] | ||
* ) | ||
* ); | ||
* // [ 10, [ 0, 1, 3, 6, 10 ], '1-2-3-4' ] | ||
@@ -20,0 +26,0 @@ * ``` |
@@ -15,6 +15,8 @@ import type { IObjectOf } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/key-permutations.ts | ||
* import { keyPermutations } from "@thi.ng/transducers"; | ||
* | ||
* [...keyPermutations({ a: [1, 2], b: [true, false], c: ["X", "Y"] })] | ||
* console.log( | ||
* [...keyPermutations({ a: [1, 2], b: [true, false], c: ["X", "Y"] })] | ||
* ); | ||
* // [ | ||
@@ -21,0 +23,0 @@ * // { a: 1, b: true, c: 'X' }, |
import type { Reducer } from "./api.js"; | ||
export declare function last<T>(): Reducer<T, T>; | ||
export declare function last<T>(xs: Iterable<T>): T; | ||
export declare function last<T>(src: Iterable<T>): T; | ||
//# sourceMappingURL=last.d.ts.map |
import { NO_OP } from "@thi.ng/api/api"; | ||
import { reduce, reducer } from "./reduce.js"; | ||
function last(xs) { | ||
return xs ? reduce(last(), xs) : reducer(NO_OP, (_, x) => x); | ||
function last(src) { | ||
return src ? reduce(last(), src) : reducer(NO_OP, (_, x) => x); | ||
} | ||
@@ -6,0 +6,0 @@ export { |
@@ -9,6 +9,8 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/length.ts | ||
* import { length } from "@thi.ng/transducers"; | ||
* | ||
* [...length(0, ["a", "bc", "def"])] | ||
* console.log( | ||
* [...length(0, ["a", "bc", "def"])] | ||
* ); | ||
* // [1, 2, 3] | ||
@@ -15,0 +17,0 @@ * ``` |
@@ -14,6 +14,8 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/line.ts | ||
* import { line } from "@thi.ng/transducers"; | ||
* | ||
* [...line(50, 100, 10)] | ||
* console.log( | ||
* [...line(50, 100, 10)] | ||
* ); | ||
* // [ | ||
@@ -20,0 +22,0 @@ * // 50, 55, 60, 65, 70, |
@@ -6,6 +6,8 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/lookup.ts | ||
* import { lookup1d, map } from "@thi.ng/transducers"; | ||
* | ||
* [...map(lookup1d([10, 20, 30]), [2,0,1])] | ||
* console.log( | ||
* [...map(lookup1d([10, 20, 30]), [2,0,1])] | ||
* ); | ||
* // [ 30, 10, 20 ] | ||
@@ -27,6 +29,8 @@ * ``` | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/lookup2d.ts | ||
* import { lookup2d, map, range, range2d } from "@thi.ng/transducers"; | ||
* | ||
* [...map(lookup2d([...range(9)], 3), range2d(2, -1, 0, 3))] | ||
* console.log( | ||
* [...map(lookup2d([...range(9)], 3), range2d(2, -1, 0, 3))] | ||
* ); | ||
* // [ 2, 1, 0, 5, 4, 3, 8, 7, 6 ] | ||
@@ -33,0 +37,0 @@ * ``` |
@@ -11,11 +11,9 @@ import type { Fn2 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* import { assocObj, mapIndexed, transduce } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/map-indexed.ts | ||
* import { mapIndexed } from "@thi.ng/transducers"; | ||
* | ||
* transduce( | ||
* mapIndexed((i, x) => ["id" + i, x * 10], 42), | ||
* assocObj(), | ||
* [1, 2, 3] | ||
* ) | ||
* // { id42: 10, id43: 20, id44: 30 } | ||
* console.log( | ||
* [...mapIndexed((i, x) => ["id" + i, x * 10], 42, [1, 2, 3])] | ||
* ); | ||
* // [ [ "id42", 10 ], [ "id43", 20 ], [ "id44", 30 ] ] | ||
* ``` | ||
@@ -22,0 +20,0 @@ * |
@@ -11,6 +11,6 @@ import type { Fn2, IObjectOf } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/map-keys.ts | ||
* import { mapKeys } from "@thi.ng/transducers"; | ||
* | ||
* [...mapKeys( | ||
* const res = [...mapKeys( | ||
* { | ||
@@ -21,3 +21,5 @@ * a: (x) => x != null ? x * 10 : x, | ||
* [{a: 1, b: 2}, {c: 3, d: 4}] | ||
* )] | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ { a: 10, b: 200 }, { c: 3, d: 4, b: 'n/a', a: undefined } ] | ||
@@ -24,0 +26,0 @@ * ``` |
@@ -12,11 +12,15 @@ import type { Fn } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* import { mapNth } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/map-nth.ts | ||
* import { mapNth, range } from "@thi.ng/transducers"; | ||
* | ||
* [...mapNth(3, (x) => x * 10, range(1,10))] | ||
* // [ 10, 2, 3, 40, 5, 6, 70, 8, 9 ] | ||
* console.log( | ||
* [...mapNth(3, (x) => `*${x}*`, range(1, 10))] | ||
* ); | ||
* // [ "*1*", 2, 3, "*4*", 5, 6, "*7*", 8, 9 ] | ||
* | ||
* // with offset | ||
* [...mapNth(3, 5, (x) => x * 10, range(1,10))] | ||
* // [ 1, 2, 3, 4, 5, 60, 7, 8, 90 ] | ||
* console.log( | ||
* [...mapNth(3, 5, (x) => x * 100, range(1, 10))] | ||
* ); | ||
* // [ 1, 2, 3, 4, 5, 600, 7, 8, 900 ] | ||
* ``` | ||
@@ -23,0 +27,0 @@ * |
@@ -13,6 +13,8 @@ import type { Fn, IObjectOf } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/map-vals.ts | ||
* import { mapVals } from "@thi.ng/transducers"; | ||
* | ||
* [...mapVals((x)=> x * 10, [{a: 1, b: 2}, {c: 3, d: 4}])] | ||
* console.log( | ||
* [...mapVals((x)=> x * 10, [{a: 1, b: 2}, {c: 3, d: 4}])] | ||
* ); | ||
* // [ { a: 10, b: 20 }, { c: 30, d: 40 } ] | ||
@@ -19,0 +21,0 @@ * ``` |
@@ -8,6 +8,8 @@ import type { Fn } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/map.ts | ||
* import { map } from "@thi.ng/transducers"; | ||
* | ||
* [...map((x) => x * 10, [1, 2, 3])] | ||
* console.log( | ||
* [...map((x) => x * 10, [1, 2, 3])] | ||
* ); | ||
* // [ 10, 20, 30 ] | ||
@@ -14,0 +16,0 @@ * ``` |
import type { Fn, Nullable } from "@thi.ng/api"; | ||
import type { Transducer } from "./api.js"; | ||
import type { MaybeReduced, Transducer } from "./api.js"; | ||
/** | ||
@@ -10,9 +10,13 @@ * Transducer. Similar to {@link map}, but expects the given mapping function | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/mapcat.ts | ||
* import { mapcat } from "@thi.ng/transducers"; | ||
* | ||
* [...mapcat((x) => [x, x], [1, 2, 3])] | ||
* console.log( | ||
* [...mapcat((x) => [x, x], [1, 2, 3])] | ||
* ); | ||
* // [ 1, 1, 2, 2, 3, 3 ] | ||
* | ||
* [...mapcat((x) => x > 2 ? [x, x, x] : null, [1, 2, 3])] | ||
* console.log( | ||
* [...mapcat((x) => x > 2 ? [x, x, x] : null, [1, 2, 3])] | ||
* ); | ||
* // [ 3, 3, 3 ] | ||
@@ -23,4 +27,4 @@ * ``` | ||
*/ | ||
export declare function mapcat<A, B>(fn: Fn<A, Nullable<Iterable<B>>>): Transducer<A, B>; | ||
export declare function mapcat<A, B>(fn: Fn<A, Nullable<Iterable<B>>>, src: Iterable<A>): IterableIterator<B>; | ||
export declare function mapcat<A, B>(fn: Fn<A, MaybeReduced<Nullable<Iterable<B>>>>): Transducer<A, B>; | ||
export declare function mapcat<A, B>(fn: Fn<A, MaybeReduced<Nullable<Iterable<B>>>>, src: Iterable<A>): IterableIterator<B>; | ||
//# sourceMappingURL=mapcat.d.ts.map |
import type { Maybe, Predicate } from "@thi.ng/api"; | ||
import type { Transducer } from "./api.js"; | ||
/** | ||
* Transducer composition / syntax sugar for: | ||
* Transducer. Yields none or only the first value which passed the predicate | ||
* check and then causes early termination. If `src` input is given, returns | ||
* first match found (or `undefined`). Also see {@link matchLast}. | ||
* | ||
* @example | ||
* ```ts | ||
* import { comp, filter, take } from "@thi.ng/transducers"; | ||
* @remarks | ||
* `matchFirst()` is syntax sugar for: `comp(filter(pred), take(1))` | ||
* | ||
* comp(filter(pred), take(1)) | ||
* // [Function] | ||
* ``` | ||
* | ||
* Yields none or only the first value which passed the predicate check | ||
* and then causes early termination. If `src` input is given, returns | ||
* first match found (or `undefined`). Also see {@link matchLast}. | ||
* | ||
* @example | ||
* ```ts | ||
* import { comp, map, matchFirst, push, transduce } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/match-first.ts | ||
* import { | ||
* comp, map, matchFirst, push, transduce | ||
* } from "@thi.ng/transducers"; | ||
* | ||
* matchFirst((x) => x >= 5, [3, 1, 4, 2, 6, 5]) | ||
* console.log( | ||
* matchFirst((x) => x >= 5, [3, 1, 4, 2, 6, 5]) | ||
* ); | ||
* // 6 | ||
* | ||
* transduce( | ||
* const res = transduce( | ||
* comp( | ||
@@ -32,3 +29,5 @@ * matchFirst((x) => x >= 5), | ||
* [3, 1, 4, 2, 6, 5] | ||
* ) | ||
* ); | ||
* | ||
* console.log(res); | ||
* // [60] | ||
@@ -35,0 +34,0 @@ * ``` |
import type { Maybe, Predicate } from "@thi.ng/api"; | ||
import type { Transducer } from "./api.js"; | ||
/** | ||
* Similar to {@link matchFirst}, but matches yields none or only the **last** | ||
* value which passed the predicate check. If `src` input is given, returns last | ||
* match found (or `undefined`). | ||
* Similar to {@link matchFirst}, but matches only the **last** value which | ||
* passed the predicate check. If `src` input is given, returns last match found | ||
* (or `undefined`). | ||
* | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/match-last.ts | ||
* import { comp, map, matchLast, push, transduce } from "@thi.ng/transducers"; | ||
* | ||
* matchLast((x) => x >= 5, [3, 1, 6, 5, 4, 2]) | ||
* console.log( | ||
* matchLast((x) => x >= 5, [3, 1, 6, 5, 4, 2]) | ||
* ); | ||
* // 5 | ||
* | ||
* transduce( | ||
* const res = transduce( | ||
* comp( | ||
@@ -22,4 +24,6 @@ * matchLast((x) => x >= 5), | ||
* [3, 1, 6, 5, 4, 2] | ||
* ) | ||
* // 50 | ||
* ); | ||
* | ||
* console.log(res); | ||
* // [ 50 ] | ||
* ``` | ||
@@ -26,0 +30,0 @@ * |
@@ -1,6 +0,6 @@ | ||
import type { Comparator, Fn0 } from "@thi.ng/api"; | ||
import type { Comparator, Fn0, Nullable } from "@thi.ng/api"; | ||
import type { Reducer } from "./api.js"; | ||
export declare function maxCompare<T>(init: Fn0<T>, cmp?: Comparator<T>): Reducer<T, T>; | ||
export declare function maxCompare<T>(init: Fn0<T>, xs: Iterable<T>): T; | ||
export declare function maxCompare<T>(init: Fn0<T>, cmp: Comparator<T>, xs: Iterable<T>): T; | ||
export declare function maxCompare<T>(init: Fn0<T>, src: Iterable<T>): T; | ||
export declare function maxCompare<T>(init: Fn0<T>, cmp: Nullable<Comparator<T>>, src: Iterable<T>): T; | ||
//# sourceMappingURL=max-compare.d.ts.map |
@@ -1,11 +0,4 @@ | ||
import { compare } from "@thi.ng/compare/compare"; | ||
import { $$reduce, reducer } from "./reduce.js"; | ||
import { __compareOp } from "./internal/mathop.js"; | ||
function maxCompare(...args) { | ||
const res = $$reduce(maxCompare, args); | ||
if (res !== void 0) { | ||
return res; | ||
} | ||
const init = args[0]; | ||
const cmp = args[1] || compare; | ||
return reducer(init, (acc, x) => cmp(acc, x) >= 0 ? acc : x); | ||
return __compareOp(maxCompare, args, 1); | ||
} | ||
@@ -12,0 +5,0 @@ export { |
@@ -7,3 +7,3 @@ import type { Reducer } from "./api.js"; | ||
export declare function maxMag(): Reducer<number, number>; | ||
export declare function maxMag(xs: Iterable<number>): number; | ||
export declare function maxMag(src: Iterable<number>): number; | ||
//# sourceMappingURL=max-mag.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function maxMag(xs) { | ||
return xs ? reduce(maxMag(), xs) : reducer( | ||
function maxMag(src) { | ||
return src ? reduce(maxMag(), src) : reducer( | ||
() => 0, | ||
@@ -5,0 +5,0 @@ (acc, x) => Math.abs(x) > Math.abs(acc) ? x : acc |
import type { Reducer } from "./api.js"; | ||
export declare function max(): Reducer<number, number>; | ||
export declare function max(xs: Iterable<number>): number; | ||
export declare function max(src: Iterable<number>): number; | ||
//# sourceMappingURL=max.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function max(xs) { | ||
return xs ? reduce(max(), xs) : reducer( | ||
function max(src) { | ||
return src ? reduce(max(), src) : reducer( | ||
() => -Infinity, | ||
@@ -5,0 +5,0 @@ (acc, x) => Math.max(acc, x) |
@@ -7,3 +7,3 @@ import type { Reducer } from "./api.js"; | ||
export declare function mean(): Reducer<number, number>; | ||
export declare function mean(xs: Iterable<number>): number; | ||
export declare function mean(src: Iterable<number>): number; | ||
//# sourceMappingURL=mean.d.ts.map |
import { reduce } from "./reduce.js"; | ||
function mean(xs) { | ||
function mean(src) { | ||
let n = 1; | ||
return xs ? reduce(mean(), xs) : [ | ||
return src ? reduce(mean(), src) : [ | ||
() => n = 0, | ||
@@ -6,0 +6,0 @@ (acc) => n > 1 ? acc / n : acc, |
@@ -1,6 +0,6 @@ | ||
import type { Comparator, Fn0 } from "@thi.ng/api"; | ||
import type { Comparator, Fn0, Nullable } from "@thi.ng/api"; | ||
import type { Reducer } from "./api.js"; | ||
export declare function minCompare<T>(init: Fn0<T>, cmp?: Comparator<T>): Reducer<T, T>; | ||
export declare function minCompare<T>(init: Fn0<T>, xs: Iterable<T>): T; | ||
export declare function minCompare<T>(init: Fn0<T>, cmp: Comparator<T>, xs: Iterable<T>): T; | ||
export declare function minCompare<T>(init: Fn0<T>, src: Iterable<T>): T; | ||
export declare function minCompare<T>(init: Fn0<T>, cmp: Nullable<Comparator<T>>, src: Iterable<T>): T; | ||
//# sourceMappingURL=min-compare.d.ts.map |
@@ -1,11 +0,4 @@ | ||
import { compare } from "@thi.ng/compare/compare"; | ||
import { $$reduce, reducer } from "./reduce.js"; | ||
import { __compareOp } from "./internal/mathop.js"; | ||
function minCompare(...args) { | ||
const res = $$reduce(minCompare, args); | ||
if (res !== void 0) { | ||
return res; | ||
} | ||
const init = args[0]; | ||
const cmp = args[1] || compare; | ||
return reducer(init, (acc, x) => cmp(acc, x) <= 0 ? acc : x); | ||
return __compareOp(minCompare, args, -1); | ||
} | ||
@@ -12,0 +5,0 @@ export { |
@@ -7,3 +7,3 @@ import type { Reducer } from "./api.js"; | ||
export declare function minMag(): Reducer<number, number>; | ||
export declare function minMag(xs: Iterable<number>): number; | ||
export declare function minMag(src: Iterable<number>): number; | ||
//# sourceMappingURL=min-mag.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function minMag(xs) { | ||
return xs ? reduce(minMag(), xs) : reducer( | ||
function minMag(src) { | ||
return src ? reduce(minMag(), src) : reducer( | ||
() => Infinity, | ||
@@ -5,0 +5,0 @@ (acc, x) => Math.abs(x) < Math.abs(acc) ? x : acc |
import type { Reducer } from "./api.js"; | ||
export declare function min(): Reducer<number, number>; | ||
export declare function min(xs: Iterable<number>): number; | ||
export declare function min(src: Iterable<number>): number; | ||
//# sourceMappingURL=min.d.ts.map |
import { reduce, reducer } from "./reduce.js"; | ||
function min(xs) { | ||
return xs ? reduce(min(), xs) : reducer( | ||
function min(src) { | ||
return src ? reduce(min(), src) : reducer( | ||
() => Infinity, | ||
@@ -5,0 +5,0 @@ (acc, x) => Math.min(acc, x) |
@@ -7,4 +7,4 @@ import type { Reducer } from "./api.js"; | ||
export declare function mul(init?: number): Reducer<number, number>; | ||
export declare function mul(xs: Iterable<number>): number; | ||
export declare function mul(init: number, xs: Iterable<number>): number; | ||
export declare function mul(src: Iterable<number>): number; | ||
export declare function mul(init: number, src: Iterable<number>): number; | ||
//# sourceMappingURL=mul.d.ts.map |
@@ -1,4 +0,4 @@ | ||
import { __mathop } from "./internal/mathop.js"; | ||
import { __mathOp } from "./internal/mathop.js"; | ||
function mul(...args) { | ||
return __mathop(mul, (acc, x) => acc * x, 1, args); | ||
return __mathOp(mul, (acc, x) => acc * x, 1, args); | ||
} | ||
@@ -5,0 +5,0 @@ export { |
@@ -8,6 +8,6 @@ import type { IObjectOf } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* import { multiplexObj } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/multiplex-obj.ts | ||
* import { multiplexObj, map } from "@thi.ng/transducers"; | ||
* | ||
* [...multiplexObj( | ||
* const res = [...multiplexObj( | ||
* { | ||
@@ -19,6 +19,10 @@ * initial: map(x => x.charAt(0)), | ||
* ["Alice", "Bob", "Charlie"] | ||
* )] | ||
* // [ { length: 5, upper: 'ALICE', initial: 'A' }, | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ | ||
* // { length: 5, upper: 'ALICE', initial: 'A' }, | ||
* // { length: 3, upper: 'BOB', initial: 'B' }, | ||
* // { length: 7, upper: 'CHARLIE', initial: 'C' } ] | ||
* // { length: 7, upper: 'CHARLIE', initial: 'C' } | ||
* // ] | ||
* ``` | ||
@@ -25,0 +29,0 @@ * |
@@ -24,6 +24,6 @@ import type { MultiplexTxLike, Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/multiplex.ts | ||
* import { iterator, map, multiplex } from "@thi.ng/transducers"; | ||
* | ||
* [...iterator( | ||
* const res = [...iterator( | ||
* multiplex( | ||
@@ -35,4 +35,5 @@ * map(x => x.charAt(0)), | ||
* ["Alice", "Bob", "Charlie"] | ||
* )] | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ [ "A", "ALICE", 5 ], [ "B", "BOB", 3 ], [ "C", "CHARLIE", 7 ] ] | ||
@@ -42,6 +43,6 @@ * ``` | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/multiplex-2.ts | ||
* import { iterator, map, mapcat, multiplex } from "@thi.ng/transducers"; | ||
* | ||
* [...iterator( | ||
* const res = [...iterator( | ||
* multiplex( | ||
@@ -55,4 +56,5 @@ * // override default unwrap behavior for this transducer | ||
* [[1, 2], [3]] | ||
* )] | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ [ [ 1, 2 ], [ 1, 2 ] ], [ [ 3 ], [ 3 ] ] ] | ||
@@ -59,0 +61,0 @@ * |
@@ -12,3 +12,3 @@ import type { Reducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/norm-count.ts | ||
* import { filter, normCount, transduce } from "@thi.ng/transducers"; | ||
@@ -19,3 +19,5 @@ * | ||
* // compute percentage of values < 3 | ||
* transduce(filter(x => x<3), normCount(items.length), items); | ||
* console.log( | ||
* transduce(filter(x => x<3), normCount(items.length), items) | ||
* ); | ||
* // 0.7 | ||
@@ -27,3 +29,3 @@ * ``` | ||
export declare function normCount(norm: number): Reducer<any, number>; | ||
export declare function normCount(norm: number, xs: Iterable<any>): number; | ||
export declare function normCount(norm: number, src: Iterable<any>): number; | ||
//# sourceMappingURL=norm-count.d.ts.map |
@@ -8,3 +8,3 @@ import type { Fn } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/norm-frequencies-auto.ts | ||
* import { normFrequenciesAuto } from "@thi.ng/transducers"; | ||
@@ -14,7 +14,11 @@ * | ||
* | ||
* normFrequenciesAuto(items) | ||
* console.log( | ||
* normFrequenciesAuto(items) | ||
* ); | ||
* // Map(5) { 1 => 0.4, 2 => 0.3, 3 => 0.1, 4 => 0.1, 5 => 0.1 } | ||
* | ||
* // frequencies by 1st letter | ||
* normFrequenciesAuto(x => x[0], ["alice", "abba", "bob", "charlie"]) | ||
* console.log( | ||
* normFrequenciesAuto(x => x[0], ["alice", "abba", "bob", "charlie"]) | ||
* ); | ||
* // Map(3) { 'a' => 0.5, 'b' => 0.25, 'c' => 0.25 } | ||
@@ -24,5 +28,5 @@ * ``` | ||
export declare function normFrequenciesAuto<A>(): Reducer<A, Map<A, number>>; | ||
export declare function normFrequenciesAuto<A>(xs: Iterable<A>): Map<A, number>; | ||
export declare function normFrequenciesAuto<A>(src: Iterable<A>): Map<A, number>; | ||
export declare function normFrequenciesAuto<A, B>(key: Fn<A, B>): Reducer<A, Map<B, number>>; | ||
export declare function normFrequenciesAuto<A, B>(key: Fn<A, B>, xs: Iterable<A>): Map<B, number>; | ||
export declare function normFrequenciesAuto<A, B>(key: Fn<A, B>, src: Iterable<A>): Map<B, number>; | ||
//# sourceMappingURL=norm-frequencies-auto.d.ts.map |
@@ -11,3 +11,3 @@ import type { Fn } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/norm-frequencies.ts | ||
* import { normFrequencies } from "@thi.ng/transducers"; | ||
@@ -17,4 +17,12 @@ * | ||
* | ||
* normFrequencies(10, items) | ||
* // Map(5) { 1 => 0.4, 2 => 0.3, 3 => 0.1, 4 => 0.1, 5 => 0.1 } | ||
* console.log( | ||
* normFrequencies(10, items) | ||
* ); | ||
* // Map(5) { | ||
* // 1 => 0.4, | ||
* // 2 => 0.3, | ||
* // 3 => 0.1, | ||
* // 4 => 0.1, | ||
* // 5 => 0.1, | ||
* // } | ||
* ``` | ||
@@ -25,5 +33,5 @@ * | ||
export declare function normFrequencies<A>(norm: number): Reducer<A, Map<A, number>>; | ||
export declare function normFrequencies<A>(norm: number, xs: Iterable<A>): Map<A, number>; | ||
export declare function normFrequencies<A>(norm: number, src: Iterable<A>): Map<A, number>; | ||
export declare function normFrequencies<A, B>(norm: number, key: Fn<A, B>): Reducer<A, Map<B, number>>; | ||
export declare function normFrequencies<A, B>(norm: number, key: Fn<A, B>, xs: Iterable<A>): Map<B, number>; | ||
export declare function normFrequencies<A, B>(norm: number, key: Fn<A, B>, src: Iterable<A>): Map<B, number>; | ||
//# sourceMappingURL=norm-frequencies.d.ts.map |
@@ -7,15 +7,23 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/norm-range.ts | ||
* import { normRange } from "@thi.ng/transducers"; | ||
* | ||
* [...normRange(4)] | ||
* console.log( | ||
* [...normRange(4)] | ||
* ); | ||
* // [0, 0.25, 0.5, 0.75, 1.0] | ||
* | ||
* [...normRange(4, false)] | ||
* console.log( | ||
* [...normRange(4, false)] | ||
* ); | ||
* // [0, 0.25, 0.5, 0.75] | ||
* | ||
* [...normRange(4, true, true)] | ||
* console.log( | ||
* [...normRange(4, true, true)] | ||
* ); | ||
* // [1, 0.75, 0.5, 0.25, 0] | ||
* | ||
* [...normRange(4, false, true)] | ||
* console.log( | ||
* [...normRange(4, false, true)] | ||
* ); | ||
* // [1, 0.75, 0.5, 0.25] | ||
@@ -22,0 +30,0 @@ * ``` |
{ | ||
"name": "@thi.ng/transducers", | ||
"version": "9.0.5", | ||
"version": "9.0.6", | ||
"description": "Lightweight transducer implementations for ES6 / TypeScript", | ||
@@ -13,3 +13,3 @@ "type": "module", | ||
}, | ||
"homepage": "https://github.com/thi-ng/umbrella/tree/develop/packages/transducers#readme", | ||
"homepage": "https://thi.ng/transducers", | ||
"funding": [ | ||
@@ -44,16 +44,16 @@ { | ||
"dependencies": { | ||
"@thi.ng/api": "^8.11.2", | ||
"@thi.ng/arrays": "^2.9.6", | ||
"@thi.ng/checks": "^3.6.4", | ||
"@thi.ng/compare": "^2.3.5", | ||
"@thi.ng/compose": "^3.0.4", | ||
"@thi.ng/errors": "^2.5.7", | ||
"@thi.ng/math": "^5.10.13", | ||
"@thi.ng/random": "^3.8.0" | ||
"@thi.ng/api": "^8.11.3", | ||
"@thi.ng/arrays": "^2.9.7", | ||
"@thi.ng/checks": "^3.6.5", | ||
"@thi.ng/compare": "^2.3.6", | ||
"@thi.ng/compose": "^3.0.5", | ||
"@thi.ng/errors": "^2.5.8", | ||
"@thi.ng/math": "^5.11.0", | ||
"@thi.ng/random": "^3.8.1" | ||
}, | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.43.2", | ||
"esbuild": "^0.21.1", | ||
"@microsoft/api-extractor": "^7.47.0", | ||
"esbuild": "^0.21.5", | ||
"typedoc": "^0.25.13", | ||
"typescript": "^5.4.5" | ||
"typescript": "^5.5.2" | ||
}, | ||
@@ -594,3 +594,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "df34b4a9e650cc7323575356de207d78933bdcf3\n" | ||
"gitHead": "154c95cf9d6bab32174498ec3b5b5d87e42be7f9\n" | ||
} |
import type { Transducer } from "./api.js"; | ||
/** | ||
* Ensures the total number of transformed values will be multiples of | ||
* `n`. | ||
* Ensures the total number of transformed values will be multiples of `n`. | ||
* | ||
* @remarks | ||
* Only makes sense for finite streams / reductions. Does nothing if the | ||
* to be transformed data source has exactly multiple of `n` values, but | ||
* if not pads / supplies additional `fill` values at the end until the | ||
* next multiple is reached. No padding takes place if input is empty, | ||
* since length 0 is always a multiple. | ||
* Only makes sense for finite streams / reductions. Does nothing if the to be | ||
* transformed data source has exactly multiple of `n` values, but if not pads / | ||
* supplies additional `fill` values at the end until the next multiple is | ||
* reached. No padding takes place if input is empty, since length 0 is always a | ||
* multiple. | ||
* | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/pad-last.ts | ||
* import { padLast } from "@thi.ng/transducers"; | ||
* | ||
* [...padLast(8, 0, [1, 2, 3, 4, 5])] | ||
* console.log( | ||
* [...padLast(8, 0, [1, 2, 3, 4, 5])] | ||
* ); | ||
* // [ 1, 2, 3, 4, 5, 0, 0, 0 ] | ||
* | ||
* [...padLast(8, 0, [1])] | ||
* console.log( | ||
* [...padLast(8, 0, [1])] | ||
* ); | ||
* // [ 1, 0, 0, 0, 0, 0, 0, 0 ] | ||
* | ||
* [...padLast(8, 0, [])] | ||
* console.log( | ||
* [...padLast(8, 0, [])] | ||
* ); | ||
* // [] | ||
* | ||
* [...padLast(2, 0, [1, 2, 3])] | ||
* // [ 1, 2, 3, 0 ] | ||
* console.log( | ||
* [...padLast(4, 0, [1, 2])] | ||
* ); | ||
* // [ 1, 2, 0, 0 ] | ||
* | ||
* [...padLast(2, 0, [1, 2, 3, 4])] | ||
* // [ 1, 2, 3, 4 ] | ||
* console.log( | ||
* [...padLast(4, 0, [1, 2, 3, 4, 5])] | ||
* ); | ||
* // [ 1, 2, 3, 4, 5, 0, 0, 0 ] | ||
* ``` | ||
@@ -32,0 +41,0 @@ * |
@@ -12,13 +12,35 @@ /** | ||
* @example | ||
* ```ts | ||
* import { concat, repeat } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/pad-sides.ts | ||
* import { padSides, range } from "@thi.ng/transducers"; | ||
* | ||
* // pad both sides with 10 | ||
* console.log( | ||
* [...padSides(range(3), 10)] | ||
* ); | ||
* | ||
* // pad both sides 3x | ||
* console.log( | ||
* [...padSides(range(3), 10, 3)] | ||
* ); | ||
* | ||
* // left/start only | ||
* console.log( | ||
* [...padSides(range(3), 10, 3, 0)] | ||
* ); | ||
* | ||
* // right/end only | ||
* console.log( | ||
* [...padSides(range(3), 10, 0, 3)] | ||
* ); | ||
* | ||
* // padSides() is syntax sugar for: | ||
* | ||
* // default | ||
* concat(repeat(x, numLeft), src, repeat(x, numRight)) | ||
* // concat(repeat(x, numLeft), src, repeat(x, numRight)) | ||
* | ||
* // left only | ||
* concat(repeat(x, numLeft), src) | ||
* // concat(repeat(x, numLeft), src) | ||
* | ||
* // right only | ||
* concat(src, repeat(x, numRight)) | ||
* // concat(src, repeat(x, numRight)) | ||
* ``` | ||
@@ -25,0 +47,0 @@ * |
@@ -11,15 +11,23 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/page.ts | ||
* import { page, range } from "@thi.ng/transducers"; | ||
* | ||
* [...page(0, 5, range(12))] | ||
* console.log( | ||
* [...page(0, 5, range(12))] | ||
* ); | ||
* // [ 0, 1, 2, 3, 4 ] | ||
* | ||
* [...page(1, 5, range(12))] | ||
* console.log( | ||
* [...page(1, 5, range(12))] | ||
* ); | ||
* // [ 5, 6, 7, 8, 9 ] | ||
* | ||
* [...page(2, 5, range(12))] | ||
* console.log( | ||
* [...page(2, 5, range(12))] | ||
* ); | ||
* // [ 10, 11 ] | ||
* | ||
* [...page(3, 5, range(12))] | ||
* console.log( | ||
* [...page(3, 5, range(12))] | ||
* ); | ||
* // [] | ||
@@ -26,0 +34,0 @@ * ``` |
import type { IObjectOf } from "@thi.ng/api"; | ||
/** | ||
* Iterator yielding key-value pairs of given object's own properties and their | ||
* values. Same as `zip(keys(x), vals(x))`. | ||
* values. Same as `zip(keys(x), vals(x))` or iterator version of | ||
* `Object.entries(x)`. | ||
* | ||
@@ -11,6 +12,8 @@ * @remarks | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/pairs.ts | ||
* import { pairs } from "@thi.ng/transducers"; | ||
* | ||
* [...pairs({ a: 1, b: 2 })] | ||
* console.log( | ||
* [...pairs({ a: 1, b: 2 })] | ||
* ); | ||
* // [['a', 1], ['b', 2]] | ||
@@ -17,0 +20,0 @@ * ``` |
@@ -7,11 +7,24 @@ /** | ||
* In the general case, this is similar to `concat(x, reverse(x))`, | ||
* though keeps input type intact. | ||
* though this function keeps input type intact. | ||
* | ||
* Uses {@link symmetric}.for all inputs other than arrays or strings. | ||
* | ||
* @example | ||
* ```ts | ||
* import { palindrome } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/palindrome.ts | ||
* import { palindrome, range } from "@thi.ng/transducers"; | ||
* | ||
* palindrome("hello"); // "helloolleh" | ||
* palindrome([1, 2, 3]); // [1, 2, 3, 3, 2, 1] | ||
* palindrome(range(3)); // IterableIterator<number> | ||
* console.log( | ||
* palindrome("hello") | ||
* ); | ||
* // "helloolleh" | ||
* | ||
* console.log( | ||
* palindrome([1, 2, 3]) | ||
* ); | ||
* // [1, 2, 3, 3, 2, 1] | ||
* | ||
* console.log( | ||
* [...palindrome(range(3))] | ||
* ); | ||
* // [ 0, 1, 2, 2, 1, 0 ] | ||
* ``` | ||
@@ -18,0 +31,0 @@ * |
@@ -1,2 +0,1 @@ | ||
import { ensureArray } from "@thi.ng/arrays/ensure-array"; | ||
import { isArray } from "@thi.ng/checks/is-array"; | ||
@@ -7,4 +6,5 @@ import { isString } from "@thi.ng/checks/is-string"; | ||
import { str } from "./str.js"; | ||
import { symmetric } from "./symmetric.js"; | ||
function palindrome(x) { | ||
return isString(x) ? str("", concat([x], reverse(x))) : isArray(x) ? x.concat(x.slice().reverse()) : (x = ensureArray(x), concat(x, reverse(x))); | ||
return isString(x) ? str("", concat([x], reverse(x))) : isArray(x) ? x.concat(x.slice().reverse()) : symmetric(x); | ||
} | ||
@@ -11,0 +11,0 @@ export { |
@@ -9,6 +9,8 @@ import type { Fn } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/partition-by.ts | ||
* import { partitionBy } from "@thi.ng/transducers"; | ||
* | ||
* [...partitionBy((x) => x & 1, [1, 2, 4, 6, 3, 5, 8, 4])] | ||
* console.log( | ||
* [...partitionBy((x) => x & 1, [1, 2, 4, 6, 3, 5, 8, 4])] | ||
* ); | ||
* // [ [ 1 ], [ 2, 4, 6 ], [ 3, 5 ], [ 8, 4 ] ] | ||
@@ -15,0 +17,0 @@ * ``` |
@@ -7,6 +7,8 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* import { partitionOf } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/partition-of.ts | ||
* import { partitionOf, range } from "@thi.ng/transducers"; | ||
* | ||
* [...partitionOf([3,2,4], range(20))] | ||
* console.log( | ||
* [...partitionOf([3, 2, 4], range(20))] | ||
* ); | ||
* // [ [ 0, 1, 2 ], | ||
@@ -13,0 +15,0 @@ * // [ 3, 4 ], |
@@ -13,10 +13,14 @@ import type { SortOpts, Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/partition-sort.ts | ||
* import { partitionSort } from "@thi.ng/transducers"; | ||
* | ||
* [...partitionSort(4, [5,9,2,6,4,1,3,8,7,0])] | ||
* console.log( | ||
* [...partitionSort(4, [5, 9, 2, 6, 4, 1, 3, 8, 7, 0])] | ||
* ); | ||
* // [ 2, 5, 6, 9, 1, 3, 4, 8, 0, 7 ] | ||
* | ||
* // with key fn and custom comparator | ||
* [...partitionSort(3, (x) => x.val, (a, b) => b - a, | ||
* const res = [...partitionSort( | ||
* 3, | ||
* { key: (x) => x.val, compare: (a, b) => b - a }, | ||
* [ | ||
@@ -27,3 +31,5 @@ * { id: "a", val: 5 }, | ||
* ] | ||
* )] | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ { id: 'c', val: 8 }, { id: 'b', val: 7 }, { id: 'a', val: 5 } ] | ||
@@ -30,0 +36,0 @@ * ``` |
@@ -71,6 +71,6 @@ import type { Fn, IObjectOf } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/partition-sync.ts | ||
* import { partitionSync } from "@thi.ng/transducers"; | ||
* | ||
* src = [ | ||
* const src = [ | ||
* ["a", 1], ["a", 2], ["d", 100], ["b", 10], | ||
@@ -82,3 +82,5 @@ * ["b", 11], ["c", 0], ["a", 3] | ||
* // here the label is the first element of each input item | ||
* [...partitionSync(["a", "b"], { key: (x) => x[0] }, src)] | ||
* console.log( | ||
* [...partitionSync(["a", "b"], { key: (x) => x[0] }, src)] | ||
* ); | ||
* // [ { a: ["a", 2], b: ["b", 10] }, | ||
@@ -96,7 +98,12 @@ * // { b: ["b", 11], a: ["a", 3] } ] | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/partition-sync-2.ts | ||
* import { partitionSync } from "@thi.ng/transducers"; | ||
* | ||
* const src = [ | ||
* ["a", 1], ["a", 2], ["d", 100], ["b", 10], | ||
* ["b", 11], ["c", 0], ["a", 3] | ||
* ]; | ||
* | ||
* // passing `false` to disable tuple reset | ||
* [...partitionSync( | ||
* const res = [...partitionSync( | ||
* ["a", "b"], | ||
@@ -108,3 +115,5 @@ * { | ||
* src | ||
* )] | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ { a: ["a", 2], b: ["b", 10] }, | ||
@@ -111,0 +120,0 @@ * // { a: ["a", 2], b: ["b", 11] }, |
@@ -39,3 +39,3 @@ import { isArray } from "@thi.ng/checks/is-array"; | ||
currKeys.add(k); | ||
if (mergeOnly || requiredInputs(requiredKeys, currKeys)) { | ||
if (mergeOnly || __requiredInputs(requiredKeys, currKeys)) { | ||
acc = reduce(acc, curr); | ||
@@ -59,3 +59,3 @@ first = false; | ||
if (all && currKeys.size > 0) { | ||
acc = reduce(acc, collect(cache, currKeys)); | ||
acc = reduce(acc, __collect(cache, currKeys)); | ||
cache.clear(); | ||
@@ -78,4 +78,4 @@ currKeys.clear(); | ||
currKeys.add(k); | ||
while (requiredInputs(requiredKeys, currKeys)) { | ||
acc = reduce(acc, collect(cache, currKeys)); | ||
while (__requiredInputs(requiredKeys, currKeys)) { | ||
acc = reduce(acc, __collect(cache, currKeys)); | ||
first = false; | ||
@@ -110,3 +110,3 @@ if (isReduced(acc)) break; | ||
} | ||
const requiredInputs = (required, curr) => { | ||
const __requiredInputs = (required, curr) => { | ||
if (curr.size < required.size) return false; | ||
@@ -118,3 +118,3 @@ for (let id of required) { | ||
}; | ||
const collect = (cache, currKeys) => { | ||
const __collect = (cache, currKeys) => { | ||
const curr = {}; | ||
@@ -121,0 +121,0 @@ for (let id of currKeys) { |
@@ -11,10 +11,12 @@ import type { Transducer } from "./api.js"; | ||
* Also see: | ||
* - [`thi.ng/transducers-async`](https://thi.ng/transducers-async). | ||
* - [`thi.ng/rstream`](https://thi.ng/rstream) | ||
* - [`thi.ng/csp`](https://thi.ng/csp). | ||
* | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/partition-time.ts | ||
* import { fromInterval, trace } from "@thi.ng/rstream"; | ||
* import { partitionTime } from "@thi.ng/transducers"; | ||
* | ||
* // stream emits | ||
* // stream emits counter value every 250ms | ||
* // callect & partition into tuples every 1000ms | ||
* fromInterval(250) | ||
@@ -21,0 +23,0 @@ * .transform(partitionTime(1000)) |
@@ -9,6 +9,8 @@ import type { Predicate } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/partition-when.ts | ||
* import { partitionWhen } from "@thi.ng/transducers"; | ||
* | ||
* [...partitionWhen((x) => !!x, [0, 1, 0, 0, 1, 1, 0, 1])] | ||
* console.log( | ||
* [...partitionWhen((x) => !!x, [0, 1, 0, 0, 1, 1, 0, 1])] | ||
* ); | ||
* // [ [ 0 ], [ 1, 0, 0 ], [ 1 ], [ 1, 0 ], [ 1 ] ] | ||
@@ -15,0 +17,0 @@ * ``` |
@@ -10,12 +10,18 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* import { partition } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/partition.ts | ||
* import { partition, range } from "@thi.ng/transducers"; | ||
* | ||
* [...partition(3, range(10))] | ||
* console.log( | ||
* [...partition(3, range(10))] | ||
* ); | ||
* // [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ] ] | ||
* | ||
* [...partition(3, true, range(10))] | ||
* console.log( | ||
* [...partition(3, true, range(10))] | ||
* ); | ||
* // [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], [ 9 ] ] | ||
* | ||
* [...partition(3, 1, range(10))] | ||
* console.log( | ||
* [...partition(3, 1, range(10))] | ||
* ); | ||
* // [ [ 0, 1, 2 ], | ||
@@ -22,0 +28,0 @@ * // [ 1, 2, 3 ], |
import type { Transducer } from "./api.js"; | ||
/** | ||
* Transducer version of | ||
* [`peek()`](https://docs.thi.ng/umbrella/api/functions/peek.html), i.e. | ||
* [`peek()`](https://docs.thi.ng/umbrella/arrays/functions/peek.html), i.e. | ||
* extracts the last item of an array. | ||
* | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/peek.ts | ||
* import { peek } from "@thi.ng/transducers"; | ||
* | ||
* [...peek([ [1, 2, 3], [4, 5] ])] | ||
* console.log( | ||
* [...peek([ [1, 2, 3], [4, 5] ])] | ||
* ); | ||
* // [ 3, 5 ] | ||
@@ -13,0 +15,0 @@ * ``` |
@@ -9,13 +9,19 @@ /** | ||
* @example | ||
* ```ts | ||
* import { permutations } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/permutations.ts | ||
* import { map, permutations, range } from "@thi.ng/transducers"; | ||
* | ||
* [...permutations("ab", range(3))] | ||
* console.log( | ||
* [...permutations("ab", range(3))] | ||
* ); | ||
* // [ ['a', 0], ['a', 1], ['a', 2], | ||
* // ['b', 0], ['b', 1], ['b', 2] ] | ||
* | ||
* [...map((x: any[]) => x.join(""), permutations("ab", "-", range(3)))] | ||
* console.log( | ||
* [...map((x: any[]) => x.join(""), permutations("ab", "-", range(3)))] | ||
* ); | ||
* // ['a-0', 'a-1', 'a-2', 'b-0', 'b-1', 'b-2'] | ||
* | ||
* [...permutations([], "", range(0))] | ||
* console.log( | ||
* [...permutations([], "", range(0))] | ||
* ); | ||
* // [] | ||
@@ -41,9 +47,13 @@ * ``` | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/permutations-n.ts | ||
* import { permutationsN } from "@thi.ng/transducers"; | ||
* | ||
* [...permutationsN(2)] | ||
* console.log( | ||
* [...permutationsN(2)] | ||
* ); | ||
* // [ [0, 0], [0, 1], [1, 0], [1, 1] ] | ||
* | ||
* [...permutationsN(2, 3)] | ||
* console.log( | ||
* [...permutationsN(2, 3)] | ||
* ); | ||
* // [ [0, 0], [0, 1], [0, 2], | ||
@@ -53,3 +63,5 @@ * // [1, 0], [1, 1], [1, 2], | ||
* | ||
* [...permutationsN(2, 2, [10, 20])] | ||
* console.log( | ||
* [...permutationsN(2, 2, [10, 20])] | ||
* ); | ||
* // [ [ 10, 20 ], [ 10, 21 ], [ 11, 20 ], [ 11, 21 ] ] | ||
@@ -56,0 +68,0 @@ * ``` |
@@ -7,6 +7,8 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/pluck.ts | ||
* import { pluck } from "@thi.ng/transducers"; | ||
* | ||
* [...pluck("id", [{id: 1}, {id: 2}, {}])] | ||
* console.log( | ||
* [...pluck("id", [{id: 1}, {id: 2}, {}])] | ||
* ); | ||
* // [ 1, 2, undefined ] | ||
@@ -13,0 +15,0 @@ * ``` |
@@ -11,3 +11,3 @@ import type { Comparator, Maybe } from "@thi.ng/api"; | ||
export declare function pushSort<T>(cmp?: Comparator<T>): Reducer<T, T[]>; | ||
export declare function pushSort<T>(cmp: Maybe<Comparator<T>>, xs: Iterable<T>): T[]; | ||
export declare function pushSort<T>(cmp: Maybe<Comparator<T>>, src: Iterable<T>): T[]; | ||
//# sourceMappingURL=push-sort.d.ts.map |
import { compare } from "@thi.ng/compare/compare"; | ||
function pushSort(cmp = compare, xs) { | ||
return xs ? [...xs].sort(cmp) : [ | ||
function pushSort(cmp = compare, src) { | ||
return src ? [...src].sort(cmp) : [ | ||
() => [], | ||
@@ -5,0 +5,0 @@ (acc) => acc.sort(cmp), |
import type { Reducer } from "./api.js"; | ||
/** | ||
* Reducer which collects inputs into a new array. | ||
*/ | ||
export declare function push<T>(): Reducer<T, T[]>; | ||
export declare function push<T>(xs: Iterable<T>): T[]; | ||
export declare function push<T>(src: Iterable<T>): T[]; | ||
//# sourceMappingURL=push.d.ts.map |
import { reducer } from "./reduce.js"; | ||
function push(xs) { | ||
return xs ? [...xs] : reducer( | ||
function push(src) { | ||
return src ? [...src] : reducer( | ||
() => [], | ||
@@ -5,0 +5,0 @@ (acc, x) => (acc.push(x), acc) |
@@ -10,15 +10,23 @@ import type { ArrayLikeIterable } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/range-nd.ts | ||
* import { rangeNd } from "@thi.ng/transducers"; | ||
* | ||
* [...rangeNd([2])] | ||
* console.log( | ||
* [...rangeNd([2])] | ||
* ); | ||
* // [ [ 0 ], [ 1 ] ] | ||
* | ||
* [...rangeNd([2, -2])] | ||
* console.log( | ||
* [...rangeNd([2, -2])] | ||
* ); | ||
* // [ [ 0, 0 ], [ 0, -1 ], [ 1, 0 ], [ 1, -1 ] ] | ||
* | ||
* [...rangeNd([-1,2], [1,3])] | ||
* console.log( | ||
* [...rangeNd([-1,2], [1,3])] | ||
* ); | ||
* // [ [ -1, 2 ], [ -1, 3 ], [ 0, 2 ], [ 0, 3 ] ] | ||
* | ||
* [...rangeNd([2, 2, 2])] | ||
* console.log( | ||
* [...rangeNd([2, 2, 2])] | ||
* ); | ||
* // [ | ||
@@ -25,0 +33,0 @@ * // [ 0, 0, 0 ], |
@@ -14,6 +14,8 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/rechunk.ts | ||
* import { rechunk } from "@thi.ng/transducers"; | ||
* | ||
* [...rechunk(/-/, ["abc-d", "ef-g-", "hij", "-k-lm"])] | ||
* console.log( | ||
* [...rechunk(/-/, ["abc-d", "ef-g-", "hij", "-k-lm"])] | ||
* ); | ||
* // [ "abc", "def", "g", "hij", "k", "lm" ] | ||
@@ -23,5 +25,6 @@ * ``` | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/rechunk-2.ts | ||
* import { fromNodeJS, trace } from "@thi.ng/rstream"; | ||
* import { rechunk } from "@thi.ng/transducers"; | ||
* import { spawn } from "node:child_process" | ||
* import { fromNodeJS, trace } from "@thi.ng/rstream"; | ||
* | ||
@@ -49,4 +52,4 @@ * const cmd = spawn("ls", ["-la"]); | ||
export declare function rechunk(re?: RegExp): Transducer<string, string>; | ||
export declare function rechunk(xs: Iterable<string>): IterableIterator<string>; | ||
export declare function rechunk(re: RegExp, xs: Iterable<string>): IterableIterator<string>; | ||
export declare function rechunk(src: Iterable<string>): IterableIterator<string>; | ||
export declare function rechunk(re: RegExp, src: Iterable<string>): IterableIterator<string>; | ||
//# sourceMappingURL=rechunk.d.ts.map |
import type { Fn0, FnAny } from "@thi.ng/api"; | ||
import type { IReducible, Reducer, ReductionFn } from "./api.js"; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, xs: Iterable<A>): B; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, acc: B, xs: Iterable<A>): B; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, xs: IReducible<A, B>): B; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, acc: A, xs: IReducible<A, B>): B; | ||
export declare function reduceRight<A, B>(rfn: Reducer<A, B>, xs: ArrayLike<A>): B; | ||
export declare function reduceRight<A, B>(rfn: Reducer<A, B>, acc: B, xs: ArrayLike<A>): B; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, src: Iterable<A>): B; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, acc: B, src: Iterable<A>): B; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, src: IReducible<A, B>): B; | ||
export declare function reduce<A, B>(rfn: Reducer<A, B>, acc: A, src: IReducible<A, B>): B; | ||
export declare function reduceRight<A, B>(rfn: Reducer<A, B>, src: ArrayLike<A>): B; | ||
export declare function reduceRight<A, B>(rfn: Reducer<A, B>, acc: B, src: ArrayLike<A>): B; | ||
/** | ||
@@ -10,0 +10,0 @@ * Convenience helper for building a full {@link Reducer} using the identity |
@@ -7,3 +7,3 @@ import { identity } from "@thi.ng/api/fn"; | ||
import { isReduced, unreduced } from "./reduced.js"; | ||
const parseArgs = (args) => args.length === 2 ? [void 0, args[1]] : args.length === 3 ? [args[1], args[2]] : illegalArity(args.length); | ||
const __parseArgs = (args) => args.length === 2 ? [void 0, args[1]] : args.length === 3 ? [args[1], args[2]] : illegalArity(args.length); | ||
function reduce(...args) { | ||
@@ -14,8 +14,8 @@ const rfn = args[0]; | ||
const reduce2 = rfn[2]; | ||
args = parseArgs(args); | ||
args = __parseArgs(args); | ||
const acc = args[0] == null ? init() : args[0]; | ||
const xs = args[1]; | ||
const src = args[1]; | ||
return unreduced( | ||
complete( | ||
implementsFunction(xs, "$reduce") ? xs.$reduce(reduce2, acc) : isArrayLike(xs) ? reduceArray(reduce2, acc, xs) : reduceIterable(reduce2, acc, xs) | ||
implementsFunction(src, "$reduce") ? src.$reduce(reduce2, acc) : isArrayLike(src) ? __reduceArray(reduce2, acc, src) : __reduceIterable(reduce2, acc, src) | ||
) | ||
@@ -26,7 +26,7 @@ ); | ||
const [init, complete, reduce2] = args[0]; | ||
args = parseArgs(args); | ||
args = __parseArgs(args); | ||
let acc = args[0] == null ? init() : args[0]; | ||
const xs = args[1]; | ||
for (let i = xs.length; i-- > 0; ) { | ||
acc = reduce2(acc, xs[i]); | ||
const src = args[1]; | ||
for (let i = src.length; i-- > 0; ) { | ||
acc = reduce2(acc, src[i]); | ||
if (isReduced(acc)) { | ||
@@ -39,5 +39,5 @@ acc = acc.deref(); | ||
} | ||
const reduceArray = (rfn, acc, xs) => { | ||
for (let i = 0, n = xs.length; i < n; i++) { | ||
acc = rfn(acc, xs[i]); | ||
const __reduceArray = (rfn, acc, src) => { | ||
for (let i = 0, n = src.length; i < n; i++) { | ||
acc = rfn(acc, src[i]); | ||
if (isReduced(acc)) { | ||
@@ -50,4 +50,4 @@ acc = acc.deref(); | ||
}; | ||
const reduceIterable = (rfn, acc, xs) => { | ||
for (let x of xs) { | ||
const __reduceIterable = (rfn, acc, src) => { | ||
for (let x of src) { | ||
acc = rfn(acc, x); | ||
@@ -54,0 +54,0 @@ if (isReduced(acc)) { |
import type { Reducer } from "./api.js"; | ||
export declare function reductions<A, B>(rfn: Reducer<A, B>): Reducer<A, B[]>; | ||
export declare function reductions<A, B>(rfn: Reducer<A, B>, xs: Iterable<A>): B[]; | ||
export declare function reductions<A, B>(rfn: Reducer<A, B>, src: Iterable<A>): B[]; | ||
//# sourceMappingURL=reductions.d.ts.map |
import { reduce } from "./reduce.js"; | ||
import { isReduced, reduced } from "./reduced.js"; | ||
function reductions(rfn, xs) { | ||
function reductions(rfn, src) { | ||
const [init, complete, _reduce] = rfn; | ||
return xs ? reduce(reductions(rfn), xs) : [ | ||
return src ? reduce(reductions(rfn), src) : [ | ||
() => [init()], | ||
@@ -7,0 +7,0 @@ (acc) => (acc[acc.length - 1] = complete(acc[acc.length - 1]), acc), |
@@ -8,6 +8,8 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/repeat.ts | ||
* import { repeat } from "@thi.ng/transducers"; | ||
* | ||
* [...repeat(42, 5)] | ||
* console.log( | ||
* [...repeat(42, 5)] | ||
* ); | ||
* // [42, 42, 42, 42, 42] | ||
@@ -14,0 +16,0 @@ * ``` |
@@ -8,10 +8,14 @@ import type { Fn } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/repeatedly.ts | ||
* import { repeatedly } from "@thi.ng/transducers"; | ||
* | ||
* [...repeatedly(() => Math.floor(Math.random() * 10), 5)] | ||
* console.log( | ||
* [...repeatedly(() => Math.floor(Math.random() * 10), 5)] | ||
* ); | ||
* // [7, 0, 9, 3, 1] | ||
* | ||
* // same result as range(5) | ||
* [...repeatedly((i) => i, 5)] | ||
* console.log( | ||
* [...repeatedly((i) => i, 5)] | ||
* ); | ||
* // [0, 1, 2, 3, 4] | ||
@@ -18,0 +22,0 @@ * ``` |
@@ -8,6 +8,8 @@ import type { FnU2 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/repeatedly2d.ts | ||
* import { repeatedly2d } from "@thi.ng/transducers"; | ||
* | ||
* [...repeatedly2d((x, y) => [(x + 1) * 10, (y + 1) * 100], 2, 3)] | ||
* console.log( | ||
* [...repeatedly2d((x, y) => [(x + 1) * 10, (y + 1) * 100], 2, 3)] | ||
* ); | ||
* // [ | ||
@@ -14,0 +16,0 @@ * // [ 10, 100 ], [ 20, 100 ], |
@@ -8,6 +8,8 @@ import type { FnU3 } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/repeatedly3d.ts | ||
* import { repeatedly3d } from "@thi.ng/transducers"; | ||
* | ||
* [...repeatedly3d((x, y, z) => [(x+1)*10, (y+1)*100, (z+1)*1000], 2, 2, 2)] | ||
* console.log( | ||
* [...repeatedly3d((x, y, z) => [(x+1)*10, (y+1)*100, (z+1)*1000], 2, 2, 2)] | ||
* ); | ||
* // [ | ||
@@ -14,0 +16,0 @@ * // [ 10, 100, 1000 ], [ 20, 100, 1000 ], [ 10, 200, 1000 ], [ 20, 200, 1000 ], |
@@ -6,6 +6,8 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/reverse.ts | ||
* import { reverse } from "@thi.ng/transducers"; | ||
* | ||
* [...reverse("hello world")] | ||
* console.log( | ||
* [...reverse("hello world")] | ||
* ); | ||
* // [ "d", "l", "r", "o", "w", " ", "o", "l", "l", "e", "h" ] | ||
@@ -12,0 +14,0 @@ * ``` |
20
run.d.ts
import type { Fn } from "@thi.ng/api"; | ||
import type { IReducible, TxLike } from "./api.js"; | ||
/** | ||
* Transforms `xs` with given transducer and optional side effect | ||
* without any reduction step. If `fx` is given it will be called with | ||
* every value produced by the transducer. If `fx` is *not* given, the | ||
* transducer is assumed to include at least one {@link sideEffect} step | ||
* itself. Returns nothing. | ||
* Transforms `src` with given transducer `tx` and optional side-effect `fx` | ||
* without any reduction step. If `fx` is given it will be called with every | ||
* value produced by the transducer. If `fx` is *not* given, the transducer is | ||
* assumed to include at least one {@link sideEffect} step itself. Returns | ||
* nothing. | ||
* | ||
* @param tx - | ||
* @param xs - | ||
* @param src - | ||
*/ | ||
export declare function run<A>(tx: TxLike<A, any>, xs: Iterable<A>): void; | ||
export declare function run<A>(tx: TxLike<A, any>, xs: IReducible<A, any>): void; | ||
export declare function run<A, B>(tx: TxLike<A, B>, fx: Fn<B, void>, xs: Iterable<A>): void; | ||
export declare function run<A, B>(tx: TxLike<A, B>, fx: Fn<B, void>, xs: IReducible<A, any>): void; | ||
export declare function run<A>(tx: TxLike<A, any>, src: Iterable<A>): void; | ||
export declare function run<A>(tx: TxLike<A, any>, src: IReducible<A, any>): void; | ||
export declare function run<A, B>(tx: TxLike<A, B>, fx: Fn<B, void>, src: Iterable<A>): void; | ||
export declare function run<A, B>(tx: TxLike<A, B>, fx: Fn<B, void>, src: IReducible<A, any>): void; | ||
//# sourceMappingURL=run.d.ts.map |
@@ -9,7 +9,9 @@ import type { IRandom } from "@thi.ng/random"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/sample.ts | ||
* import { range, sample } from "@thi.ng/transducers"; | ||
* | ||
* // 10% probability | ||
* [...sample(0.1, range(100))] | ||
* console.log( | ||
* [...sample(0.1, range(100))] | ||
* ); | ||
* // [ 3, 24, 25, 36, 43, 49, 59, 64, 82, 86, 89 ] | ||
@@ -16,0 +18,0 @@ * ``` |
@@ -14,29 +14,33 @@ import type { Reducer, Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* import { | ||
* add, comp, last, length, multiplex, range, scan, transduce | ||
* } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/scan.ts | ||
* import * as tx from "@thi.ng/transducers"; | ||
* | ||
* [...iterator(scan(add()), range(10))] | ||
* console.log( | ||
* [...tx.iterator(tx.scan(tx.add()), tx.range(10))] | ||
* ); | ||
* // [ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45 ] | ||
* | ||
* // directly as iterator and with initial result | ||
* [...scan(add(), 100, range(10))] | ||
* console.log( | ||
* [...tx.scan(tx.add(), 100, tx.range(10))] | ||
* ); | ||
* // [ 100, 101, 103, 106, 110, 115, 121, 128, 136, 145 ] | ||
* | ||
* // as transducer | ||
* transduce( | ||
* const res = tx.transduce( | ||
* // parallel processing lanes for each input | ||
* multiplex( | ||
* // join strings | ||
* scan(str(" ")), | ||
* // compute total length (+1) | ||
* comp(length(1), scan(add())) | ||
* tx.multiplex( | ||
* // first lane: join strings | ||
* tx.scan(tx.str(" ")), | ||
* // second lane: compute total length (+1) | ||
* tx.comp(tx.length(1), tx.scan(tx.add())) | ||
* ), | ||
* // only keep final value | ||
* last(), | ||
* // use last() reducer to only keep final value | ||
* tx.last(), | ||
* // inputs | ||
* ["alpha", "beta", "gamma", "delta"] | ||
* ) | ||
* // [ 'alpha beta gamma delta', 23 ] | ||
* ); | ||
* | ||
* console.log(res); | ||
* // [ 'alpha beta gamma delta', 123 ] | ||
* ``` | ||
@@ -43,0 +47,0 @@ * |
@@ -11,6 +11,6 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/select-keys.ts | ||
* import { selectKeys } from "@thi.ng/transducers"; | ||
* | ||
* [...selectKeys( | ||
* const res = [...selectKeys( | ||
* ["id", "age"], | ||
@@ -22,3 +22,5 @@ * [ | ||
* ] | ||
* )] | ||
* )]; | ||
* | ||
* console.log(res); | ||
* // [ { age: 23, id: 1 }, { age: 42, id: 2 }, { id: 3 } ] | ||
@@ -25,0 +27,0 @@ * ``` |
@@ -14,10 +14,16 @@ import { type MaybeDeref } from "@thi.ng/api/deref"; | ||
* | ||
* Also see {@link partition}. | ||
* | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/sliding-window.ts | ||
* import { range, slidingWindow } from "@thi.ng/transducers"; | ||
* | ||
* [...slidingWindow(3, range(5))] | ||
* console.log( | ||
* [...slidingWindow(3, range(5))] | ||
* ); | ||
* // [ [ 0 ], [ 0, 1 ], [ 0, 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ] ] | ||
* | ||
* [...slidingWindow(3, false, range(5))] | ||
* console.log( | ||
* [...slidingWindow(3, false, range(5))] | ||
* ); | ||
* // [ [ 0, 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ] ] | ||
@@ -24,0 +30,0 @@ * ``` |
@@ -11,4 +11,4 @@ import type { Predicate } from "@thi.ng/api"; | ||
export declare function some<T>(pred?: Predicate<T>): Reducer<T, boolean>; | ||
export declare function some<T>(xs: Iterable<T>): boolean; | ||
export declare function some<T>(pred: Predicate<T>, xs: Iterable<T>): boolean; | ||
export declare function some<T>(src: Iterable<T>): boolean; | ||
export declare function some<T>(pred: Predicate<T>, src: Iterable<T>): boolean; | ||
//# sourceMappingURL=some.d.ts.map |
@@ -14,37 +14,52 @@ import type { Fn, Maybe } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* import { filter, map, macat, step, take } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/step.ts | ||
* import { filter, map, mapcat, step, take } from "@thi.ng/transducers"; | ||
* | ||
* // single result (unwrapped, default) | ||
* step(map(x => x * 10))(1); | ||
* console.log( | ||
* step(map((x: number) => x * 10))(1) | ||
* ); | ||
* // 10 | ||
* | ||
* // single result (no unwrapping) | ||
* step(map(x => x * 10), false)(1); | ||
* console.log( | ||
* step(map((x: number) => x * 10), false)(1) | ||
* ); | ||
* // [10] | ||
* | ||
* // multiple results | ||
* step(mapcat(x => [x, x + 1, x + 2]))(1) | ||
* console.log( | ||
* step(mapcat((x: number) => [x, x + 1, x + 2]))(1) | ||
* ); | ||
* // [ 1, 2, 3 ] | ||
* | ||
* // multiple results (default behavior) | ||
* step(mapcat(x => x))([1, 2]) | ||
* console.log( | ||
* step(mapcat((x: number[]) => take(2, x)))([1, 2, 3, 4]) | ||
* ); | ||
* // [1, 2] | ||
* step(mapcat(x => x))([3]) | ||
* | ||
* console.log( | ||
* step(mapcat((x: number[]) => x))([3]) | ||
* ); | ||
* // 3 | ||
* | ||
* // ...once more without unwrapping | ||
* step(mapcat(x => x), false)([3]) | ||
* console.log( | ||
* step(mapcat((x: number[]) => x), false)([3]) | ||
* ); | ||
* // [3] | ||
* | ||
* // no result | ||
* f = step(filter((x) => !(x & 1))) | ||
* f(1); // undefined | ||
* f(2); // 2 | ||
* // filter even values | ||
* const f = step(filter((x: number) => !(x & 1))) | ||
* | ||
* console.log(f(1)); // undefined | ||
* console.log(f(2)); // 2 | ||
* | ||
* // reduced value termination | ||
* f = step(take(2)); | ||
* f(1); // 1 | ||
* f(1); // 1 | ||
* f(1); // undefined | ||
* f(1); // undefined | ||
* const g = step(take(2)); | ||
* console.log(g(1)); // 1 | ||
* console.log(g(1)); // 1 | ||
* console.log(g(1)); // undefined | ||
* console.log(g(1)); // undefined | ||
* ``` | ||
@@ -51,0 +66,0 @@ * |
import type { Reducer } from "./api.js"; | ||
/** | ||
* Reducer which concatenates inputs into a string, each value separated by | ||
* `sep` (default: ""). | ||
* | ||
* @param sep | ||
*/ | ||
export declare function str(sep?: string): Reducer<any, string>; | ||
export declare function str(sep: string, xs: Iterable<any>): string; | ||
export declare function str(sep: string, src: Iterable<any>): string; | ||
//# sourceMappingURL=str.d.ts.map |
import { reducer } from "./reduce.js"; | ||
function str(sep, xs) { | ||
function str(sep, src) { | ||
sep = sep || ""; | ||
let first = true; | ||
return xs ? [...xs].join(sep) : reducer( | ||
return src ? [...src].join(sep) : reducer( | ||
() => "", | ||
@@ -7,0 +7,0 @@ (acc, x) => (acc = first ? acc + x : acc + sep + x, first = false, acc) |
@@ -29,10 +29,14 @@ import type { IRandom } from "@thi.ng/random"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/stream-shuffle.ts | ||
* import { range, streamShuffle } from "@thi.ng/transducers"; | ||
* import { XsAdd } from "@thi.ng/random"; | ||
* | ||
* [...streamShuffle(5, range(10))] | ||
* console.log( | ||
* [...streamShuffle(5, range(10))] | ||
* ); | ||
* // [ 3, 2, 5, 0, 8, 7, 1, 6, 4, 9 ] | ||
* | ||
* [...streamShuffle({ n: 5, rnd: new XsAdd(12345) }, range(10))] | ||
* console.log( | ||
* [...streamShuffle({ n: 5, rnd: new XsAdd(12345) }, range(10))] | ||
* ); | ||
* [ 0, 4, 3, 7, 8, 1, 5, 2, 6, 9 ] | ||
@@ -39,0 +43,0 @@ * ``` |
import type { SortOpts, Transducer } from "./api.js"; | ||
/** | ||
* Transducer. Similar to {@link partitionSort}, however uses proper sliding | ||
* window and insertion sort instead of fully sorting window as done by | ||
* Transducer. Similar to {@link partitionSort}, however uses a sliding window | ||
* of size `n` and insertion sort instead of fully sorting window as done by | ||
* `partitionSort`. | ||
* | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/stream-sort.ts | ||
* import { streamSort } from "@thi.ng/transducers"; | ||
* | ||
* [...streamSort(4, [5,9,2,6,4,1,3,8,7,0])] | ||
* console.log( | ||
* [...streamSort(4, [5,9,2,6,4,1,3,8,7,0])] | ||
* ); | ||
* // [ 2, 4, 1, 3, 5, 6, 0, 7, 8, 9 ] | ||
@@ -13,0 +15,0 @@ * ``` |
@@ -21,18 +21,25 @@ import type { Fn } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/struct.ts | ||
* import { push, struct, transduce } from "@thi.ng/transducers"; | ||
* | ||
* transduce( | ||
* const res = transduce( | ||
* struct([["id", 1, (id) => id[0]], ["pos", 2], ["vel", 2], ["color", 4]]), | ||
* push(), | ||
* [0, 100, 200, -1, 0, 1, 0.5, 0, 1, 1, 0, 0, 5, 4, 0, 0, 1, 1] | ||
* ) | ||
* // [ { color: [ 1, 0.5, 0, 1 ], | ||
* ); | ||
* | ||
* console.log(res); | ||
* // [ | ||
* // { | ||
* // color: [ 1, 0.5, 0, 1 ], | ||
* // vel: [ -1, 0 ], | ||
* // pos: [ 100, 200 ], | ||
* // id: 0 }, | ||
* // { color: [ 0, 0, 1, 1 ], | ||
* // id: 0, | ||
* // }, { | ||
* // color: [ 0, 0, 1, 1 ], | ||
* // vel: [ 5, 4 ], | ||
* // pos: [ 0, 0 ], | ||
* // id: 1 } ] | ||
* // id: 1, | ||
* // } | ||
* // ] | ||
* ``` | ||
@@ -39,0 +46,0 @@ * |
@@ -7,4 +7,4 @@ import type { Reducer } from "./api.js"; | ||
export declare function sub(init?: number): Reducer<number, number>; | ||
export declare function sub(xs: Iterable<number>): number; | ||
export declare function sub(init: number, xs: Iterable<number>): number; | ||
export declare function sub(src: Iterable<number>): number; | ||
export declare function sub(init: number, src: Iterable<number>): number; | ||
//# sourceMappingURL=sub.d.ts.map |
@@ -1,4 +0,4 @@ | ||
import { __mathop } from "./internal/mathop.js"; | ||
import { __mathOp } from "./internal/mathop.js"; | ||
function sub(...args) { | ||
return __mathop(sub, (acc, x) => acc - x, 0, args); | ||
return __mathOp(sub, (acc, x) => acc - x, 0, args); | ||
} | ||
@@ -5,0 +5,0 @@ export { |
@@ -7,12 +7,18 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/swizzle.ts | ||
* import { swizzle } from "@thi.ng/transducers"; | ||
* | ||
* [...swizzle([3, 0, 2, 1], [[1, 2, 3, 4], [10, 20, 30, 40]])] | ||
* console.log( | ||
* [...swizzle([3, 0, 2, 1], [[1, 2, 3, 4], [10, 20, 30, 40]])] | ||
* ); | ||
* // [ [ 4, 1, 3, 2 ], [ 40, 10, 30, 20 ] ] | ||
* | ||
* [...swizzle([0, 0, 1, 1], [[1, 2, 3, 4], [10, 20, 30, 40]])] | ||
* console.log( | ||
* [...swizzle([0, 0, 1, 1], [[1, 2, 3, 4], [10, 20, 30, 40]])] | ||
* ); | ||
* // [ [ 1, 1, 2, 2 ], [ 10, 10, 20, 20 ] ] | ||
* | ||
* [...swizzle(["z", "x"], [{x: 1, y: 2, z: 3}])] | ||
* console.log( | ||
* [...swizzle(["z", "x"], [{x: 1, y: 2, z: 3}])] | ||
* ); | ||
* // [ [ 3, 1 ] ] | ||
@@ -19,0 +25,0 @@ * ``` |
/** | ||
* Yields an iterator of all `src` values, followed by the same values in | ||
* reverse order. Efficiently builds the reversed order via an internal linked | ||
* list. | ||
* list. The input MUST be finite! | ||
* | ||
* @remarks | ||
* Also see {@link palindrome}. | ||
* | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/symmetric.ts | ||
* import { symmetric } from "@thi.ng/transducers"; | ||
* | ||
* [...symmetric([1, 2, 3])] | ||
* console.log( | ||
* [...symmetric("abc")] | ||
* ); | ||
* // [ "a", "b", "c", "c", "b", "a" ] | ||
* | ||
* console.log( | ||
* [...symmetric([1, 2, 3])] | ||
* ); | ||
* // [ 1, 2, 3, 3, 2, 1 ] | ||
@@ -12,0 +22,0 @@ * ``` |
@@ -7,6 +7,8 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/take-last.ts | ||
* import { range, takeLast } from "@thi.ng/transducers"; | ||
* | ||
* [...takeLast(3, range(10))] | ||
* console.log( | ||
* [...takeLast(3, range(10))] | ||
* ); | ||
* // [ 7, 8, 9 ] | ||
@@ -13,0 +15,0 @@ * ``` |
@@ -7,6 +7,8 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/take-nth.ts | ||
* import { range, takeNth } from "@thi.ng/transducers"; | ||
* | ||
* [...takeNth(3, range(10))] | ||
* console.log( | ||
* [...takeNth(3, range(10))] | ||
* ); | ||
* // [ 0, 3, 6, 9 ] | ||
@@ -13,0 +15,0 @@ * ``` |
@@ -10,6 +10,8 @@ import type { Predicate } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/take-while.ts | ||
* import { range, takeWhile } from "@thi.ng/transducers"; | ||
* | ||
* [...takeWhile((x) => x < 5, range(10))] | ||
* console.log( | ||
* [...takeWhile((x) => x < 5, range())] | ||
* ); | ||
* // [ 0, 1, 2, 3, 4 ] | ||
@@ -16,0 +18,0 @@ * ``` |
@@ -7,6 +7,11 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* import { comp, map, range, take } from "@thi.ng/transducers"; | ||
* ```ts tangle:../export/take.ts | ||
* import { comp, iterator, map, range, take } from "@thi.ng/transducers"; | ||
* | ||
* [...iterator(comp(take(5), map((x) => x * 10)), range(10))] | ||
* // pre-compose transducer which only takes first N items and then | ||
* // transforms only those via map()... | ||
* // apply to infinite range() counter | ||
* console.log( | ||
* [...iterator(comp(take(5), map((x) => x * 10)), range())] | ||
* ); | ||
* // [ 0, 10, 20, 30, 40 ] | ||
@@ -13,0 +18,0 @@ * ``` |
@@ -9,9 +9,13 @@ import type { Transducer } from "./api.js"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/toggle.ts | ||
* import { toggle } from "@thi.ng/transducers"; | ||
* | ||
* [...toggle(1, 0, false, [1, 2, 3, 4])] | ||
* console.log( | ||
* [...toggle(1, 0, false, [1, 2, 3, 4])] | ||
* ); | ||
* // [ 1, 0, 1, 0 ] | ||
* | ||
* [...toggle("on", "off", true, [1, 2, 3, 4])] | ||
* console.log( | ||
* [...toggle("on", "off", true, [1, 2, 3, 4])] | ||
* ); | ||
* // [ 'off', 'on', 'off', 'on' ] | ||
@@ -18,0 +22,0 @@ * ``` |
import type { IReducible, Reducer, Transducer, TxLike } from "./api.js"; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>): Transducer<Iterable<A>, C>; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, xs: Iterable<A>): C; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, xs: IReducible<A, C>): C; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, acc: C, xs: Iterable<A>): C; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<C, B>, acc: C, xs: IReducible<A, C>): C; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, src: Iterable<A>): C; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, src: IReducible<A, C>): C; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, acc: C, src: Iterable<A>): C; | ||
export declare function transduce<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<C, B>, acc: C, src: IReducible<A, C>): C; | ||
export declare function transduceRight<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>): Transducer<ArrayLike<A>, C>; | ||
export declare function transduceRight<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, xs: ArrayLike<A>): C; | ||
export declare function transduceRight<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, acc: C, xs: ArrayLike<A>): C; | ||
export declare function transduceRight<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, src: ArrayLike<A>): C; | ||
export declare function transduceRight<A, B, C>(tx: TxLike<A, B>, rfn: Reducer<B, C>, acc: C, src: ArrayLike<A>): C; | ||
//# sourceMappingURL=transduce.d.ts.map |
@@ -12,10 +12,10 @@ import { illegalArity } from "@thi.ng/errors/illegal-arity"; | ||
const $transduce = (tfn, rfn, args) => { | ||
let acc, xs; | ||
let acc, src; | ||
switch (args.length) { | ||
case 4: | ||
xs = args[3]; | ||
src = args[3]; | ||
acc = args[2]; | ||
break; | ||
case 3: | ||
xs = args[2]; | ||
src = args[2]; | ||
break; | ||
@@ -27,3 +27,3 @@ case 2: | ||
} | ||
return rfn(ensureTransducer(args[0])(args[1]), acc, xs); | ||
return rfn(ensureTransducer(args[0])(args[1]), acc, src); | ||
}; | ||
@@ -30,0 +30,0 @@ export { |
@@ -62,13 +62,15 @@ import type { Fn2, FnN } from "@thi.ng/api"; | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/tween.ts | ||
* import { tween } from "@thi.ng/transducers"; | ||
* | ||
* [...tween({ | ||
* num: 10, | ||
* min: 0, | ||
* max: 100, | ||
* init: (a, b) => [a, b], | ||
* mix: ([a, b], t) => Math.floor(a + (b - a) * t), | ||
* stops: [[20, 100], [50, 200], [80, 0]] | ||
* })] | ||
* console.log( | ||
* [...tween({ | ||
* num: 10, | ||
* min: 0, | ||
* max: 100, | ||
* init: (a, b) => [a, b], | ||
* mix: ([a, b], t) => Math.floor(a + (b - a) * t), | ||
* stops: [[20, 100], [50, 200], [80, 0]] | ||
* })] | ||
* ); | ||
* // [ 100, 100, 100, 133, 166, 200, 133, 66, 0, 0, 0 ] | ||
@@ -81,14 +83,16 @@ * ``` | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/tween-2.ts | ||
* import { tween } from "@thi.ng/transducers"; | ||
* import { mix, smoothStep } from "@thi.ng/math" | ||
* | ||
* [...tween({ | ||
* num: 10, | ||
* min: 0, | ||
* max: 100, | ||
* init: (a, b) => [a, b], | ||
* mix: ([a, b], t) => Math.floor(mix(a, b, smoothStep(0.1, 0.9, t))), | ||
* stops: [[20, 100], [50, 200], [80, 0]] | ||
* })] | ||
* console.log( | ||
* [...tween({ | ||
* num: 10, | ||
* min: 0, | ||
* max: 100, | ||
* init: (a, b) => [a, b], | ||
* mix: ([a, b], t) => Math.floor(mix(a, b, smoothStep(0.1, 0.9, t))), | ||
* stops: [[20, 100], [50, 200], [80, 0]] | ||
* })] | ||
* ); | ||
* // [ 100, 100, 100, 120, 179, 200, 158, 41, 0, 0, 0 ] | ||
@@ -95,0 +99,0 @@ * ``` |
14
zip.d.ts
@@ -16,10 +16,14 @@ /** | ||
* @example | ||
* ```ts | ||
* ```ts tangle:../export/zip.ts | ||
* import { zip } from "@thi.ng/transducers"; | ||
* | ||
* zip([1, 2, 3], [3, 4, 5, 0, 9]) | ||
* // [ 1, 3 ] [ 2, 4 ] [ 3, 5 ] | ||
* console.log( | ||
* [...zip([1, 2, 3], [3, 4, 5, 0, 9])] | ||
* ); | ||
* // [ [ 1, 3 ] [ 2, 4 ] [ 3, 5 ] ] | ||
* | ||
* zip([1, 2, 3]) | ||
* // [ 1 ] [ 2 ] [ 3 ] | ||
* console.log( | ||
* [...zip([1, 2, 3])] | ||
* ); | ||
* // [ [ 1 ] [ 2 ] [ 3 ] ] | ||
* ``` | ||
@@ -26,0 +30,0 @@ */ |
Sorry, the diff of this file is too big to display
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
358482
6995
1096
Updated@thi.ng/api@^8.11.3
Updated@thi.ng/arrays@^2.9.7
Updated@thi.ng/checks@^3.6.5
Updated@thi.ng/compare@^2.3.6
Updated@thi.ng/compose@^3.0.5
Updated@thi.ng/errors@^2.5.8
Updated@thi.ng/math@^5.11.0
Updated@thi.ng/random@^3.8.1