Re:How to use array reduce for more than just numbers
by mikeryan on Sunday June 02, 2019 @10:52AM
(#20075802)
While your example reads a bit better, you’re now iterating through your list twice.
Re:How to use array reduce for more than just numbers
by fwip on Sunday June 02, 2019 @11:11AM
(#20075947)
Most code isn't performance-critical.
This pattern will probably be dominated by the time taken to filter and map, not loop iteration.
Re:How to use array reduce for more than just numbers
by saagarjha on Sunday June 02, 2019 @11:50AM
(#20076164)
Does JavaScript have adapters to covert lists into generators or lazy iterators?
Re:How to use array reduce for more than just numbers
by hn_throwaway_99 on Sunday June 02, 2019 @12:43PM
(#20076496)
Unfortunately not in a way that can optimize these methods. This is one reason the Java Streams API, which has a similar purpose (e.g. map, filter, flatMap, reduce, etc.), is a better design IMO because you can chain together method calls without creating (and iterating) the full array at each step.
Re:How to use array reduce for more than just numbers
by londt8 on Sunday June 02, 2019 @11:12AM
(#20075958)
Technically there is no iteration in filter/map/reduce at all.
Re:How to use array reduce for more than just numbers
by Stratoscope on Sunday June 02, 2019 @12:28PM
(#20076401)
That's incorrect. All of those functions are implemented using iteration.
Consider this excerpt from the .map() polyfill on MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
// 7. Let k be 0
k = 0;
// 8. Repeat, while k < len
while (k < len) {
// [loop body omitted]
// d. Increase k by 1.
k++;
}
Otherwise I enjoyed reading this.