http://qs1969.pair.com?node_id=427875


in reply to Re^5: Better mousetrap (getting top N values from list X)
in thread Better mousetrap (getting top N values from list X)

Okay. I see what you are saying now. The slice across the return from sort generates a lazy list.

So the code:

my @data = getdata( 100000 ); my @topN = ( sort( @data ) )[ 1 .. 10 ]; undef( @data ); for my $top ( @topN ) { ## << COrrect per 427878 ## do something with $top }

Passes the whole list of data to sort, (which takes a copy of it in case we modify or discard it) and returns an iterator which gets bound to @top, so that each time we access the next element of @top in the for loop, the iterator is called via the magic of tieing, and the copy of the 100,000 elements that we passed to sort gets scanned to produce the next of our 5 return values.

Lazy huh?!


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.