> -----Original Message-----
> From: Larry Wall [mailto:[EMAIL PROTECTED]
> Sent: Thursday, September 02, 2004 8:41 PM
> To: Perl6
> Subject: Re: Reverse .. operator
> 
> On Thu, Sep 02, 2004 at 08:34:22PM -0400, Joe Gottman wrote:
> : Is there similar shorthand to set @foo = (5, 3, 3, 2, 1) ?  I know you
> can
> : go
> :
> :             @foo = reverse (1 ..5);
> :
> : but this has the major disadvantage that it cannot be evaluated lazily;
> : reverse has to see the entire list before it can emit the first element
> of
> : the reversed list.
> 
> I don't see any reason why it can't be evaluated lazily.  The .. produces
> a range object that gets shoved into the lazy list that gets bound to
> the slurp array of reverse().  If you pop that, there's no reason it
> couldn't go out and ask the end of the lazy list for its last element.
> Just have to make .. objects smart enough to deal off either end of
> the deck.

   I get it.  One way to implement this would to give the .. object a
.reverse member iterator that lazily iterates from right to left, and have
the reverse subroutine call the .reverse member on the list that was passed
to it (if this member exists).  The advantage of this is that it can be
extended for other types, or even to arrays returned from functions.  For
instance,
 
    multi sub grep(Code $f, [EMAIL PROTECTED] does reverse)  returns (Array does
reverse {grep $f, @array.reverse;}) #reverse input

   multi sub map(Code $f, [EMAIL PROTECTED] does reverse) returns (Array does reverse
{map {reverse $f($_)} @array.reverse;}) #reverse input and result of each
call to $f

If it isn't possible to overload a multi sub on a .does property, we can
achieve the same effect by creating a ReversableArray class that inherits
from Array and overloading on that.

Joe Gottman



Reply via email to