Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fun with Enumerator
Search
cyclotron3k
August 14, 2018
Programming
1
18
Fun with Enumerator
Fun with Ruby's Enumerator class
cyclotron3k
August 14, 2018
Tweet
Share
More Decks by cyclotron3k
See All by cyclotron3k
Managing concurrent workloads in Ruby
cyclotron3k
0
28
Other Decks in Programming
See All in Programming
俺流レスポンシブコーディング 2025
tak_dcxi
14
8.5k
配送計画の均等化機能を提供する取り組みについて(⽩⾦鉱業 Meetup Vol.21@六本⽊(数理最適化編))
izu_nori
0
150
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
140
DSPy Meetup Tokyo #1 - はじめてのDSPy
masahiro_nishimi
1
160
Cap'n Webについて
yusukebe
0
130
How Software Deployment tools have changed in the past 20 years
geshan
0
29k
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
380
AIコーディングエージェント(skywork)
kondai24
0
150
ゲームの物理 剛体編
fadis
0
320
非同期処理の迷宮を抜ける: 初学者がつまづく構造的な原因
pd1xx
1
700
Context is King? 〜Verifiability時代とコンテキスト設計 / Beyond "Context is King"
rkaga
7
1.1k
React Native New Architecture 移行実践報告
taminif
1
150
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
970
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
How STYLIGHT went responsive
nonsquared
100
6k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Docker and Python
trallard
47
3.7k
Typedesign – Prime Four
hannesfritz
42
2.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3k
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?