Slide 1

Slide 1 text

Enumerator is an Enumerable Sergio Gil iamserg.io / @porras RUG::B July 2015

Slide 2

Slide 2 text

Enumerator is an Enumerable Sergio Gil iamserg.io / @porras RUG::B July 2015 !

Slide 3

Slide 3 text

Everyone ❤ Enumerable

Slide 4

Slide 4 text

Everyone ❤ Enumerable FACT

Slide 5

Slide 5 text

x.map { |i| i * i } # [1, 4, 9, 16, ...]

Slide 6

Slide 6 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1

Slide 7

Slide 7 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1 x.max # 9

Slide 8

Slide 8 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1 x.max # 9 x.all? { |i| i < 5 } # false

Slide 9

Slide 9 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1 x.max # 9 x.all? { |i| i < 5 } # false x.any?(&:odd?) # true

Slide 10

Slide 10 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1 x.max # 9 x.all? { |i| i < 5 } # false x.any?(&:odd?) # true x.detect { |i| i > 5 } # 6

Slide 11

Slide 11 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1 x.max # 9 x.all? { |i| i < 5 } # false x.any?(&:odd?) # true x.detect { |i| i > 5 } # 6 x.reduce(&:*) # 362880

Slide 12

Slide 12 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1 x.max # 9 x.all? { |i| i < 5 } # false x.any?(&:odd?) # true x.detect { |i| i > 5 } # 6 x.reduce(&:*) # 362880 x.select(&:odd?) # [1, 3, 5, 7, 9]

Slide 13

Slide 13 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1 x.max # 9 x.all? { |i| i < 5 } # false x.any?(&:odd?) # true x.detect { |i| i > 5 } # 6 x.reduce(&:*) # 362880 x.select(&:odd?) # [1, 3, 5, 7, 9] x.take_while { |i| i < 5 } # [1, 2, 3, 4]

Slide 14

Slide 14 text

x.group_by { |i| i % 3 }
 # {0 => [3, 6, 9], 1 => [1, 4, 7], 2 => [2, 5, 8]}


Slide 15

Slide 15 text

x.group_by { |i| i % 3 }
 # {0 => [3, 6, 9], 1 => [1, 4, 7], 2 => [2, 5, 8]}
 x.each_slice(3) { |slice| p slice }
 # [1, 2, 3]
 # [4, 5, 6]
 # [7, 8, 9]


Slide 16

Slide 16 text

x.group_by { |i| i % 3 }
 # {0 => [3, 6, 9], 1 => [1, 4, 7], 2 => [2, 5, 8]}
 x.each_slice(3) { |slice| p slice }
 # [1, 2, 3]
 # [4, 5, 6]
 # [7, 8, 9]
 x.each_slice(3).map { |slice| slice.first }
 # [1, 4, 7]


Slide 17

Slide 17 text

x.group_by { |i| i % 3 }
 # {0 => [3, 6, 9], 1 => [1, 4, 7], 2 => [2, 5, 8]}
 x.each_slice(3) { |slice| p slice }
 # [1, 2, 3]
 # [4, 5, 6]
 # [7, 8, 9]
 x.each_slice(3).map { |slice| slice.first }
 # [1, 4, 7]
 x.partition(&:odd?)
 # [[1, 3, 5, 7, 9], [2, 4, 6, 8]]

Slide 18

Slide 18 text

http://www.rubydoc.info/stdlib/core/Enumerable

Slide 19

Slide 19 text

x.map { |i| i * i } # [1, 4, 9, 16, ...] x.min # 1 x.max # 9 x.all? { |i| i < 5 } # false x.any?(&:odd?) # true x.detect { |i| i > 5 } # 6 x.reduce(&:*) # 362880 x.select(&:odd?) # [1, 3, 5, 7, 9] x.take_while { |i| i < 5 } # [1, 2, 3, 4]

Slide 20

Slide 20 text

But what’s x?

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No t necessarily .

Slide 23

Slide 23 text

http://www.rubydoc.info/stdlib/core/Enumerable

Slide 24

Slide 24 text

And what’s an Enumerable?

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

x = (1..9)

Slide 27

Slide 27 text

My own Enumerable!

Slide 28

Slide 28 text

1. Implement #each

Slide 29

Slide 29 text

1. Implement #each 2. include Enumerable

Slide 30

Slide 30 text

1. Implement #each 2. include Enumerable 3. No step 3

Slide 31

Slide 31 text

three = [1, 2, 3] three.each { |i| puts i } three.map { |i| i * i } three.select(&:odd?) three.max three.first three.reduce(&:+)

Slide 32

Slide 32 text

class Three include Enumerable def each yield 1 yield 2 yield 3 end end three = Three.new three.each { |i| puts i } three.map { |i| i * i } three.select(&:odd?) three.max three.first three.reduce(&:+)

Slide 33

Slide 33 text

Recap so far:

Slide 34

Slide 34 text

Recap so far: Enumerable is cool.

Slide 35

Slide 35 text

Recap so far: Enumerable is cool. You can have your own Enumerables; just implement #each.

Slide 36

Slide 36 text

Enumerator: an easy way to implement #each

Slide 37

Slide 37 text

Also, no need to include Enumerable

Slide 38

Slide 38 text

Fact 1. It takes a block which is not called but stored

Slide 39

Slide 39 text

Fact 1. It takes a block which is not called but stored my_enum = Enumerator.new { raise '' } # nothing happens

Slide 40

Slide 40 text

Fact 2. It implements #each, includes Enumerable

Slide 41

Slide 41 text

Fact 2. It implements #each, includes Enumerable my_enum.respond_to?(:each) # true

Slide 42

Slide 42 text

Fact 2. It implements #each, includes Enumerable my_enum.respond_to?(:each) # true my_enum.is_a?(Enumerable) # true

Slide 43

Slide 43 text

And what does #each do?

Slide 44

Slide 44 text

Well, it calls the block (that’s fact 3)

Slide 45

Slide 45 text

Well, it calls the block (that’s fact 3) my_enum.each {} # example.rb:6:in `block in ': (RuntimeError) # from examples/facts.rb:16:in `each' # from examples/facts.rb:16:in `each' # from examples/facts.rb:16:in `'

Slide 46

Slide 46 text

my_enum = Enumerator.new do |e| e << 'hello!' end my_enum.each do |item| p item end # "hello!"

Slide 47

Slide 47 text

my_enum = [].tap do |a| a << 'hello!' end my_enum.each do |item| p item end # "hello!"

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Bonus Track: Enumerator::Lazy

Slide 51

Slide 51 text

Bonus Track: Enumerator::Lazy my_enum.map(&:upcase).class # Array

Slide 52

Slide 52 text

Bonus Track: Enumerator::Lazy my_enum.map(&:upcase).class # Array my_enum.lazy.map(&:upcase).class # Enumerator::Lazy

Slide 53

Slide 53 text

Chaining! Even on infinite streams! Fibonacci.numbers.select { |i| i % 5 == 0 }. take_while { |i| i < 10 ** 6 }

Slide 54

Slide 54 text

Chaining! Even on infinite streams! Fibonacci.numbers.select { |i| i % 5 == 0 }. take_while { |i| i < 10 ** 6 } # [0, 5, 55, 610, 6765, 75025, 832040]

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

class Clock def self.seconds Enumerator.new do |e| loop do e << Time.now.to_i sleep 1 end end.lazy end end

Slide 58

Slide 58 text

class Clock def self.seconds Enumerator.new do |e| loop do e << Time.now.to_i sleep 1 end end.lazy end end Clock.seconds.select(&:odd?).take(5).each do |time| p time end

Slide 59

Slide 59 text

class Clock def self.seconds Enumerator.new do |e| loop do e << Time.now.to_i sleep 1 end end.lazy end end Clock.seconds.select(&:odd?).take(5).each do |time| p time end # 1435648191 # 1435648193 # 1435648195 # 1435648197 # 1435648199

Slide 60

Slide 60 text

class Clock ... def self.minutes seconds.select { |t| t % 60 == 0 } end end

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

CLOUDSOUND

Slide 63

Slide 63 text

{ "tracks": [ { "id": 1, "duration": 37, "title": "Tools", "artist": "Tess Goyette Jr." }, ..., { "id": 11, "duration": 153, "title": "Grocery", "artist": "Geovanni McKenzie" } ], "meta": { "next": "http://localhost:4567/tracks?after=11" } } GET /tracks

Slide 64

Slide 64 text

{ "tracks": [ { "id": 12, "duration": 153, "title": "Jewelery", "artist": "Berniece Hayes" }, ... { "id": 21, "duration": 161, "title": "Grocery & Beauty", "artist": "Adah Kozey" } ], "meta": { "next": "http://localhost:4567/tracks?after=21" } } GET /tracks?after=11

Slide 65

Slide 65 text

class Api def initialize(url) @url = url end def tracks url = "#{@url}/tracks" Enumerator.new do |e| loop do data = get(url) data['tracks'].each do |track| e << track end url = data['meta']['next'] end end.lazy end end

Slide 66

Slide 66 text

> api = Api.new('http://localhost:4567') =>

Slide 67

Slide 67 text

> api = Api.new('http://localhost:4567') => > api.tracks => :each>>

Slide 68

Slide 68 text

> api = Api.new('http://localhost:4567') => > api.tracks => :each>> > api.tracks.first => {"id"=>1, "duration"=>37, "title"=>"Tools", "artist"=>"Tess Goyette Jr."}

Slide 69

Slide 69 text

> api = Api.new('http://localhost:4567') => > api.tracks => :each>> > api.tracks.first => {"id"=>1, "duration"=>37, "title"=>"Tools", "artist"=>"Tess Goyette Jr."} > api.tracks.drop(15).first => {"id"=>16, "duration"=>192, "title"=>"Clothing & Books", "artist"=>"Vicente Dickens"}

Slide 70

Slide 70 text

> api = Api.new('http://localhost:4567') => > api.tracks => :each>> > api.tracks.first => {"id"=>1, "duration"=>37, "title"=>"Tools", "artist"=>"Tess Goyette Jr."} > api.tracks.drop(15).first => {"id"=>16, "duration"=>192, "title"=>"Clothing & Books", "artist"=>"Vicente Dickens"} > api.tracks.detect { |t| t['duration'] == 180 } => {"id"=>157, "duration"=>180, "title"=>"Health & Games", "artist"=>"Kareem Tillman DDS"}

Slide 71

Slide 71 text

> total_duration = 0 => 0

Slide 72

Slide 72 text

> total_duration = 0 => 0

Slide 73

Slide 73 text

> total_duration = 0 => 0 > api.tracks.select { |track| track['duration'] > 120 }.take_while { |track| (total_duration += track['duration']) < 90 * 60 }.to_a

Slide 74

Slide 74 text

> total_duration = 0 => 0 > api.tracks.select { |track| track['duration'] > 120 }.take_while { |track| (total_duration += track['duration']) < 90 * 60 }.to_a

Slide 75

Slide 75 text

> total_duration = 0 => 0 > api.tracks.select { |track| track['duration'] > 120 }.take_while { |track| (total_duration += track['duration']) < 90 * 60 }.to_a => [{"id"=>2, "duration"=>144, "title"=>"Toys & Garden", "artist"=>"Rossie Senger"}, {"id"=>4, "duration"=>234, "title"=>"Baby", "artist"=>"Scottie Kling"}, {"id"=>5, "duration"=>215, "title"=>"Jewelery & Books", "artist"=>"Marge Simonis"}, {"id"=>7, "duration"=>216, "title"=>"Outdoors & Clothing", "artist"=>"Cortez Windler"}, {"id"=>11, "duration"=>153, "title"=>"Grocery", "artist"=>"Geovanni McKenzie"}, {"id"=>11, "duration"=>153, "title"=>"Jewelery", "artist"=>"Berniece Hayes"}, {"id"=>13, "duration"=>132, "title"=>"Toys, Movies & Health", "artist"=>"Joel Kuhlman"}, {"id"=>14, "duration"=>212, "title"=>"Jewelery & Games", "artist"=>"Jalyn Langosh"}, {"id"=>15, "duration"=>192, "title"=>"Clothing & Books", "artist"=>"Vicente Dickens"}, {"id"=>17, "duration"=>223, "title"=>"Industrial & Garden", "artist"=>"Archibald

Slide 76

Slide 76 text

> total_duration = 0 => 0 > api.tracks.select { |track| track['duration'] > 120 }.take_while { |track| (total_duration += track['duration']) < 90 * 60 }.to_a => [{"id"=>2, "duration"=>144, "title"=>"Toys & Garden", "artist"=>"Rossie Senger"}, {"id"=>4, "duration"=>234, "title"=>"Baby", "artist"=>"Scottie Kling"}, {"id"=>5, "duration"=>215, "title"=>"Jewelery & Books", "artist"=>"Marge Simonis"}, {"id"=>7, "duration"=>216, "title"=>"Outdoors & Clothing", "artist"=>"Cortez Windler"}, {"id"=>11, "duration"=>153, "title"=>"Grocery", "artist"=>"Geovanni McKenzie"}, {"id"=>11, "duration"=>153, "title"=>"Jewelery", "artist"=>"Berniece Hayes"}, {"id"=>13, "duration"=>132, "title"=>"Toys, Movies & Health", "artist"=>"Joel Kuhlman"}, {"id"=>14, "duration"=>212, "title"=>"Jewelery & Games", "artist"=>"Jalyn Langosh"}, {"id"=>15, "duration"=>192, "title"=>"Clothing & Books", "artist"=>"Vicente Dickens"}, {"id"=>17, "duration"=>223, "title"=>"Industrial & Garden", "artist"=>"Archibald

Slide 77

Slide 77 text

$ ruby examples/mixtape/mixtape.rb 45

Slide 78

Slide 78 text

$ ruby examples/mixtape/mixtape.rb 45 14 tracks (44:17) Rossie Senger - Toys & Garden (2:24) Scottie Kling - Baby (3:54) Marge Simonis - Jewelery & Books (3:35) Cortez Windler - Outdoors & Clothing (3:36) Geovanni McKenzie - Grocery (2:33) Berniece Hayes - Jewelery (2:33) Joel Kuhlman - Toys, Movies & Health (2:12) Jalyn Langosh - Jewelery & Games (3:32) Vicente Dickens - Clothing & Books (3:12) Archibald Huels - Industrial & Garden (3:43) Adah Kozey - Grocery & Beauty (2:41) Mr. Geo Torp - Electronics (3:21) Doug Lesch - Jewelery, Health & Tools (3:08) Esta Daugherty - Shoes & Baby (3:53)

Slide 79

Slide 79 text

Recap:

Slide 80

Slide 80 text

Recap: Enumerable: do magic with lists.

Slide 81

Slide 81 text

Recap: Enumerable: do magic with lists. Enumerator: do lists with everything.

Slide 82

Slide 82 text

Thanks! Sergio Gil iamserg.io / @porras RUG::B July 2015 https://github.com/porras/enumerator-talk

Slide 83

Slide 83 text

Thanks! Sergio Gil iamserg.io / @porras RUG::B July 2015 https://github.com/porras/enumerator-talk

Slide 84

Slide 84 text

Thanks! Questions? Sergio Gil iamserg.io / @porras RUG::B July 2015 https://github.com/porras/enumerator-talk

Slide 85

Slide 85 text

This talk’s little dirty secret

Slide 86

Slide 86 text

Like on a koan,

Slide 87

Slide 87 text

when the student finally understands Enumerator

Slide 88

Slide 88 text

they’ll notice that

Slide 89

Slide 89 text

they don’t need Enumerator

Slide 90

Slide 90 text

there is no Enumerator

Slide 91

Slide 91 text

Enumerator is #each

Slide 92

Slide 92 text

Enumerator is #each #each is Enumerator

Slide 93

Slide 93 text

class Api def initialize(url) @url = url end def tracks url = "#{@url}/tracks" Enumerator.new do |e| loop do data = get(url) data['tracks'].each do |track| e << track end url = data['meta']['next'] end end.lazy end end

Slide 94

Slide 94 text

class Api def initialize(url) @url = url end def tracks Tracks.new("#{@url}/tracks").lazy end class Tracks def initialize(url) @url = url end def each url = @url loop do data = get(url) data['tracks'].each do |track| yield track end url = data['meta']['next'] end end include Enumerable end end https://github.com/porras/enumerator-talk/tree/no-enumerator

Slide 95

Slide 95 text

and the student will be enlightened