Perl-Python-a-Day: Sorting
Lasse Vågsæther Karlsen
lasse at vkarlsen.no
Thu Oct 13 01:44:00 EDT 2005
Xah Lee wrote:
> Sorting in Perl
>
> In Perl, to sort a list, do like this:
>
> @li=(1,9,2,3);
> @li2 = sort {$a <=> $b} @li;
> print join(' ', @li2);
>
>
> In Perl, sort is a function, not some Object Oriented thing. It returns
> the sorted result as another list. This is very simple and nice.
Like the sorted function in Python ?
li2 = sorted(li)
you can also specify a key and a cmp function if you need to.
<snip>
> In this way, the cost of the internals of the ordering function is
> avoided. (it runs on each list element once) However, your huge list is
> copied 1 extra time. So, there are pros and cons. Because this work
> around is very complex in both its semantics and syntax, it has
> acquired a coolness factor among Perl coders, and is given the name
> Schwartzian Transform.
That's about the worst explanation I've seen of Scwartzian Transform
anywhere. Why deviate from what *every other source* does? Give an
example, like the typical one of sorting filenames based on the size of
the file they refer to. If you get the size in the comparison function,
you can potentially get the size (which is a rather costly operation)
many times for each file. Instead, you prefetch it once for each
filename and store that in the list, and use the size as the key for
sorting.
So while your explanation is technically correct, you lost some of the
explaining factors. In other words, using Google lets anyone wanting to
know about "Schwartzian Transform" find *much* better explanations than
your mumbo jumbo.
But that's no surprise, you're Xah Lee anyway, loosing out is your
middle name.
BTW, this way of doing the sort is nothing special for Perl, that
construct can be seen many places simply because it does not fight any
of Perls "problems" with sorting, instead it overcomes a common problem
with sorting in any language.
>
> It is interesting to note what compiler flaws can do to imperative
> languages and its people. In Python, the language syntax is tainted. In
tainted how?
> Perl, a complex construct is invented. In both camps, the basic
invented how?
> mathematics of sorting and its implementation aspects are completely
> belied.
belied how?
It's interesting to note that these "fact posts" of yours are nothing
bud badly worded opinions.
--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse at vkarlsen.no
PGP KeyID: 0x2A42A1C2
More information about the Python-list
mailing list