TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A visual demo of Ruby's lazy enumerator

125 pointsby rossta4 months ago

10 comments

afraca4 months ago
When I learned Haskell in college I was blown away by how laziness enables cool things like dealing with infinite lists or more performance even though the UX is exactly the same. (Apparently with Ruby there is the slight hint of adding the lazy method in between)<p>Later I found out laziness in the whole system by default leads to some difficult issues, which quite a few people seem to agree with. Simon Peyton Jones (Haskell co-creator) apparently has said &quot;The next Haskell will be strict&quot;. (<a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14011943">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14011943</a>)
评论 #42669723 未加载
评论 #42674173 未加载
adsteel_4 months ago
Hm, the CSS and JS don&#x27;t appear to load for me. Not even a &lt;body&gt;&lt;html&gt; set of tags in the HTML response.
评论 #42669247 未加载
Syntaf4 months ago
Really cool visualization and neat to learn about lazy enumeration!<p>Excuse me while I go back through my code and make sure I’m using lazy enumeration wherever I’m iterating over large collections….
评论 #42668434 未加载
hakunin4 months ago
Lazy enumeration can also save memory, because you aren’t storing entire collections during intermediate steps, and it works with infinite&#x2F;unknown size collections. Such as streaming data.<p>Some examples:<p>I wrote a utility gem a while ago that lets you lazily intersect, union, etc various potentially infinite streams of data. <a href="https:&#x2F;&#x2F;github.com&#x2F;maxim&#x2F;enum_utils&#x2F;">https:&#x2F;&#x2F;github.com&#x2F;maxim&#x2F;enum_utils&#x2F;</a><p>I also used lazy enumeration for traversing the wordmap in my no-RAM static storage gem. <a href="https:&#x2F;&#x2F;github.com&#x2F;maxim&#x2F;wordmap&#x2F;">https:&#x2F;&#x2F;github.com&#x2F;maxim&#x2F;wordmap&#x2F;</a>
评论 #42668756 未加载
评论 #42668985 未加载
pansa24 months ago
So in Ruby, `map` and `select` are eager-by-default, but you can just write `lazy.map().select()` to make the <i>whole chain</i> lazy? That&#x27;s really nice - much better than Python 2, which was also eager-by-default and you had to change every function call to make it lazy (`map` =&gt; `imap`, `filter` =&gt; `ifilter`).<p>Python 3 changed to being lazy-by-default. I assume that improves CPU&#x2F;memory usage, but in some cases it does have less predictable behaviour. I can see why Ruby, with its goal of &quot;developer happiness&quot;, would choose to retain the predictable behaviour of eager-by-default.
评论 #42672073 未加载
thih94 months ago
Related discussion from yesterday:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=42652775">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=42652775</a> (different submitter; 29 points, 3 comments)
endorphine4 months ago
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.<p>Nevertheless, this was great.
评论 #42668419 未加载
ryangs4 months ago
Java Streams and Kotlin Sequences provide similar iterator capabilities. Iterators are great for this lazy performance but can sometimes be difficult to debug. Especially if you are nesting many iterators, then extracting the underlying collection can be complicated. But necessary in many workflows.
lukasb4 months ago
Apparently Typescript doesn’t have anything built-in like this?
Lio4 months ago
That&#x27;s lovely and makes it very obvious what&#x27;s happening.