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
Enumerators
Search
Oliver Legg
October 14, 2013
Programming
760
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
760
Presenters – Take II
ollylegg
1
520
Other Decks in Programming
See All in Programming
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
390
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
190
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
160
APNsからLive Activityを開始する話
yumnumm
0
160
AWSに止められる覚悟してますか?
morizo_1984
2
520
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
21
21k
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.5k
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
130
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
570
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
100
CodeRabbitの効果検証と過ごしてみた3ヶ月
armondando
0
200
Verilogで学ぶCPU自作入門
uyuki234
8
4.1k
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Claude Code のすすめ
schroneko
67
230k
BBQ
matthewcrist
90
10k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
740
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
490
A Soul's Torment
seathinner
8
3.7k
The agentic SEO stack - context over prompts
schlessera
0
970
The Curious Case for Waylosing
cassininazir
1
560
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
610
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
840
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
420
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
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 }