Schwartzian Transform - Comparison To Other Languages

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 and/or languages:

    We teach boys to be such men as we are. We do not teach them to aspire to be all they can. We do not give them a training as if we believed in their noble nature. We scarce educate their bodies. We do not train the eye and the hand. We exercise their understandings to the apprehension and comparison of some facts, to a skill in numbers, in words; we aim to make accountants, attorneys, engineers; but not to make able, earnest, great- hearted men.
    Ralph Waldo Emerson (1803–1882)

    The less sophisticated of my forbears avoided foreigners at all costs, for the very good reason that, in their circles, speaking in tongues was commonly a prelude to snake handling. The more tolerant among us regarded foreign languages as a kind of speech impediment that could be overcome by willpower.
    Barbara Ehrenreich (b. 1941)