develooper Front page | perl.perl5.porters | Postings from July 2004

Re: optimizing Short circuiting sort method based on [M]

Thread Previous | Thread Next
david nicol
July 21, 2004 16:02
Re: optimizing Short circuiting sort method based on [M]
Message ID:
On Wed, 2004-07-21 at 17:16, Dave Mitchell wrote:
> On Wed, Jul 21, 2004 at 04:58:54PM -0500, david nicol wrote:
> > How impossible is it to find out the size of an Lvalue array?  I'm
> > imagining defining the true value of  wantarray() function to be
> > a reference to the array that is to be filled by the result.
> > 
> > Is that possible?.
> Eh? what would wantarray return a ref to in
>     sub f { wantarray() ... }
>     ($a, $b, @c) = f();

an array of aliases to $a, $b, and $c:


 so that assigning to


will be the same thing as returning values.  Except then we need to
get out of the function somehow.  So maybe optimizing the stack-copies
out of returning would only happen when assignment to @{wantarray()}
happens in a return:

	return @{wantarray()} = qw/foo bar baz/;

Or better yet, assigning to @{wantarray()} or a slice of it would
imply returning.

This might get real confusing real fast, and I can't right away come up
with a use for the feature that wouldn't be more cleearly written using
 C<map> and doing explicit dispatch outside of the function.

It would be a powerful implementation-hiding abstraction

        sub f(){
            my $target = wantarray() or die 'usage: ($a, $b, $c) = f()';

            my @NeedsEffing = grep 
                                {effthisp $target->[$_]}

            @$target[@NeedsEffing] =
                   {effthis $_}

but what would it give us that we can't already get by making the
assignee one of the function parameters instead of an l-value?

        sub f(@){
            wantarray() or die 'usage: f($a, $b, $c)';

            my @NeedsEffing = grep {effthisp $_[$_]} (0..$#_);

            effthis( $_ ) for @_[@NeedsEffing];            

The real advantages are that access to details about the Lvalue allows 
expected-type-based polymorphism.  If we care about that.

For the purposes of implementing a truncated sort optimization, all we
care about is the length of the L-array, if any.  If that's not going to
be available, truncated sorting is best left to the unwritten
 Sort::Truncated module and however the invocation works it will take M,
or at least the target array, as a parameter.

david nicol
    "People used to be able to read my thoughts, but
it doesn't appear to work any more. Should I eat less cheese?"
                                               -- Elizabeth Woods

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About