Comparison To Other Languages
Some other languages provide a convenient interface to the same optimization as the Schwartzian transform:
- In Python 2.4 and above, both the sorted function and the in-place list.sort method take a key= parameter that allows the user to provide a "key function" (like foo in the examples above). In Python 3 and above, use of the key function is the only way to specify a custom sort order (the previously-supported comparator argument was removed). Before Python 2.4, developers would use the lisp-originated Decorate-Sort-Undecorate (DSU) idiom, usually by wrapping the objects in a (sortkey, object) tuple.
- In Ruby 1.8.6 and above, the Enumerable abstract class (which includes Arrays) contains a sort_by method which allows you to specify the "key function" (like foo in the examples above) as a code block.
- In D 2 and above, the schwartzSort function is available. It might require less temporary data and be faster than the Perl idiom or the decorate-sort-undecorate idiom present in Python and Lisp. This is because sorting is done in-place and only minimal extra data (one array of transformed elements) is created.
- Racket's core
sort
function accepts a#:key
keyword argument with a function that extracts a key, and an additional#:cache-keys?
requests that the resulting values are cached during sorting. For example, a convenient way to shuffle a list is(sort l < #:key (λ (_) (random)) #:cache-keys? #t)
.
Read more about this topic: Schwartzian Transform
Famous quotes containing the words comparison to, comparison and/or languages:
“In comparison to the French Revolution, the American Revolution has come to seem a parochial and rather dull event. This, despite the fact that the American Revolution was successfulrealizing the purposes of the revolutionaries and establishing a durable political regimewhile the French Revolution was a resounding failure, devouring its own children and leading to an imperial despotism, followed by an eventual restoration of the monarchy.”
—Irving Kristol (b. 1920)
“But the best read naturalist who lends an entire and devout attention to truth, will see that there remains much to learn of his relation to the world, and that it is not to be learned by any addition or subtraction or other comparison of known quantities, but is arrived at by untaught sallies of the spirit, by a continual self-recovery, and by entire humility.”
—Ralph Waldo Emerson (18031882)
“No doubt, to a man of sense, travel offers advantages. As many languages as he has, as many friends, as many arts and trades, so many times is he a man. A foreign country is a point of comparison, wherefrom to judge his own.”
—Ralph Waldo Emerson (18031882)