Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

So, I finally bought a copy of HOP. In it, the author uses anonymous subs as iterators. The details of this aren't too important for this discussion. He then introduces a function called igrep, which applies a filter to an iterator, much like grep, but one that is evaluated lazily, in the tradition of an iterator.

So, here is an example implementation:

# Syntax sugar sub Iterator (&) { $_[0] } # Takes a sub followed by an iterator sub igrep (&$) { my ($check, $iterator) = @_; return Iterator { # While the source iterator is not exhausted while (defined( local $_ = $iterator->() )) { return $_ if $check->(); } return undef; # we are exhausted } }

The author later introduces some examples of iterators that return lists on each invocation, instead of scalars. To account for this, he starts using a hypothetical igrep_l function that works with lists instead of a scalar. Not hard to implement, but I feel it gets ugly to have different functions for different contexts, especially iterators whose return value is dependant on context.

So, finally the question: how can this igrep function effectively propagate the calling context to the iterator? Here is something that I cobbled together. I was wondering if anyone had any simpler ideas. Note: the sub passed to igrep will have @_ as the param (in a scalar context) or params (in a list context) just retuned from the iterator. $_ is aliased to the first element returned by the iterator for the simple cases where an iterator is always called in a scalar context, or always returns a scalar.

#!perl -l sub Iterator (&) { $_[0] } sub igrep (&$) { my ($check, $iterator) = @_; sub { local *_; my @data; my $wantarray = wantarray; while(@data = ($wantarray ? $iterator->() : scalar $iterator-> +())) { *_ = \ $data[0]; return $wantarray ? @data : $data[0] if $check->(@data); } return undef; } }

So, it seems like there is a lot of checks to wantarray here. An alternative would be to have a giant if (wantarray) dividing the sub into two implementations.

BTW, while reading this book made me think to ask it, I have actually had this problem come up in other domains. I can't remember where I have done before, but I do remember using a similar strategy.

Some other notes: I have intentionally not used strict or warning for the sake of brevity and clarity in these examples. Also, for the same reasons, I am not worried about the void context.

Well, I hope this question has interested more people than it has bored. :-)

Ted Young

($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

In reply to Preserving Calling Context by TedYoung

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found