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
Enumerators
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Oliver Legg
October 14, 2013
Programming
750
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Enumerators
Oliver Legg
October 14, 2013
More Decks by Oliver Legg
See All by Oliver Legg
Ruby on Rails – A Primer
ollylegg
1
740
Presenters – Take II
ollylegg
1
510
Other Decks in Programming
See All in Programming
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
120
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
AIエージェントと協働するCLI開発 — BunとOpenClawで学んだこと
yoshikouki
1
240
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
250
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
180
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
140
Swiftのレキシカルスコープ管理
kntkymt
0
220
3Dシーンの圧縮
fadis
1
680
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
170
さぁV100、メモリをお食べ・・・
nilpe
0
130
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
1.2k
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
110
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
First, design no harm
axbom
PRO
2
1.2k
Odyssey Design
rkendrick25
PRO
2
690
Utilizing Notion as your number one productivity tool
mfonobong
4
320
Automating Front-end Workflow
addyosmani
1370
210k
AI: The stuff that nobody shows you
jnunemaker
PRO
8
700
Joys of Absence: A Defence of Solitary Play
codingconduct
1
390
BBQ
matthewcrist
89
10k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
ラッコキーワード サービス紹介資料
rakko
1
3.6M
Become a Pro
speakerdeck
PRO
31
6k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Transcript
ENumerators
Enumerable #all? #any? #chunk #collect #collect_concat #count #cycle #detect #drop
#drop_while #each_cons #each_entry #each_slice #each_with_index #each_with_object #entries #find #find_all #find_index #first #flat_map #grep #group_by #include? #inject #lazy #map #max #max_by #min_by #minmax #minmax_by #none? #one? #partition #reduce #reject #reverse_each #select #slice_before #sort #sort_by #take #take_while #to_a #zip
Enumerator #each #feed #next #next_values #peek #peek_values #rewind #size #with_index
#with_object
enumerator = [1, 2, 3].each # => #<Enumerator: [1, 2,
3]:each> enumerator = [1, 2, 3].to_enum # => #<Enumerator: [1, 2, 3]:each> enumerator = [1, 2, 3].enum_for(:each) # => #<Enumerator: [1, 2, 3]:each>
def counter yield 1 yield 2 yield 3 end enumerator
= enum_for(:counter) enumerator.to_a # => [1, 2, 3]
with_object with_index
letters = ['a', 'a', 'a', 'b', 'b', 'c'] counts =
letters.inject({}) do |memo, letter| memo[letter] ||= 0 memo[letter] += 1 memo end # => {"a"=>3, "b"=>2, "c"=>1}
letters = ['a', 'a', 'a', 'b', 'b', 'c'] counts =
letters.each.with_object({}) do |letter, memo| memo[letter] ||= 0 memo[letter] += 1 end # => {"a"=>3, "b"=>2, "c"=>1}
range = (1..10) range.map.with_index {|n, i| n * i }
# => [0, 2, 6, 12, 20, 30, 42, 56, 72, 90] range.select.with_index {|_, i| i.even? } # => [1, 3, 5, 7, 9]
Iterator
enumerator = [1, 2, 3].each # => #<Enumerator: [1, 2,
3]:each> enumerator.next # => 1 enumerator.next # => 2 enumerator.next # => 3 enumerator.next # StopIteration: iteration reached an end
enumerator = [1, 2, 3].to_enum # => #<Enumerator: [1, 2,
3]:each> enumerator.peek # => 1 enumerator.peek # => 2 enumerator.peek # => 3 enumerator.next # => 1
loop do i = enumerator.next puts i # `loop` silently
rescues StopIteration end
Generator
fibonacci = Enumerator.new(Float::INFINITY) do |yielder| a, b = 0, 1
loop do yielder.yield a a, b = b, (a + b) end end
class SumOfNaturalNumbers < Enumerator def initialize super(Float::INFINITY) do |yielder| n
= 1 loop do yielder.yield (n * (n + 1)) / 2 n += 1 end end end end
def fibonacci a, b = 0, 1 loop do yield
a a, b = b, (a + b) end end enumerator = enum_for(:fibonacci) # => #<Enumerator: main:fibonacci>
LAZY
require 'prime' primes = Prime.instance primes .select {|i| i.to_s.end_with?('3') }
.take(10) # infinite loop
require 'prime' primes = Prime.instance primes .lazy .select {|i| i.to_s.end_with?('3')
} .take(10) .to_a # => [3, 13, 23, 43, 53, 73, 83, 103, 113, 163]
File.open('/usr/share/dict/words') .each_line .lazy .map(&:chomp) .take_while {|line| line.length < 10 }
.to_a
mine = ->(repository) { repository.owner == 'olly' } cutoff =
(Time.now - (365 * 24 * 60 * 60)).to_datetime year_old = ->(repository) { repository.commits.first.date > cutoff } client.repositories .lazy .select(&mine) .select(&year_old) .take(10) .each {|repository| puts repository.name }