From: jeremy Ruten Date: 2010-08-02T12:22:57+09:00 Subject: Re: Lazy array Ruby 1.9 has the Enumerator class, which can act like a lazy list I think. A good example is given in the documentation: fib = Enumerator.new { |y| a = b = 1 loop { y << a a, b = b, a + b } } p fib.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] Also, this is a nice trick: Infinity = 1.0/0 range = 5..Infinity p range.take(10) #=> [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] This one only works for consecutive values though. jeremy On Sun, Aug 1, 2010 at 8:56 PM, skim wrote: > Ruby Arrays dynamically expand as needed.  You can apply blocks to them to > return things like even numbers. > > array = [] > array.size # => 0 > array[0] # => nil > array[9999] # => nil > array << 1 > array.size # => 1 > array << 2 << 3 << 4 > array.size # => 4 > > array = (0..9).to_a > array.select do |e| >  e % 2 == 0 > end > > # => [0,2,4,6,8] > > Does this help? > > On Sun, Aug 1, 2010 at 17:45, Andrew Wagner wrote: > >> How can I get a lazy array in Ruby? E.g., in Haskell, I can talk about >> [1..], which is an infinite list, lazily generated as needed. I can also do >> things like "iterate (+2) 0", which applies whatever function I give it to >> generate a lazy list. In this case, it would give me all even numbers. >> Anyway, I'm sure I can do such things in Ruby, but can't seem to work out >> how. >> >