http://qs321.pair.com?node_id=544134

TedYoung has asked for the wisdom of the Perl Monks concerning the following question:

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. :-)