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
80
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
Honoのおもしろいミドルウェアをみてみよう
yusukebe
1
200
SpringBoot3.4の構造化ログ #kanjava
irof
2
980
WebDriver BiDiとは何なのか
yotahada3
1
140
Open source software: how to live long and go far
gaelvaroquaux
0
630
自分ひとりから始められる生産性向上の取り組み #でぃーぷらすオオサカ
irof
8
2.7k
第3回関東Kaggler会_AtCoderはKaggleの役に立つ
chettub
3
950
Introduction to kotlinx.rpc
arawn
0
670
AWS Lambda functions with C# 用の Dev Container Template を作ってみた件
mappie_kochi
0
240
[JAWS-UG横浜 #79] re:Invent 2024 の DB アップデートは Multi-Region!
maroon1st
1
140
ソフトウェアエンジニアの成長
masuda220
PRO
10
920
チームリードになって変わったこと
isaka1022
0
190
最近のVS Codeで気になるニュース 2025/01
74th
1
260
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
182
22k
Thoughts on Productivity
jonyablonski
69
4.5k
The Cost Of JavaScript in 2023
addyosmani
47
7.3k
How STYLIGHT went responsive
nonsquared
98
5.4k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
960
Java REST API Framework Comparison - PWX 2021
mraible
28
8.4k
Mobile First: as difficult as doing things right
swwweet
223
9.3k
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