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)
“The difference between human vision and the image perceived by the faceted eye of an insect may be compared with the difference between a half-tone block made with the very finest screen and the corresponding picture as represented by the very coarse screening used in common newspaper pictorial reproduction. The same comparison holds good between the way Gogol saw things and the way average readers and average writers see things.”
—Vladimir Nabokov (18991977)
“The trouble with foreign languages is, you have to think before your speak.”
—Swedish proverb, trans. by Verne Moberg.