Here's one way. The iterator in this example is very simple: it has no way to reset or reinitialize once it is exhausted, and it will only reach exhaustion in a while-loop or equivalent
(update: i.e., in scalar-context invocation).
c:\@Work\Perl>perl
use strict;
use warnings;
# use Data::Dump qw(dd); # for debug
sub Iterator (&) { return $_[0]; } # syntactic sugar per mjd (Dominus
+)
sub irange_limited {
my ($start, $end) = @_;
return Iterator {
return if $start > $end;
return wantarray ? $start .. $end : $start++;
};
}
my $iter = irange_limited(3, 5);
for my $n ($iter->()) {
print "for loop: $n \n";
}
while (my $n = $iter->()) {
print "while loop: $n \n";
}
^Z
for loop: 3
for loop: 4
for loop: 5
while loop: 3
while loop: 4
while loop: 5
Update: Here's a slightly more sophisticated iterator. It always returns the full range when invoked in list context, and it will automatically reset when exhausted upon invocation in scalar context. Note, however, that list and scalar context invocations are independent of each other!
sub irange_limited {
my ($start, $end) = @_;
my $n = $start;
return Iterator {
return
wantarray ? $start .. $end :
$n > $end ? ($n = $start, ()) :
$n++
;
};
}
(Update: Also note that this solution has a semipredicate problem. The range -1, 1 will result in a value of 0 (false) that will terminate a while-loop prematurely. A further defined test is needed in the while-loop condition because undef (also false) flags iterator
exhaustion. (Update: There are also other solutions to this problem :))
Give a man a fish: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|