in reply to How can I create an iterator that works inside foreach()
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!
(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 :))sub irange_limited { my ($start, $end) = @_; my $n = $start; return Iterator { return wantarray ? $start .. $end : $n > $end ? ($n = $start, ()) : $n++ ; }; }
Give a man a fish: <%-{-{-{-<
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How can I create an iterator that works inside foreach() (updated)
by PUCKERING (Sexton) on Nov 05, 2022 at 02:24 UTC | |
by AnomalousMonk (Archbishop) on Nov 05, 2022 at 03:27 UTC | |
by hv (Prior) on Nov 05, 2022 at 04:04 UTC | |
by AnomalousMonk (Archbishop) on Nov 05, 2022 at 03:55 UTC |
In Section
Seekers of Perl Wisdom