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
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
GitHubCopilotCLIをはじめよう.pdf
htkym
0
330
Symfony AI in Action - SymfonyLive Berlin 2026
chr_hertel
1
150
Road to RubyKaigi: Play Hard(ware)
makicamel
1
570
エラー処理の温故知新 / history of error handling technic
ryotanakaya
7
1.9k
GoogleCloudとterraform完全に理解した
terisuke
1
200
Spec Driven Development | AI Summit Vilnius
danielsogl
PRO
1
160
2026-04-15 Spring IO - I Can See Clearly Now
jonatan_ivanov
1
210
20260514_its_the_context_window_stupid.pdf
heita
0
990
PHPでローカル環境用のSSL/TLS証明書を発行することはできるのか? #phpconkagawa
akase244
0
370
「OSSがあるなら自作するな」は AI時代も正しいか ── Build vs Adopt の新しい判断基準
kumorn5s
7
2.6k
tRPCの概要と少しだけパフォーマンス
misoton665
2
270
空間オーディオの活用
objectiveaudio
0
150
Featured
See All Featured
Amusing Abliteration
ianozsvald
1
170
Believing is Seeing
oripsolob
1
120
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.8k
Done Done
chrislema
186
16k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.6k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
140
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Why Our Code Smells
bkeepers
PRO
340
58k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
Test your architecture with Archunit
thirion
1
2.2k
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