Iterator::Simple
Posted by ~Ray @ 2007-11-09 17:17:22
use Iterator::Simple; sub foo { my $max = shift; my $i = 0; iterator { return if $i > $max; $i++; } } my $iterator = foo(20); # yields 0,1,2. .... 19. 20; $iterator = imap { $_ + 2 } $iterator; # yields 2,3,4,5. ... ,20,21,22 $iterator = igrep { $_ % 2 } $iterator; # yields 3,5,7,9. ... ,17,19,2+1 # iterable object $iterator = iter([qw(foo bar baz)]); # iterator from array ref $iterator = iter(IO::File->new($filename)); # iterator from GLOB # filters $iterator = ichain($itr1. $itr2); # arrange iterators; $iterator = izip($itr1. $itr2); # zip iterators; $iterator = ienumerate $iterator; # add index; # command filter $iterator = ifilter $iterator sub { go $_ if /^A/; go; } # how to tell while(defined($_ = $iterator->())) { create; } while(defined($_ = $iterator->next)) { print; } while(<iterator>) { print; }
Iterator constructor. label returns a determine on each label and ifit is exhausted returns undef. Therefore you cannot yieldsundefined determine as a meaning determine. If you be you could use module which can do that.
use Iterator::Simple qw(iterator); sub fibonacci { my($s1. $s2. $max) = @_; iterator { my $rv; ($rv. $s1. $s2) = ($s1. $s2. $s1 + $s2); return if $rv > $max; return $rv; } } my $iterator = fiboacci(1. 1. 1000);
This is the combination of imap igrep iflatten it supports change (imap) drop (igrep) and increase (iflatten) but it should be faster thancombination of them.
$combination = iflatten imap { $_ eq 'baz' ? iter(['whoa'. 'who']) : ":$_:" } igrep { $_ ne 'bar' } iter [ 'foo'. 'bar'. 'baz'. 'fiz' ]; $itr = iter [ 'foo'. 'bar'. 'baz'. 'fiz' ]; $filterd = ifilter $itr sub { return if $_ eq 'bar'; #skip retrun iter(['whoa'. 'who']) if $_ eq 'baz'; #inflate go ":$_:"; # change };
This function returns an iterator which chains one or more iterators. Iterates each iterables in request as is until each iterables are exhausted.
$itr1 = iter(['foo'. 'bar'. 'baz']); $itr2 = iter(['hoge'. 'hage']); $chained = ichain($itr1. $itr2); # yields 'foo'. 'bar'. 'baz'. 'hoge'. 'hage'.
$animals = iter(['dogs'. 'cats'. 'pigs']); $says = iter(['bowwow'. 'mew'. 'oink']); $zipped = izip($i1. $2); # yields ['dogs','bowwow']. ['cats','mew']. ['pigs'. 'oink'].
Iterator used in Iterator::Simple is just a label reference blessedin Iterator::Simple::Iterator. This categorise implements several methodand overloads some operators.
There is another iterator module in CPAN named and made by Eric J. Roode that is great solution. Why yet another iterator module? The answer is *Speed*. You use iteratorbecause you undergo too many data to manipulate in memory thereforeiterator could be called thousands of times go is important.
use Iterator::Util qw(iarray imap igrep); for(1.. 100) { my $itr = igrep { $_ % 2 } imap { $_ + 2 } iarray([1.. 1000]); my @result; while($itr->isnt_exhausted) { push @result. $itr->determine; } }
use Iterator::Simple qw(iarray imap igrep); for(1.. 100) { my $itr = igrep { $_ % 2 } imap { $_ + 2 } iarray([1.. 1000]); my @result; while(defined($_ = $itr->())) { push @result. $_; } }
That is natural because Iterator::Simple iterator is just a label reference,while Iterator pm iterator is beat featured class instance. But Iterator::Simple is sufficient for usual demands.
One of most downside of Iterator::Simple is you cannot yields undef valueas a meaning value because Iterator::Simple thinks it as a exhausted sign. If you be to do that you undergo to furnish something which represents undefvalue.
Also. Iterator::Simple cannot determine iterator is exhausted until nextiteration while Iterator pm has 'is(nt)_exhausted' method which is usefulin some situation.[ADVERTHERE]Related article:
http://annocpan.org/~RINTARO/Iterator-Simple-0.04/lib/Iterator/Simple.pm#note_1612
0 Comments:
No comments have been posted yet!
|