Front page | perl.fwp |
Postings from August 2001
Re: Sorting in-place
Thread Previous
|
Thread Next
From:
Michael G Schwern
Date:
August 8, 2001 10:30
Subject:
Re: Sorting in-place
Message ID:
20010808133018.G918@blackrider
On Mon, Aug 06, 2001 at 01:21:12PM +0300, Ilmari Karonen wrote:
> so it will return the empty list in list context? Or one could just
> simply write
>
> return;
>
> instead.
Yes, that's what I should have used. I sometimes get flagitis.
Anyhow, the point is you can handle the accidentaly use of
"void-context" sort as last-evaluated-expression problem with a
return. Still, I think it will cause confusion.
There's nothing stopping anyone from writing a Sort::Inplace XS module
that exports both a sort_inplace() function and exports on request a
sort() that sorts inplace in void context. You could even go so far
as to override CORE::GLOBAL::sort so everywhere has this new sort().
I encourage someone to do so.
About sort() in scalar context. It appears perl already optimizes
this away to simply return the size of the array. However, it's not
safe to change this to sort in-place. Consider:
my @stuff = ();
sub foo {
...do some calculations and things...
return sort @stuff;
}
so it would normally be used like this:
my @whatever = foo(...);
but it could also be called like this:
my $how_many = foo(...);
not realizing foo() calls sort(). If we made sort() in scalar context
sort @stuff in-place, it would change the behavior of that code.
@stuff would inadvertently get sorted.
--
Michael G. Schwern <schwern@pobox.com> http://www.pobox.com/~schwern/
Perl6 Quality Assurance <perl-qa@perl.org> Kwalitee Is Job One
<Skrewtape> I've heard that semen tastes different depending on diet. Is that
true?
<Skrewtape> Hello?
<Schwern> Skrewtape: Hang on, I'm conducting research.
Thread Previous
|
Thread Next