Slide 1

Slide 1 text

Stephen Caudill @voxdolo

Slide 2

Slide 2 text

How I Lazy And You Can Ruby 2

Slide 3

Slide 3 text

Enumerator::Lazy

Slide 4

Slide 4 text

lazy evaluation In computer programming, lazy evaluation is the technique of delaying an evaluation of any expression until a value is actually being used and also avoid repeated evaluations.

Slide 5

Slide 5 text

eager evaluation In computer programming, eager evaluation or greedy evaluation is the evaluation strategy used by most traditional programming languages. In eager evaluation, an expression is evaluated as soon as it is bound to a variable.

Slide 6

Slide 6 text

eager evaluation (1..Float::INFINITY).map do |i| i ** 2 end

Slide 7

Slide 7 text

IRB::Abort: abort then interrupt!

Slide 8

Slide 8 text

(1..Float::INFINITY).lazy.map do |i| i ** 2 end lazy evaluation

Slide 9

Slide 9 text

#:map>

Slide 10

Slide 10 text

Enumerator::Lazy#force “When it absolutely, positively has to be there [right now]”

Slide 11

Slide 11 text

(1..Float::INFINITY).lazy.map do |i| i ** 2 end.force

Slide 12

Slide 12 text

IRB::Abort: abort then interrupt!

Slide 13

Slide 13 text

(1..Float::INFINITY).lazy.map do |i| i ** 2 end.take(10).force

Slide 14

Slide 14 text

(1..Float::INFINITY).lazy.map do |i| i ** 2 end.first(10)

Slide 15

Slide 15 text

[1,4,9,16,25,36,49,64,81,100]

Slide 16

Slide 16 text

Now wait just a cotton-picking minute

Slide 17

Slide 17 text

Why be lazy?

Slide 18

Slide 18 text

fibonacci = Enumerator.new do |enum| yielder = ->(n){ enum.yield n } yielder.call(a=0) yielder.call(b=1) loop do a, b = b, a + b yielder.call(b) end end Combinatorics .lazy

Slide 19

Slide 19 text

fibonacci.first(10) ! => [0,1,1,2,3,5,8,13,21,34]

Slide 20

Slide 20 text

(1..Float::INFINITY).lazy.select do |x| x ** 2 % 5 == 0 end.take(10).reduce(:+) ! => 275

Slide 21

Slide 21 text

Stephen Caudill @voxdolo

Slide 22

Slide 22 text

No content