Your browser does not support our blog javascript

iterator

search for more blogs here

 
Home - Take this blog! - Get your Author's Pass Here - Submit Comments Below

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!

From:   Website:
Subject:   Code:
Message:


   

 


 

 

 





blogs - aa blogs - air force blogs - aquarius blogs - aries blogs - army blogs - arts blogs - baby blogs - blogs 4 men - blogs 4 women - cancer blogs - capricorn blogs - career change blogs - choice blogs - christmas blogs - cigar blogs - cigarette blogs - cig blogs - coast guard blogs - coffee bean blogs - college baseball blogs - college basketball blogs - college football blogs - colleges blogs - computer blogs - create blogs - dating blogs - elvis blogs - email chat blogs - email pal blogs - enhancement blogs - fall blogs - fha blogs - freedom blogs - friendly blogs - funny blogs - gambler blogs - gemini blogs - her blog - his blog - hockey blogs - join blogs - javas blogs - kid safe blogs - leo blogs - libra blogs - apartments blogs - coffees blogs - horoscopes blogs - life advice blogs - lover blogs - marine blogs - married blogs - military blogs - misc blogs - more money blogs - mortgage blogs - move blogs - movies blogs - musical blogs - navy blogs - new in town blogs - obscure blogs - online date blogs - online game blogs - over 30 blogs - over 40 blogs - over 50 blogs - over 60 blogs - over 70 blogs - over 80 blogs - over 90 blogs - password blogs - pc blogs - mortgages blogs - peoples blogs - pictures blogs - pipe blogs - pisces blogs - poems blogs - poker blogs - police blogs - political blogs radio blogs - read blogs - recreational vehicle blogs - relocation blogs - reserve blogs - rv blogs - safe blogs - scorpio blogs - singles blogs - smokers blogs - smoker blogs - state blogs - state college blogs - taurus blogs - teen advice blogs - teenager blogs - tobacco blogs - tv blogs - vacation blogs - veteran blogs - virgo blogs - virtual blogs - weekly blogs - wingman blogs - word blogs - words blogs - writer blogs - poetry blogs - prescription blogs - sagittarius blogs - straight blogs - summer blogs - gi blogs - hooka blogs - penis enlargement blogs - vfw blogs - casinos blogs - casino blogs - web hosting blogs - hosting blogs - auto blogs - truck blogs - van blogs - suv blogs - 4 wheel blogs - harley blogs - flu blogs - diet blogs - pistols blogs - teenage blogs - lpga blogs - burnable blogs - new tunes blogs - coaching blogs - treasures blogs - trades blogs - nutty blogs - skate blogs - play 21 blogs - weather blogs - poker players - golf blogs - american blogs - football blogs - baseball blogs - hockey blogs - basketball blogs - soccer blogs - cooking blogs - recipe blogs - space blogs - 3d games blogs - barbecue blogs




blogs home