Garrett Goebel wrote:
> 
> Let's see if I've got this straight. To paraphrase Damian's
> RFC 77 for reduce:
> 
> If a C<grep>'s block were terminated by a call to C<last>,
> grep immediately returns the last block value (i.e. C<undef>
> on the first block call, $_[0] otherwise). Or maybe that's @_ 
> otherwise... 

No; I think you have it right down here:


> The end result would be that if C<last> occured the first time that
> grep's block was evaluated, an empty list would be returned. 


> I don't know how grep works internally. I don't know if grep pushes
> elements into @a one at a time, or if it returns a finished list of 
> elements which pass the conditional block. If it is the latter as I
> assume, a short-circuited grep would return a list of all the
> elements of @b that had passed through to that point. 

Actually, that's what it would do regardless of the internal
implementation.  Either way, the result (assigned to @a) is the
list of items which passed the condition, up to the point the
loop was terminated via "last".


> > So: should 
> > 
> >     scalar grep { 1 and last } LIST
> > 
> > return 1, if LIST is not empty, 

Yes.  The first item in LIST passed the condition,
and then the loop terminated.


> >     scalar grep { 0 or last } LIST
> > 
> > return 0?

Yep.

These are scalar contexts; we're asking for the number of
items in the result.

-- 
John Porter

        We're building the house of the future together.

Reply via email to