I am just getting started with highland.js and streams in node and I am stuck trying to calculate the min/max/mean of some numbers. This is what I have so far:
const _ = require('highland');
const input = [
{ val: 1 },
{ val: 2 },
{ val: 3 },
];
_(input)
.reduce((acc, { val }) => {
if (typeof acc.min === 'undefined' || val < acc.min) {
acc.min = val;
}
if (typeof acc.max === 'undefined' || val > acc.max) {
acc.max = val;
}
acc.count = (acc.count || 0) + 1;
acc.sum = (acc.sum || 0) + val;
return acc;
}, {});
If I then do, say, toCallback and console.log the result I get
{min: 1, max: 3, count: 3, sum: 6} but I am not interested in the count and sum fields, I want the object like {min: 1, max: 3, mean: 2}.
However, since the return of the reduce is an object, there's nothing highland can do with it - I can only consume it but I would like to do the average in highland land.
How can I continue from here or how should I refactor the code to get that average?
You could try using highland .map method like this: