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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.