Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Lazy Enumeration
Search
Eric Hodel
April 27, 2016
Programming
0
130
Lazy Enumeration
An introduction to lazy enumeration in ruby
Eric Hodel
April 27, 2016
Tweet
Share
More Decks by Eric Hodel
See All by Eric Hodel
Building maintainable command-line tools with MRuby
drbrain
0
660
Introduction to Rake
drbrain
0
350
Lessons in Mentorship
drbrain
1
230
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
130
Open Source Maintenance — RailsClub Moscow
drbrain
1
170
drbdump
drbrain
2
510
Other Decks in Programming
See All in Programming
Rails Girls Tokyo 18th GMO Pepabo Sponsor Talk
yutokyokutyo
0
120
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
300
Metaprogramming isn't real, it can't hurt you
okuramasafumi
0
110
humanlayerのブログから学ぶ、良いCLAUDE.mdの書き方
tsukamoto1783
0
200
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
190
AI時代の認知負荷との向き合い方
optfit
0
180
SourceGeneratorのススメ
htkym
0
430
Sekiban + Microsoft Orleans のアクターをAWS対応しました / Sekiban + Microsoft Orleans actors are now supported on AWS.
tomohisa
0
110
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
190
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
360
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
230
ぼくの開発環境2026
yuzneri
1
270
Featured
See All Featured
Designing for Timeless Needs
cassininazir
0
140
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Rails Girls Zürich Keynote
gr2m
96
14k
Google's AI Overviews - The New Search
badams
0
920
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
The Curse of the Amulet
leimatthew05
1
8.9k
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
SEO for Brand Visibility & Recognition
aleyda
0
4.3k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
Darren the Foodie - Storyboard
khoart
PRO
2
2.5k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7k
Transcript
Lazy Enumera,on Eric Hodel —
[email protected]
Loop values = [1, 2, 3, 4] doubles = []
index = 0 while index < values.length do doubles << values[index] * 2 index += 1 end
Enumera,ng Where am I? values[index] Am I done? index <
values.length What’s next? index += 1
Enumerator API Where am I? next #=> nil or exception
if done Am I done? nil, StopIteration What’s next? handled for you
External Enumerator result = db_conn.exec ‘SELECT * FROM orders’ while
order = result.next do # … end
Internal Enumerator result = db_conn.exec ‘SELECT * FROM orders’ result.each_row
do |order| # … end
External vs Internal You write loop Impera,ve C, Ruby Loop
built-in Func,onal Scheme, Ruby
Eager Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.map { |order| # 10,000 values order[‘total’] }.reduce { |sum, order_total| sum + order_total }
Eager Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.map { |order| # 100,000,000 values order[‘total’] }.reduce { |sum, order_total| sum + order_total }
100,000,000 Objects >> a = Array.new 100_000_000 >> ObjectSpace.memsize_of a
=> 800000040 800MB 400ms
Lazy Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value
= orders.lazy.map { |order| order[‘total’] }.reduce { |sum, order_total| sum + order_total } 10MB similar ,me
Eager Processing 100M 100M .map .map 100M .map 100M
Lazy Processing 1 1 .map .map 1 .map 1 2
2 2 2 3 3 3 3 … … … … 100M 100M 100M 100M
How does lazy work? Fibers!
Fiber? •Story line for a program •One Fiber runs at
a ,me •Scheduled by author •“Corou,ne”
Hierarchy Process ↳Thread ↳Fiber OS scheduled Manually scheduled
Scheduling Fibers resume(input) #=> output Run a specific Fiber Fiber.yield(value)
Return output to #resume
Ac,ve Fiber 1 1 .map .map 1 .map 1 2
2 2 2 3 3 3 3 … … … … 100M 100M 100M 100M Fiber Fiber Fiber Fiber
Example of Fiber ⃠
Lazy Enumera,on •Reduces memory used •Great for huge data sets
•Processes one at a ,me •Uses Fiber (corou,ne)