From: Nat Pryce Date: 2002-11-19T23:44:32+09:00 Subject: Re: Thoughts on generators On Tue, 2002-11-19 at 12:49, Martin DeMello wrote: > ....it might be useful to see where we *can* efficiently write > > g = SimpleGenerator.new(a) # should be space-efficient > i = g.next # should be O(1) and cheap > > and where the generator iterates over the sequence nondestructively, > so that it is threadsafe. One could use g.val rather than g.next, > depending on how you think of it. > > SimpleGenerator.new would most likely need to be a factory that > instantiated more specific per-type generators. Some easy examples for > generator-generating objects: > - arrays and other structures that support a[i] where i is an integer > (initialize sets @i = -1, and .next does an @i+=1, @array[@i].dup) > - functions on integers (.next returns f(@i)) > - sequences where f[i+1] = g(f[i], f[i-1], ..) and which don't depend > on too large a history (the Fibonacci sequence, e.g., or a random > number generator) > - generator-only structures like unbounded lists and circular arrays If the behaviour of SimpleGenerator.new(a) depends on the type of a -- e.g. is polymorphic with respect to a -- then it should really be a method defined by a. E.g. your example would be reformulated as: g = a.generator i = g.next Cheers, Nat. -- Nat Pryce B13media