Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Enumerator.is_an(Enumerable) [es]

Sergio Gil
September 24, 2015

Enumerator.is_an(Enumerable) [es]

madrid-rb (Madrid Ruby Users Group) September 2015

Sergio Gil

September 24, 2015
Tweet

More Decks by Sergio Gil

Other Decks in Programming

Transcript

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

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

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

    9, 16, ...] x.min # 1 x.max # 9 x.all? { |i| i < 5 } # false
  4. 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
  5. 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
  6. 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
  7. 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]
  8. 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]
  9. x.group_by { |i| i % 3 }
 # {0 =>

    [3, 6, 9], 1 => [1, 4, 7], 2 => [2, 5, 8]}

  10. 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]

  11. 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]

  12. 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]]
  13. 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]
  14. three = [1, 2, 3] three.each { |i| puts i

    } three.map { |i| i * i } three.select(&:odd?) three.max three.first three.reduce(&:+)
  15. 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(&:+)
  16. Fact 1. Acepta un bloque pero no lo ejecuta (lo

    guarda) my_enum = Enumerator.new { raise '' } # no pasa nada
  17. Pues ejecuta al bloque (fact 3) my_enum.each {} # example.rb:6:in

    `block in <main>': (RuntimeError) # from examples/facts.rb:16:in `each' # from examples/facts.rb:16:in `each' # from examples/facts.rb:16:in `<main>'
  18. ¡Chaining! ¡Incluso en secuencias infinitas! Fibonacci.numbers.select { |i| i %

    5 == 0 }. take_while { |i| i < 10 ** 6 } # [0, 5, 55, 610, 6765, 75025, 832040]
  19. class Clock def self.seconds Enumerator.new do |e| loop do e

    << Time.now.to_i sleep 1 end end.lazy end end
  20. 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
  21. 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
  22. { "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
  23. { "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
  24. 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
  25. > api = Api.new('http://localhost:4567') => <Api:0x007fa96c0e1150 @url="http://localhost:4567"> > api.tracks =>

    <Enumerator::Lazy: <Enumerator: <Enumerator::Generator: 0x007fa96c0d9478>:each>>
  26. > api = Api.new('http://localhost:4567') => <Api:0x007fa96c0e1150 @url="http://localhost:4567"> > api.tracks =>

    <Enumerator::Lazy: <Enumerator: <Enumerator::Generator: 0x007fa96c0d9478>:each>> > api.tracks.first => {"id"=>1, "duration"=>37, "title"=>"Tools", "artist"=>"Tess Goyette Jr."}
  27. > api = Api.new('http://localhost:4567') => <Api:0x007fa96c0e1150 @url="http://localhost:4567"> > api.tracks =>

    <Enumerator::Lazy: <Enumerator: <Enumerator::Generator: 0x007fa96c0d9478>: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"}
  28. > api = Api.new('http://localhost:4567') => <Api:0x007fa96c0e1150 @url="http://localhost:4567"> > api.tracks =>

    <Enumerator::Lazy: <Enumerator: <Enumerator::Generator: 0x007fa96c0d9478>: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"}
  29. > total_duration = 0 => 0 > api.tracks.select { |track|

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

    track['duration'] > 120 }.take_while { |track| (total_duration += track['duration']) < 90 * 60 }.to_a
  31. > 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
  32. > 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
  33. $ 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)
  34. 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
  35. 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