In message <[EMAIL PROTECTED]>
          Nathan Wiger <[EMAIL PROTECTED]> wrote:

> For example, in Perl you have for a long time been able to do this:
>
>    ($one, $two) = grep /$pat/, @data;
>
> However, what currently happens is grep goes to completion, then
> discards possibly huge amounts of data just to return the first two
> matches. For example, if @data was 20,000 elements long, you could
> potentially save a good chunk of time if you only had to return the
> first and/or second match, rather than finding 1000 only to throw 998
> away.

This could fall out of using iterators in the core but without
grep itself having to know anything about the left hand side.

Basically the @data in the argument list would cause an array
iterator to be pushed on the stack and the grep op would then
pull that and the matcher off the stack and create a new more
sophisticed iterator containing references to both the pattern
and the original iterator which it would push back on the stack.

The assignment op would then pull the iterator off and step
through it until it had satisfied all it's assignment targets
at which point it could be discarded without stepping through
any remaining elements.

The only problem with this scheme (and indeed I suspect with
yours) is if the match expression has a side effect. This is
even more of a problem when trying to apply the same optimisation
to map because of the widespread use of map in a void context
to apply a side effect to the elements.

Tom

-- 
Tom Hughes ([EMAIL PROTECTED])
http://www.compton.nu/
...The two great tragedies in life: not getting what one wants and getting it.

Reply via email to