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
How I Lazy and You Can Ruby 2
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Stephen Caudill
March 12, 2014
Programming
88
0
Share
How I Lazy and You Can Ruby 2
Lightning talk on Ruby's Enmuerator::Lazy
Stephen Caudill
March 12, 2014
More Decks by Stephen Caudill
See All by Stephen Caudill
iOS BDD Beatdown
voxdolo
7
5.6k
Other Decks in Programming
See All in Programming
実用!Hono RPC2026
yodaka
2
230
アーキテクチャモダナイゼーションとは何か
nwiizo
19
5.3k
ハーネスエンジニアリングにどう向き合うか 〜ルールファイルを超えて開発プロセスを設計する〜 / How to approach harness engineering
rkaga
23
13k
의존성 주입과 모듈화
fornewid
0
150
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
190
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
6
3.4k
Spec Driven Development | AI Summit Vilnius
danielsogl
PRO
1
110
HTML-Aware ERB: The Path to Reactive Rendering @ RubyKaigi 2026, Hakodate, Japan
marcoroth
0
150
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
440
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
430
Cache-moi si tu peux : patterns et pièges du cache en production - Devoxx France 2026 - Conférence
slecache
0
260
ソフトウェア設計の結合バランス #phperkaigi
kajitack
0
140
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.4k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
Balancing Empowerment & Direction
lara
6
1.1k
New Earth Scene 8
popppiees
3
2.1k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.2k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.2k
Done Done
chrislema
186
16k
エンジニアに許された特別な時間の終わり
watany
106
240k
Navigating Weather and Climate Data
rabernat
0
170
Faster Mobile Websites
deanohume
310
31k
What's in a price? How to price your products and services
michaelherold
247
13k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Transcript
Stephen Caudill @voxdolo
How I Lazy And You Can Ruby 2
Enumerator::Lazy
lazy evaluation In computer programming, lazy evaluation is the technique
of delaying an evaluation of any expression until a value is actually being used and also avoid repeated evaluations.
eager evaluation In computer programming, eager evaluation or greedy evaluation
is the evaluation strategy used by most traditional programming languages. In eager evaluation, an expression is evaluated as soon as it is bound to a variable.
eager evaluation (1..Float::INFINITY).map do |i| i ** 2 end
IRB::Abort: abort then interrupt!
(1..Float::INFINITY).lazy.map do |i| i ** 2 end lazy evaluation
#<Enumerator::Lazy:#<Enumerator::Lazy: 1..Infinity>:map>
Enumerator::Lazy#force “When it absolutely, positively has to be there [right
now]”
(1..Float::INFINITY).lazy.map do |i| i ** 2 end.force
IRB::Abort: abort then interrupt!
(1..Float::INFINITY).lazy.map do |i| i ** 2 end.take(10).force
(1..Float::INFINITY).lazy.map do |i| i ** 2 end.first(10)
[1,4,9,16,25,36,49,64,81,100]
Now wait just a cotton-picking minute
Why be lazy?
fibonacci = Enumerator.new do |enum| yielder = ->(n){ enum.yield n
} yielder.call(a=0) yielder.call(b=1) loop do a, b = b, a + b yielder.call(b) end end Combinatorics .lazy
fibonacci.first(10) ! => [0,1,1,2,3,5,8,13,21,34]
(1..Float::INFINITY).lazy.select do |x| x ** 2 % 5 == 0
end.take(10).reduce(:+) ! => 275
Stephen Caudill @voxdolo
None