Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Enumerators
Oliver Legg
October 14, 2013
Programming
1
350
Enumerators
Oliver Legg
October 14, 2013
Tweet
Share
More Decks by Oliver Legg
See All by Oliver Legg
Ruby on Rails – A Primer
ollylegg
1
410
Presenters – Take II
ollylegg
1
210
Other Decks in Programming
See All in Programming
もしも、 上司に鬼退治を命じられたら~プロジェクト計画編~
higuuu
0
290
Unity Localization で多言語対応実装しよう / xrdnk-yokohamaunity-lt10-20220513
xrdnk
0
150
実録mruby組み込み体験
coe401_
0
110
バンドル最適化マニアクス at tfconf
mizchi
4
2.3k
Update from the Elixir team - 2022
whatyouhide
0
200
SPA/MPA 議論の俯瞰と 現代における設計のポイント - #tfcon 2022 フロントエンド設計
ahomu
3
1.9k
近況PHP / PHP in now a days
uzulla
4
1.8k
Milestoner
bkuhlmann
1
200
アプリのログをチーム外で活用してもらうためにやったこと
shotakashihara
0
190
TechFeed Conference 2022 - Kotlin Experimental
jmatsu
0
820
ebpfとWASMに思いを馳せる2022 / techfeed-conference-2022-ebpf-wasm-amsy810
masayaaoyama
0
730
Android入門
hn410
0
310
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
PRO
11
4.6k
Happy Clients
brianwarren
89
5.5k
Bootstrapping a Software Product
garrettdimon
295
110k
How to Ace a Technical Interview
jacobian
265
21k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
7
1k
Building Your Own Lightsaber
phodgson
94
4.6k
Designing for humans not robots
tammielis
241
23k
Fashionably flexible responsive web design (full day workshop)
malarkey
396
62k
Infographics Made Easy
chrislema
233
17k
Building Flexible Design Systems
yeseniaperezcruz
310
33k
Streamline your AJAX requests with AmplifyJS and jQuery
dougneiner
125
8.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
37
3.2k
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 }