[Melbourne-pm] map -- with an iterator
Shlomi Fish
shlomif at iglu.org.il
Thu Apr 22 01:17:31 PDT 2010
On Thursday 22 Apr 2010 10:39:32 Toby Corkindale wrote:
> On 22/04/10 16:54, Sam Watkins wrote:
> > On Thu, Apr 22, 2010 at 03:59:23PM +1000, Toby Corkindale wrote:
> >> Hey all,
> >>
> >> You're all familiar with the map operator, used like so:
> >> my @results = map { transform $_ } @input;
> >>
> Eg. Say that the iterator returns a 10 Mbyte object every time you call
> ->next, and that you have one million items to get through.
>
> If you have to put them all into a temporary @out array first, then you
> chew up vast amounts of ram. Whereas if your iterator/map combo is
> smarter, then you only use 10 Mb at a time. (If you accumulating
> results, then you'll still build up an array of results, of course. But
> it might be a lot smaller.. or at the very least, it's only one copy of
> the results, not two.)
>
> So, currently one tends to do:
> while (my $item = $iterator->next) {
> do_stuff_to($item);
> }
>
> But it would be nice, syntactically, to say
> $iterator->foreach(sub { do_stuff($_) });
>
I'm not sure you can do that while calling your method "foreach" (because
foreach is a reserved keyword.), but you can do it while calling it "for_" for
example :
[code] # Untested
sub for_
{
my $self = shift;
my $callback = shift;
while (my $item = $iterator->next())
{
local $_ = $item;
$callback->($item);
}
}
[/code]
There are some modules for implementing iterators like that on CPAN:
* http://search.cpan.org/dist/Pipe/
* http://search.cpan.org/dist/HOP-Stream/
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
What does "Zionism" mean? - http://shlom.in/def-zionism
Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
More information about the Melbourne-pm
mailing list