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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Stephen Caudill
March 12, 2014
Programming
0
87
How I Lazy and You Can Ruby 2
Lightning talk on Ruby's Enmuerator::Lazy
Stephen Caudill
March 12, 2014
Tweet
Share
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
メッセージングを利用して時間的結合を分離しよう #phperkaigi
kajitack
3
440
生成 AI 時代のスナップショットテストってやつを見せてあげますよ(α版)
ojun9
0
310
Redox OS でのネームスペース管理と chroot の実現
isanethen
0
460
「接続」—パフォーマンスチューニングの最後の一手 〜点と点を結ぶ、その一瞬のために〜
kentaroutakeda
4
2k
AI 開発合宿を通して得た学び
niftycorp
PRO
0
180
へんな働き方
yusukebe
6
2.8k
Geminiをパートナーに神社DXシステムを個人開発した話(いなめぐDX 開発振り返り)
fujiba
0
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
1.1k
AWS×クラウドネイティブソフトウェア設計 / AWS x Cloud-Native Software Design
nrslib
16
3.4k
今年もTECHSCOREブログを書き続けます!
hiraoku101
0
180
Mastering Event Sourcing: Your Parents Holidayed in Yugoslavia
super_marek
0
120
Xdebug と IDE による デバッグ実行の仕組みを見る / Exploring-How-Debugging-Works-with-Xdebug-and-an-IDE
shin1x1
0
210
Featured
See All Featured
Prompt Engineering for Job Search
mfonobong
0
230
Balancing Empowerment & Direction
lara
5
1k
Accessibility Awareness
sabderemane
0
85
GitHub's CSS Performance
jonrohan
1032
470k
Writing Fast Ruby
sferik
630
63k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
76
So, you think you're a good person
axbom
PRO
2
2k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.5k
Making the Leap to Tech Lead
cromwellryan
135
9.8k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
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