improve average operator
This commit is contained in:
parent
2badf46e4b
commit
4a553e3cbb
|
@ -21,15 +21,26 @@ of({a: 1, b:2, e: 1}, {a: 0, c: 1, b:3}, {a: 0, d: 1, b:1})
|
||||||
.subscribe((v) => console.log(v));
|
.subscribe((v) => console.log(v));
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function $average() {
|
function $average(cb) {
|
||||||
return pipe(
|
return pipe(
|
||||||
reduce((accum, curr) => ({
|
reduce((accum, curr) => {
|
||||||
sum: accum.sum + curr,
|
let currentValue;
|
||||||
count: accum.count + 1
|
if (typeof cb === 'string' || cb instanceof String){
|
||||||
}), { sum: 0, count: 0 }),
|
currentValue = curr[cb];
|
||||||
map(o => o.sum / o.count)
|
} else if(typeof cb === "function") {
|
||||||
);
|
currentValue = cb(curr);
|
||||||
}
|
} else {
|
||||||
|
currentValue = curr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
sum: accum.sum + currentValue,
|
||||||
|
count: accum.count + 1
|
||||||
|
}
|
||||||
|
}, { sum: 0, count: 0 }),
|
||||||
|
map(o => o.sum / o.count)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
of(10, 3, 4)
|
of(10, 3, 4)
|
||||||
|
|
Loading…
Reference in New Issue