Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Fun with Enumerator
Search
cyclotron3k
August 14, 2018
Programming
23
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Fun with Enumerator
Fun with Ruby's Enumerator class
cyclotron3k
August 14, 2018
More Decks by cyclotron3k
See All by cyclotron3k
Managing concurrent workloads in Ruby
cyclotron3k
0
34
Other Decks in Programming
See All in Programming
AmebaブログはKMPから卒業します
kmars0605
0
190
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
500
SREの越境 / SRE Collaboration
y0hgi
2
310
カツオ、ご期待ください
suneo3476
0
190
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.7k
残高管理から台帳サービスへの進化
artoy
1
200
パスワード再設定メールのURLについて知っておこう
uyuki234
0
160
sbt 2.0: takeoff
eed3si9n
0
160
Next.js: The final boss of Web Performance
guaca
0
250
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
ChromeでローカルLLM
ryu1013
0
140
dotNET_vulnclicker_intro.pdf
deka_morita
0
140
Featured
See All Featured
Facilitating Awesome Meetings
lara
57
7.2k
WCS-LA-2024
lcolladotor
0
890
sira's awesome portfolio website redesign presentation
elsirapls
0
450
Side Projects
sachag
456
43k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Designing for Performance
lara
612
70k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.7k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
The Curse of the Amulet
leimatthew05
3
16k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
260
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Transcript
Fun* with Enumerator (*may not be fun)
Enumerator != Enumerable [1, 2, 3].each => #<Enumerator: [1, 2,
3]:each> [1, 2, 3].map => #<Enumerator: [1, 2, 3]:map> [1, 2, 3].select => #<Enumerator: [1, 2, 3]:select>
It gets weird enum = [1, 2, 3].map => #<Enumerator:
[1, 2, 3]:map> enum.each { |n| "<#{n}>" } => ["<1>", "<2>", "<3>"]
None
We can make our own Enumerators enum = Enumerator.new do
|yielder| yielder.yield 1 yielder.yield 2 end enum.to_a => [1, 2]
enum = Enumerator.new do |yielder| puts "Beginning" yielder.yield 1 puts
"Middle" yielder.yield 2 puts "End" end enum.next Beginning => 1 enum.next Middle => 2 enum.next End StopIteration: iteration reached an end
Infinite sequences! fib = Enumerator.new do |yielder| a = 0
b = 1 loop do yielder.yield b tmp = a a = b b = tmp + b end end fib.first 10 => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Extending Enumerable module Enumerable def my_with_index Enumerator.new do |yielder| i
= 0 self.each do |e| yielder.yield e, i i += 1 end end end end [:foo, :bar, :baz].my_with_index do |e, i| puts "#{i}: #{e}" end.to_a => [[:foo, 0], [:bar, 1], [:baz, 2]]
Miscellany • Use .lazy when working with infinite series •
Useful for managing expensive operations • Does not implement a cache (See CachingEnumerator gem)
Questions?