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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
Transcript
10 slides
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?
More Decks by cyclotron3k
See All by cyclotron3k
Managing concurrent workloads in Ruby
cyclotron3k
0
34
Other Decks in Programming
See All in Programming
protojson における文字化けの扱い
kazuki1023
0
430
Seven New Principles of Software Engineering
yegor256
0
500
LLM 101
dalinaum
0
500
疎結合とは何か?ソフトウェア設計における結合バランスについて考える #phpcon_niigata
kajitack
1
190
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
15
12k
DroidKnights 2026 Android A2UI Jetpack Compose 기반의 안전한 AI UI 렌더링 - 박제창
itsmedreamwalker
1
240
LLM 101: Exploring LLMs, Starting with Tokens.
dalinaum
0
110
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
1.7k
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
180
Будущее локальных LLM – Алексей Голобурдин, PythoNN
sobolevn
0
120
プロジェクトコードをキレイキレイしよう!暗黙的な参照を読み解く静的解析
ostk0069
0
220
조합이폭발하는디자인시스템이걸눈으로도테스트하라고요
giljongyu
0
760
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
560
Paper Plane (Part 1)
katiecoart
PRO
3
12k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Producing Creativity
orderedlist
PRO
348
41k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
660
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
SEO for Brand Visibility & Recognition
aleyda
0
4.9k
Statistics for Hackers
jakevdp
799
230k
First, design no harm
axbom
PRO
2
1.4k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
300
The Limits of Empathy - UXLibs8
cassininazir
2
780