On Wed, Jul 07, 2004 at 11:50:16PM -0400, JOSEPH RYAN wrote: > > > ----- Original Message ----- > From: Larry Wall <[EMAIL PROTECTED]> > Date: Wednesday, July 7, 2004 11:25 pm > Subject: Re: push with lazy lists > > > On Fri, Jul 02, 2004 at 09:32:07PM -0500, Dan > Hursh wrote: > > : how 'bout > > : > > : @x = gather{ > > : loop{ > > : take time > > : } > > : } # can this be @x = gather { take time loop } > > : push @x, "later"; > > : say pop @x; # "later" > > > > Can probably be made to work right. > > > > : say pop @x; # heat death? > > > > Yes. > > > > : say @x[rand]; # how about now? > > > > Well, that's always going to ask for @x[0], which > isn't a problem. > > However, if you say rand(@x), it has to calculate > the number of > > elements in @x, which could take a little while... > > Why would the second pop be a heat death, and why > would rand always return 0?
To answer the latter first, rand (with no arguments) returns a number greater than or equal to 0 and less than 1 which when used as an index into an array gets turned into a 0. As to why the second pop would take forever, I'd imagine that in order to pop the last item from the array, all of the elements must first be generated (i.e. we lose all laziness). And unless we have some magic for generating them from either end, it'll start at the begining and continue until the end, then stop before it ever does the pop. :-) -Scott -- Jonathan Scott Duff [EMAIL PROTECTED]