Skip to content

Commit

Permalink
fix(transducers): update mean() completion step to avoid div by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 20, 2019
1 parent 1330df6 commit f644ecd
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/transducers/src/rfn/mean.ts
@@ -1,11 +1,19 @@
import { Reducer } from "../api";
import { reduce } from "../reduce";

/**
* Reducer computing mean of received inputs. Returns 0 if no inputs
* were processed.
*/
export function mean(): Reducer<number, number>;
export function mean(xs: Iterable<number>): number;
export function mean(xs?: Iterable<number>): any {
let n = 0;
return xs
? reduce(mean(), xs)
: [() => 0, (acc) => acc / n, (acc, x) => (n++, acc + x)];
: [
() => 0,
(acc) => (n > 1 ? acc / n : acc),
(acc, x) => (n++, acc + x)
];
}

0 comments on commit f644ecd

Please sign in to comment.