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

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

by PUCKERING (Sexton)
on Nov 05, 2022 at 02:24 UTC ( [id://11147977]=note: print w/replies, xml ) Need Help??


in reply to Re: How can I create an iterator that works inside foreach() (updated)
in thread How can I create an iterator that works inside foreach()

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

  • Comment on Re^2: How can I create an iterator that works inside foreach() (updated)

Replies are listed 'Best First'.
Re^3: How can I create an iterator that works inside foreach() (updated)
by AnomalousMonk (Archbishop) on Nov 05, 2022 at 03:27 UTC
    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:  <%-{-{-{-<

Re^3: How can I create an iterator that works inside foreach() (updated)
by hv (Prior) on Nov 05, 2022 at 04:04 UTC

    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.

Re^3: How can I create an iterator that works inside foreach() (updated)
by AnomalousMonk (Archbishop) on Nov 05, 2022 at 03:55 UTC

    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://11147977]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found