Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
WebRTC と Rust と8K 60fps
tnoho
2
2k
안드로이드 9년차 개발자, 프론트엔드 주니어로 커리어 리셋하기
maryang
1
110
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
230
AWS CDKの推しポイントN選
akihisaikeda
1
240
WebRTC、 綺麗に見るか滑らかに見るか
sublimer
1
160
なあ兄弟、 余白の意味を考えてから UI実装してくれ!
ktcryomm
11
11k
Canon EOS R50 V と R5 Mark II 購入でみえてきた最近のデジイチ VR180 事情、そして VR180 静止画に活路を見出すまで
karad
0
110
dotfiles 式年遷宮 令和最新版
masawada
1
770
Cell-Based Architecture
larchanjo
0
120
モデル駆動設計をやってみようワークショップ開催報告(Modeling Forum2025) / model driven design workshop report
haru860
0
270
【CA.ai #3】Google ADKを活用したAI Agent開発と運用知見
harappa80
0
310
ViewファーストなRailsアプリ開発のたのしさ
sugiwe
0
470
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.8k
What's in a price? How to price your products and services
michaelherold
246
13k
Six Lessons from altMBA
skipperchong
29
4.1k
Mobile First: as difficult as doing things right
swwweet
225
10k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Building Applications with DynamoDB
mza
96
6.8k
Designing Experiences People Love
moore
143
24k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
730
Leading Effective Engineering Teams in the AI Era
addyosmani
8
1.3k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Site-Speed That Sticks
csswizardry
13
1k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
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