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
Fun with Enumerator
Search
cyclotron3k
August 14, 2018
Programming
22
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
33
Other Decks in Programming
See All in Programming
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
630
コンパウンドプロダクト開発のためのローカルプロセスマネージャー再発明 #layerxgo
izumin5210
0
640
[KotlinConf Extended South Korea 2026] KotlinConf 2026 발표자 후기
wisemuji
0
100
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
180
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
740
「つくるAI」だけではバグは見つからない ~テストに必要な「見つけるAI」を分離させる戦略~
mfunaki
0
230
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
110
片田舎のおっさん、 Swift Buildのダイアモンド問題解決の不具合修正PRを出すが、解決方法がキャッシュをしないようにすることであり、ビルド時間が伸びると言われてマージされないので高速化もする/swiftbuild
yimajo
0
360
typoなんかねぇよ
raspython3
0
660
MySQLとPostgreSQLって何が違うの?
akagami
PRO
0
170
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
140
Webエンジニアなのにブラウザの仕組みがわからないので、Pythonで自作してみた
tatsuki12
4
1.7k
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.9k
From π to Pie charts
rasagy
0
350
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
490
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
A better future with KSS
kneath
240
18k
エンジニアに許された特別な時間の終わり
watany
108
250k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
770
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
230
WENDY [Excerpt]
tessaabrams
12
39k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
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?