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

Lazy Enumeration

Lazy Enumeration

An introduction to lazy enumeration in ruby

Eric Hodel

April 27, 2016
Tweet

More Decks by Eric Hodel

Other Decks in Programming

Transcript

  1. Loop values = [1, 2, 3, 4] doubles = []

    index = 0 while index < values.length do doubles << values[index] * 2 index += 1 end
  2. Enumera,ng Where am I? values[index] Am I done? index <

    values.length What’s next? index += 1
  3. Enumerator API Where am I? next #=> nil or exception

    if done Am I done? nil, StopIteration What’s next? handled for you
  4. Eager Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value

    = orders.map { |order| # 10,000 values order[‘total’] }.reduce { |sum, order_total| sum + order_total }
  5. Eager Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value

    = orders.map { |order| # 100,000,000 values order[‘total’] }.reduce { |sum, order_total| sum + order_total }
  6. Lazy Enumera,on orders = db_conn.exec ‘SELECT total FROM orders’ total_order_value

    = orders.lazy.map { |order| order[‘total’] }.reduce { |sum, order_total| sum + order_total } 10MB similar ,me
  7. Lazy Processing 1 1 .map .map 1 .map 1 2

    2 2 2 3 3 3 3 … … … … 100M 100M 100M 100M
  8. Fiber? •Story line for a program •One Fiber runs at

    a ,me •Scheduled by author •“Corou,ne”
  9. Ac,ve Fiber 1 1 .map .map 1 .map 1 2

    2 2 2 3 3 3 3 … … … … 100M 100M 100M 100M Fiber Fiber Fiber Fiber
  10. Lazy Enumera,on •Reduces memory used •Great for huge data sets

    •Processes one at a ,me •Uses Fiber (corou,ne)