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

Re: Short circuiting sort

From:
Glenn Linderman
Date:
June 20, 2004 12:47
Subject:
Re: Short circuiting sort
Message ID:
40D5EA38.40008@nevcal.com
On approximately 6/19/2004 9:08 PM, came the following characters from
the keyboard of David L. Nicol:

> Nick Ing-Simmons wrote:
> 
>> (I would still like sort in scalar context to return min or max value
>> - don't care which because one can change cmp function to get the other.)
>>
> 
> Currently
> 
>       ($first) = sort @ary;
> 
> gives the first of the sorted list, after doing a mess of extra work.  
> If C<sort> were to
> take a hint indicating how many results are going to be used, similar to 
> the fourth
> argument of C<split> but determined by parsing appropriate l-values, 
> sort algorithms
> that ignore the rest of the array could be used. 

So, in the situation where you have a countable number of parameters as 
the target of the sort, such as your example, or

   ($first, $second, $third, $fourth) = sort @ary;  # only sort 4 entries

   @most[ 0 .. 6 ] = sort @ary; # only sort 7 entries

the compiler could derive the hint, in a similar manner as it presently 
(recent feature) derives the in-place optimization.

> * It'll make Randall Schwartz call my C coding "Amazing" which I can use on
> the back covcer of my paperback

If you put it on the back cover of your paperback, you might want to 
spell his name correctly.  :)

-- 
Glenn -- http://nevcal.com/
===========================
The best part about procrastination is that you are never bored,
because you have all kinds of things that you should be doing.




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About