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

Enumerable's Ugly Cousin - GORUCO Microtalk

Enumerable's Ugly Cousin - GORUCO Microtalk

A condensed, "highlights" version of the longer talk I gave at Ancient City Ruby Conf.

Ross Kaffenberger

June 25, 2016
Tweet

More Decks by Ross Kaffenberger

Other Decks in Technology

Transcript

  1. enum = [1, 2, 3].to_enum # => #<Enumerator: [1, 2,

    3]:each> enum.next # => 1 enum.next # => 2 enum.next # => 3 enum.next # => StopIteration enum.each { |x| # ... }
  2. def fibonacci(n): result = [] a = b = 1

    for i in xrange(n): result.append(a) a, b = b, a + b return result for a in fibonacci(20): print a def fibonacci(n): def fibonacci(n): result = [] def fibonacci(n): result = [] a = b = 1 for i in xrange(n): result.append(a) a, b = b, a + b return result def fibonacci(n): result = [] a = b = 1 for i in xrange(n): def fibonacci(n): result = [] a = b = 1 for i in xrange(n): result.append(a) a, b = b, a + b
  3. result = [] result.append(a) return result def fibonacci(n): a =

    b = 1 for i in xrange(n): a, b = b, a + b for a in fibonacci(20): print a Eager!
  4. yield a def fibonacci(n): a = b = 1 for

    i in xrange(n): a, b = b, a + b for a in fibonacci(20): print a Lazy!
  5. def fibonacci(n): a = b = 1 for i in

    xrange(n): yield a a, b = b, a + b for a in fibonacci(20): print a
  6. def fibonacci(n) a = b = 1 n.times do yield

    a a, b = b, a + b end end fibonacci(20) { |a| puts a } Not Enumerable!
  7. def fibonacci(n) a = b = 1 n.times do yield

    a a, b = b, a + b end end def fibonacci(n) a = b = 1 n.times do yield a a, b = b, a + b end end return to_enum(__method__, n) unless block_given?
  8. def fibonacci(n) a = b = 1 n.times do yield

    a a, b = b, a + b end end def fibonacci(n) a = b = 1 n.times do yield a a, b = b, a + b end end return to_enum(__method__, n) unless block_given? ] [ Enumerable!
  9. fibonacci(25).each { |a| puts a } fibonacci(25).map { |a| a

    * 3 }.select(&:odd?) fibonacci(100).find { |a| a > 50 }.take(5)
  10. obj_to_enum(int argc, VALUE *argv, VALUE obj) { VALUE enumerator, meth

    = sym_each; if (argc > 0) { --argc; meth = *argv++; } enumerator = rb_enumeratorize_with_size(obj, meth, argc, argv, 0); if (rb_block_given_p()) { enumerator_ptr(enumerator)->size = rb_block_proc(); } return enumerator; } static VALUE enumerator.c
  11. obj_to_enum(int argc, VALUE *argv, VALUE obj) { VALUE enumerator, meth

    = sym_each; if (argc > 0) { --argc; meth = *argv++; } enumerator = rb_enumeratorize_with_size(obj, meth, argc, argv, 0); if (rb_block_given_p()) { enumerator_ptr(enumerator)->size = rb_block_proc(); } return enumerator; } rb_enumeratorize_with_size(obj, meth, static VALUE enumerator.c obj_to_enum
  12. (1..Float::INFINITY). map { |n| n * 3 }. # =>

    ? select(&:odd?). take(100_000). reduce(&:+) (1..Float::INFINITY)
  13. (1..Float::INFINITY). lazy. map { |n| n * 3 }. select(&:odd?).

    take(100_000). reduce(&:+) # => 30000000000
  14. Live the questions now. Perhaps then, someday far in the

    future, you will gradually, without even noticing it, live your way into the answer… - Rilke