3 ms·
Hmm… not sure how that particular iterator version is an improvement. Aside: the Python inspectors = list(name for name in ','.split(line) for line in file_
by minitech 7y ago
Hmm… not sure how that particular iterator version is an improvement.
Aside: the Python
inspectors = list(name for name in ','.split(line) for line in file_lines)
is backwards in two ways and should be
inspectors = [name for line in file_lines for name in line.split(',')]
- jacobolus 7y ago> not sure how that particular iterator version is an improvement In this case it is not. But if you want to do more complicated things, you can do several steps with iterators, and then crystallize an array (or sometimes a single reduced result) at the end. * * * Edit: whoops I had it correct it in my python shell and then mindlessly typed it wrong into this conversation. Apologies! * * * Also yes sure you should use a list comprehension at the end in the Python, but again if you use generator expressions you can keep working on them for several lines of streaming transformations without needing to actually construct any full list along the way. * * * As an example, instead of TFA’s min/max example, we can do something like: const readings = [0.3, 1.2, 3.4, 0.2, 3.2, 5.5, 0.4], [r0, r1] = tee(readings, 2), running_min = accumulate(r0, Math.min), running_max = accumulate(r1, Math.max), [rmin, rmax] = last(zip(running_min, running_max));
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- sametmax 7y agoYeah but in python you would just use a generator for that.
- jacobolus 7y agoIn 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.