From 9aca91228a79cf9a22fc245b8deb1ba241cbf103 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Tue, 26 Feb 2019 04:43:48 +0000 Subject: [PATCH] fix(transducers): update converge() & update readme --- packages/transducers/README.md | 1 + packages/transducers/src/xform/converge.ts | 30 +++++++--------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/packages/transducers/README.md b/packages/transducers/README.md index 190446be2b..d263ef31ce 100644 --- a/packages/transducers/README.md +++ b/packages/transducers/README.md @@ -642,6 +642,7 @@ tx.transduce(tx.map((x) => x*10), tx.push(), tx.range(4)) - [benchmark](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/benchmark.ts) - [cat](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/cat.ts) +- [converge](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/converge.ts) - [convolve2d](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/convolve.ts) - [dedupe](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/dedupe.ts) - [delayed](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/delayed.ts) diff --git a/packages/transducers/src/xform/converge.ts b/packages/transducers/src/xform/converge.ts index c21b4cba93..31d02debda 100644 --- a/packages/transducers/src/xform/converge.ts +++ b/packages/transducers/src/xform/converge.ts @@ -6,35 +6,24 @@ import { ensureReduced } from "../reduced"; /** * Transducer which for each input `x` (apart from the very first one) - * applies given predicate `pred` to previous input and `x` and only - * passed values downstream as long as the predicate returns falsy - * result. Once the result is truthy, transformation is terminated (by - * emitting a `reduced()` value). + * applies given predicate `pred` to previous input and `x`. Only passes + * values downstream as long as the predicate returns a falsy result. + * Once the result is truthy, `x` is considered converged and the + * transformation is terminated (by emitting a `reduced()` value). * * This can be used to limit processing of inputs only as long as * there're noticeable changes (according to the predicate) and then * stop the transducer pipeline once results have converged. * * ``` - * // only take new values as long as difference to prev value is >= 0.1 + * // process as long as difference to prev value is >= 0.01 * [...converge( * // predicate - * (a, b) => Math.abs(a - b) < 0.1, + * (a, b) => Math.abs(a - b) < 0.01, * // input sequence - * iterate((x, i) => x + 1 / i, 0) + * iterate((x, i) => x + Math.pow(2, -i), 0) * )] - * - * // [ 0, - * // 1, - * // 1.5, - * // 1.8333333333333333, - * // 2.083333333333333, - * // 2.283333333333333, - * // 2.4499999999999997, - * // 2.5928571428571425, - * // 2.7178571428571425, - * // 2.8289682539682537, - * // 2.9289682539682538 ] + * // [ 0, 0.5, 0.75, 0.875, 0.9375, 0.96875, 0.984375, 0.9921875 ] * ``` * * @see takeWhile @@ -43,7 +32,6 @@ import { ensureReduced } from "../reduced"; * @param src */ export function converge(pred?: Predicate2): Transducer; -export function converge(src: Iterable): IterableIterator; export function converge(pred: Predicate2, src: Iterable): IterableIterator; export function converge(...args: any[]): any { return $iter(converge, args) || @@ -56,7 +44,7 @@ export function converge(...args: any[]): any { (acc, x: T) => { if (done || (prev !== SEMAPHORE && pred(prev, x))) { done = true; - return ensureReduced(acc); + return ensureReduced(r(acc, x)); } prev = x; return r(acc, x);