Skip to content

Commit

Permalink
fix(transducers): update converge() & update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 26, 2019
1 parent 022c1ad commit 9aca912
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/transducers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 9 additions & 21 deletions packages/transducers/src/xform/converge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,7 +32,6 @@ import { ensureReduced } from "../reduced";
* @param src
*/
export function converge<T>(pred?: Predicate2<T>): Transducer<T, T>;
export function converge<T>(src: Iterable<T>): IterableIterator<T>;
export function converge<T>(pred: Predicate2<T>, src: Iterable<T>): IterableIterator<T>;
export function converge<T>(...args: any[]): any {
return $iter(converge, args) ||
Expand All @@ -56,7 +44,7 @@ export function converge<T>(...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);
Expand Down

0 comments on commit 9aca912

Please sign in to comment.