[Melbourne-pm] map -- with an iterator

Tulloh, David david.tulloh at AirservicesAustralia.com
Thu Apr 22 01:18:25 PDT 2010


> 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($_) });

You should be able to add a foreach member to the iterator class to
enable this.  Something like:

sub foreach {
	my ($this, $coderef) = @_;
	while (my $item = $this->next) {
		&$coderef($item);
	}
}
$iterator->foreach( sub { say "Hi" } );

Same basic process, but hidden.


Or the process from your original email was

my $iterator = Third::Party::Module->foo;
use_magic_wand($iterator);
$iterator.foreach(sub { action $_ });

Where use_magic_wand is to subclass $iterator.


David
http://david.tulloh.id.au/


More information about the Melbourne-pm mailing list