We could build a circular list to retrieve a list of consecutive seasons, but as I discovered
Iterator::Simple, Here follows a stupid example of its usage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Data::Dumper::Perltidy; | |
use Getopt::Long; | |
use Iterator::Simple qw(iterator); | |
use Perl6::Say; | |
use Pod::Usage; | |
use autodie; | |
use strict; | |
use warnings; | |
sub man {#{{{ | |
pod2usage( | |
-exitval => 1, | |
-verbose => 2 | |
); | |
}#}}} | |
# main | |
GetOptions ( | |
'man' => \&man, | |
); | |
sub seasons { | |
my @allSeasons = qw(spring summer autumn winter); | |
my $i=0; | |
iterator { | |
return $allSeasons[ $i++ % @allSeasons ]; | |
} | |
} | |
my $it = seasons(); | |
my $i=0; | |
while (my $now = $it->next) { | |
say $now; | |
last if $i++ == 10; | |
} | |
__END__#{{{ | |
=head1 NAME | |
=head1 SEE ALSO | |
=head1 SYNOPSIS | |
=head1 DESCRIPTION | |
=head1 AUTHOR | |
Raimon Grau Cuscó <raimonster@gmail.com> | |
=head1 Credits | |
=cut | |
# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ###}}} |
We can also wrap an iterator, "á la" stream (I'm reading the stream chapter in SICP, and nowadays, I can only think in streams :p ), delaying the evaluation of the wrapping until the given elements are needed using imap. We also have ifilter, igrep, and so.
Here's a code that wraps (or decorates, in python slang IIRC) our iterator modifying the string returned, and it limits the iterator to 6 seasons.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Data::Dumper::Perltidy; | |
use Getopt::Long; | |
use Iterator::Simple qw(iterator imap); | |
use Perl6::Say; | |
use Pod::Usage; | |
use autodie; | |
use strict; | |
use warnings; | |
sub man {#{{{ | |
pod2usage( | |
-exitval => 1, | |
-verbose => 2 | |
); | |
}#}}} | |
# main | |
GetOptions ( | |
'man' => \&man, | |
); | |
sub seasons { | |
my @allSeasons = qw(spring summer autumn winter); | |
my $i=0; | |
iterator { | |
return if $i>5; | |
return $allSeasons[ $i++ % @allSeasons ]; | |
} | |
} | |
my $it = seasons(); | |
my $s = imap {"now it's $_"} $it; | |
while (my $now = $s->next) { | |
say $now; | |
} | |
__END__#{{{ | |
=head1 NAME | |
=head1 SEE ALSO | |
=head1 SYNOPSIS | |
=head1 DESCRIPTION | |
=head1 AUTHOR | |
Raimon Grau Cuscó <raimonster@gmail.com> | |
=head1 Credits | |
=cut | |
# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ###}}} |
No hay comentarios:
Publicar un comentario