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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Eric Hodel
April 27, 2016
Programming
130
0
Share
Lazy Enumeration
An introduction to lazy enumeration in ruby
Eric Hodel
April 27, 2016
More Decks by Eric Hodel
See All by Eric Hodel
Building maintainable command-line tools with MRuby
drbrain
0
680
Introduction to Rake
drbrain
0
370
Lessons in Mentorship
drbrain
1
240
Open Source Maintenance — Ruby on Ales 2014
drbrain
1
130
Open Source Maintenance — RailsClub Moscow
drbrain
1
170
drbdump
drbrain
2
530
Other Decks in Programming
See All in Programming
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
130
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
780
Coding as Prompting Since 2025
ragingwind
0
650
20260315 AWSなんもわからん🥲
chiilog
2
180
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
1
180
RSAが破られる前に知っておきたい 耐量子計算機暗号(PQC)入門 / Intro to PQC: Preparing for the Post-RSA Era
mackey0225
3
110
RailsのValidatesをSwift Macrosで再現してみた
hokuron
0
150
AIと共にエンジニアとPMの “二刀流”を実現する
naruogram
0
120
AI Assistants for YourAngular Solutions @Angular Graz, March 2026
manfredsteyer
PRO
0
140
Nuxt Server Components
wattanx
0
240
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
4.2k
AI活用のコスパを最大化する方法
ochtum
0
370
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
97
6.6k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
64
54k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
95
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
170
Deep Space Network (abreviated)
tonyrice
0
100
Prompt Engineering for Job Search
mfonobong
0
240
Building Flexible Design Systems
yeseniaperezcruz
330
40k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
500
GitHub's CSS Performance
jonrohan
1032
470k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
190
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
230
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)