From: Luke Blanshard Date: 2006-02-12T22:01:21+09:00 Subject: Re: [QUIZ] FasterGenerator (#66) James Edward Gray II wrote: > On Feb 10, 2006, at 8:24 AM, Pit Capitain wrote: > >> It would be interesting to add a testcase with an endless internal >> iterator. I don't know the new implementation of Generator, so I >> can't say whether an endless iterator should be supported. > > That's an interesting point we will certainly talk more about as we > start to see some solutions. Generator *can* handle endless > iterators. Quiz solvers can decide whether or not to limit themselves > with that additional requirement... More importantly than endless generators, it seems to me, is ones that depend on other shared state. The hardest part about generators is the fact that (to do them correctly) they must be suspended between calls to g.yield. Here's an illustration of what I mean: x = 1 g = Generator.new do |g| 5.times {|i| g.yield [x,i].max} end until g.end? x = 10 if g.current > 2 puts g.next end This outputs 1, 1, 2, 3, 10. The next question is, why is the API for this class so crappy? I would have expected the output to be 1, 1, 2, 10, 10. But creating the generator automatically advances to the first yield, and "next" advances to the next one while returning the previous one. This is just wrong. Perhaps a more interesting quiz would be BetterGenerator that looks like Python's upcoming PEP 342 (http://python.org/peps/pep-0342.html). Luke Blanshard