On Jun 14, Evgeny Goldin (aka Genie) said:
>Is there any module implementing lazy lists ? ( similar to "infinite
>lists" of Scheme or "sequences" of ML )
>
>I've coded a small prototype of this thing using tied array, so
>it's good time to stop or dig into further development ..
It sounds like what you want is a self-generating list. Mark-Jason
Dominus just mentioned this technique in a talk he did yesterday at YAPC,
so it's fresh in my mind.
You probably want to make some sort of iterator. It will return a value,
and then use the generator function to get its next value for the next
time you call it.
This is going to be using closures, so you might want to read up on some
documentation (start at perldoc -q closure, and work from there).
sub make_stream {
my ($val, $generate) = @_;
$generate ||= sub { $val + 1 };
return sub {
my $x = $val;
$val = $generate->();
return $x;
};
}
Now you can make streams. The default stream is to just keep adding 1 to
the previous value.
$counter = make_stream(1); # integers starting at 1
$rand_ints = make_stream( # random integers from 1 to 10
1 + int rand 10,
sub { 1 + int rand 10 },
);
# and so on
Then to use them, you can just do:
while (1) {
$next_val = $counter->();
# ...
}
Hope this helps! :)
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **