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
83
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
問題の見方を変える「システム思考」超入門
panda_program
0
160
マイベストのシンプルなデータ基盤の話 - Googleスイートとのつき合い方 / mybest-simple-data-architecture-google-nized
snhryt
0
130
モテるデスク環境
mozumasu
3
1.4k
エンジニアに事業やプロダクトを理解してもらうためにやってること
murabayashi
0
130
Introducing RemoteCompose: break your UI out of the app sandbox.
camaelon
2
480
Swift Concurrency 年表クイズ
omochi
3
220
AsyncSequenceとAsyncStreamのプロポーザルを全部読む!!
s_shimotori
1
240
PHPライセンス変更の議論を通じて学ぶOSSライセンスの基礎
matsuo_atsushi
0
120
Designing Repeatable Edits: The Architecture of . in Vim
satorunooshie
0
250
Blazing Fast UI Development with Compose Hot Reload (droidcon London 2025)
zsmb
0
480
チームのテスト力を総合的に鍛えてシフトレフトを推進する/Shifting Left with Software Testing Improvements
goyoki
4
2.1k
AI駆動開発カンファレンスAutumn2025 _AI駆動開発にはAI駆動品質保証
autifyhq
0
130
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Into the Great Unknown - MozCon
thekraken
40
2.1k
The Cult of Friendly URLs
andyhume
79
6.7k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Making Projects Easy
brettharned
120
6.4k
Agile that works and the tools we love
rasmusluckow
331
21k
Rails Girls Zürich Keynote
gr2m
95
14k
Speed Design
sergeychernyshev
32
1.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6k
Mobile First: as difficult as doing things right
swwweet
225
10k
Building Flexible Design Systems
yeseniaperezcruz
329
39k
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