Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How can I create an iterator that works inside foreach() (updated)

by AnomalousMonk (Archbishop)
on Nov 05, 2022 at 00:24 UTC ( #11147975=note: print w/replies, xml ) Need Help??


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!

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:  <%-{-{-{-<

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
    Brilliant!!! The & prototype was the secret sauce! Thank you so much for your help.

    I'm embarrassed to see that it was on page 123 of High Order Perl and I missed it. An example with a foreach would have helped -- but MJD did such an awesome job with a lot of complicated topics in that book that there's no way I'm going to complain! I should have read the chapter more carefully.

    There's a web site called programming-idioms.org which provides a database of idioms implemented in different languages for easy comparison. The reason I asked about this is that I made a contribution to the idiom for generator functions but after additional testing realized it didn't work in a foreeach loop. I'll use your suggestion to update it.

    Here's a link to the programming idioms page I'll be modifying:

    Idiom #319 generator functions

      The & prototype was the secret sauce!

      Actually, I think wantarray is the secret sauce as it allows the iterator function to discriminate list vs. scalar context. :)


      Give a man a fish:  <%-{-{-{-<

      In the context of that website, it's possible that the intent is something more like the C-style for loop, in which case something like the following might be a better fit:

      sub range { my($start, $end) = @_; return sub { return undef if $start > $end; return $start++; }; } my $it = range(3, 5); for (my $value; $value = $it->(); ) { say $value }

      (But actual idiomatic perl would use a while loop here.)

      If the intent is to handle a list-style for loop, the "iterator" can be made much simpler by handling just that case, which I think is similar to the Ruby solution shown:

      sub list_range { my($start, $end) = @_; return sub { $start .. $end }; } my $lit = list_range(3, 5); for ($lit->()) { say $_ }

      In the current example perl code at the link, the "_upto" function is not needed, it can simply be replaced with "sub", as in my examples above: the sub keyword in this context yields an anonymous subroutine reference from the block that follows it, and Higher Order Perl should be telling you all about that. The comment "To work in a foreach each loop, inner sub upto must be predeclared to take an anonymous sub argument" is wrong - if we have a subroutine reference, $it->() will happily invoke it, whether it's in a for loop or not.

      Also, looking at the post on the other site, it occurs to me that you might mention the semipredicate problem with the posted code (see the end of my updated post).

      ... inner sub upto must be predeclared to take an anonymous sub argument; hence the (%).

      I don't understand this. Are upto (no leading underscore) and (%) typos? And shouldn't _upto be something like Iterator (for clarity)?


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147975]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2023-02-04 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (30 votes). Check out past polls.

    Notices?