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
Stephen Caudill
March 12, 2014
Programming
0
85
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
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
2年のAppleウォレットパス開発の振り返り
muno92
PRO
0
190
高速開発のためのコード整理術
sutetotanuki
1
340
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
120
CSC307 Lecture 05
javiergs
PRO
0
490
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
610
CSC307 Lecture 02
javiergs
PRO
1
770
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
0
990
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
120
The Art of Re-Architecture - Droidcon India 2025
siddroid
0
170
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
420
ThorVG Viewer In VS Code
nors
0
750
Featured
See All Featured
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.3k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
230
Making Projects Easy
brettharned
120
6.6k
Utilizing Notion as your number one productivity tool
mfonobong
2
200
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
46
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Building Adaptive Systems
keathley
44
2.9k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
73
The Invisible Side of Design
smashingmag
302
51k
Faster Mobile Websites
deanohume
310
31k
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