Skip to content
Snippets Groups Projects
Commit 1d5bc34e authored by Andy Yates's avatar Andy Yates
Browse files

Bringing in each() as a method on Iterator

parent 1c382bb2
No related branches found
No related tags found
No related merge requests found
...@@ -249,6 +249,30 @@ sub map { ...@@ -249,6 +249,30 @@ sub map {
}); });
} }
=head2 each
Example : $iterator->each(sub { print $_->name, "\n"; });
Description: Performs a full iteration of the current iterator instance.
Argument : a coderef which returns the desired transformation of each element.
$_ will be set locally set to each element.
Returntype : None
Exceptions : thrown if the argument is not a coderef
Caller : general
Status : Experimental
=cut
sub each {
my ($self, $coderef) = @_;
throw('Argument should be a coderef') unless ref $coderef eq 'CODE';
while($self->has_next()) {
local $_ = $self->next();
$coderef->($_);
}
return;
}
=head2 to_arrayref =head2 to_arrayref
Example : my $arrayref = $iterator->to_arrayref; Example : my $arrayref = $iterator->to_arrayref;
......
...@@ -123,5 +123,9 @@ $num = Bio::EnsEMBL::Utils::Iterator->new([1,2,3,4])->reduce(sub {$_[0] + $_[1]} ...@@ -123,5 +123,9 @@ $num = Bio::EnsEMBL::Utils::Iterator->new([1,2,3,4])->reduce(sub {$_[0] + $_[1]}
is($num, 20, "reduce with initial value calculated sum correctly"); is($num, 20, "reduce with initial value calculated sum correctly");
$num = 0;
Bio::EnsEMBL::Utils::Iterator->new([1..12])->each(sub { $num++; });
is($num, 12, 'each iterates over all elements');
done_testing; done_testing;
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment