Ross Kaffenberger
June 25, 2016
280

# Enumerable's Ugly Cousin - GORUCO Microtalk

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

June 25, 2016

## Transcript

CONCISE

13. ### Enumerator – Ruby Docs “A class which allows both internal

and external iteration.”
14. ### 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| # ... }

coroutines

23. ### Awesome Simple, expressive. Love them. List comprehensions… insanely readable and

easy to maintain

25. ### 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
26. ### 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!
27. ### 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!

29. ### 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
30. ### 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!

32. ### 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?
33. ### return to_enum(__method__) unless block_given? This seems like a big hack

Too magical That’s ugly
34. ### 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!
35. ### fibonacci(25).each { |a| puts a } fibonacci(25).map { |a| a

* 3 }.select(&:odd?) fibonacci(100).find { |a| a > 50 }.take(5)
36. ### 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
37. ### 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

39. ### (1..Float::INFINITY). map { |n| n * 3 }. # =>

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

take(100_000). reduce(&:+) # => 30000000000

53. ### 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