Hacker News new | ask | show | jobs
by moosingin3space 3245 days ago
The problem with this is that it generates two intermediate arrays in the maps, then effectively drops them in the reduce. This may be inefficient.

I wish that JavaScript's map/filter/reduce functions returned lazy iterators, like in Rust, so that code like this doesn't produce intermediate arrays. Does anyone know of a library that provides this?

1 comments

Those intermediate iterations could be dropped by composing the map functions together (same with the reduce), if that really was a performance bottleneck.
With lazy iterators, there is the possibility of better composition of maps/filters/reduces. That's why I'm a fan of the lazy iterators approach -- otherwise the abstraction breaks too easily and it ends up looking like a for-loop anyway.