Skip to content(if available)orjump to list(if available)

A visual demo of Ruby's lazy enumerator

hakunin

Lazy enumeration can also save memory, because you aren’t storing entire collections during intermediate steps, and it works with infinite/unknown size collections. Such as streaming data.

Some examples:

I wrote a utility gem a while ago that lets you lazily intersect, union, etc various potentially infinite streams of data. https://github.com/maxim/enum_utils/

I also used lazy enumeration for traversing the wordmap in my no-RAM static storage gem. https://github.com/maxim/wordmap/

endorphine

I was expecting a visual comparison towards the end of the article, where you would be able to click a button and both the eager and lazy versions would start executing simultaneously, one displayed next to the other, and you would clearly see that the lazy one completed earlier. This would make it even more obvious how the lazy one is faster.

Nevertheless, this was great.

thih9

Related discussion from yesterday:

https://news.ycombinator.com/item?id=42652775 (different submitter; 29 points, 3 comments)

Syntaf

Really cool visualization and neat to learn about lazy enumeration!

Excuse me while I go back through my code and make sure I’m using lazy enumeration wherever I’m iterating over large collections….

Lio

That's lovely and makes it very obvious what's happening.