2 ms·
In python you could use just a regular function with a for loop. You can do that in javascript too. But the versions using smaller components can be faster to w
by jacobolus 7y ago
In python you could use just a regular function with a for loop. You can do that in javascript too. But the versions using smaller components can be faster to write and more composable. Really depends what kind of style you like and what you are trying to do..
const min_max = function min_max(iterable) {
const it = iterable[Symbol.iterator]();
const {value, done} = it.next(); if (done) return;
let min = value, max = value;
for (let x of it) {
min = Math.min(min, x);
min = Math.max(max, x);
}
return [min, max];
}
min_max(readings);
Anywhere that would be appropriate for a generator in python can also be a generator in Javascript though.
- sametmax 7y agoGenerator expressions exist for that in python.