Perl-Python-a-Day: Sorting

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Mon Oct 10 06:34:59 EDT 2005


Followup-To: comp.lang.scheme

"Xah Lee" <xah at xahlee.org> writes:

> Since this is frequently used, Python provides a somewhat shorter
> syntax for it, by specifying the column used as the ordering “key”.
[...]
> Because Python's implementation is not very refined , this specialized
> syntax is actually much speedier than the general form “lambda x, y:
> cmp(x[1],y[1])”. It is a burden on the programer to always use the
> “key” syntax idiosyncrasy if he is sorting a large matrix.

It's not only clearer for a human, but also faster in all good
implementations of all languages which support that, except when the
ordering function is very simple. It's called Schwartzian transform
and I wish more language designers and programmers knew about it.

http://en.wikipedia.org/wiki/Schwartzian_transform

I urge future SRFI authors to include it. The withdrawn SRFI-32 for
sorting didn't do that, and I can't find any other SRFI which deals
with sorting.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/



More information about the Python-list mailing list