Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Schwartzian transform
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Comparison to other languages== Some other languages provide a convenient interface to the same optimization as the Schwartzian transform: * In [[Python (programming language)|Python]] 2.4 and above, both the {{mono|sorted()}} function and the in-place {{mono|list.sort()}} method take a {{mono|1=key=}} parameter that allows the user to provide a "key function" (like {{mono|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 {{mono|1=cmp=}} parameter that allowed the user to provide a "comparison function" was removed). Before Python 2.4, developers would use the lisp-originated decorate–sort–undecorate (DSU) idiom,<ref>{{cite web |title=How To/Sorting/Decorate Sort Undecorate |url=https://wiki.python.org/moin/HowTo/Sorting#The_Old_Way_Using_Decorate-Sort-Undecorate}}</ref> usually by wrapping the objects in a (sortkey, object) tuple. * In [[Ruby (programming language)|Ruby]] 1.8.6 and above, the {{mono|Enumerable}} abstract class (which includes {{mono|Array}}s) contains a {{mono|sort_by}}<ref name="Module: Enumerable">{{cite web |title=Module Enumerable |url=https://docs.ruby-lang.org/en/master/Enumerable.html#method-i-sort_by}}</ref> method, which allows specifying the "key function" (like {{mono|foo}} in the examples above) as a code block. * In [[D (programming language)|D]] 2 and above, the {{mono|schwartz Sort}} 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 (programming language)|Racket's]] core <code>sort</code> function accepts a <code>#:key</code> keyword argument with a function that extracts a key, and an additional <code>#:cache-keys?</code> requests that the resulting values are cached during sorting. For example, a convenient way to shuffle a list is {{code|2=racket|(sort l < #:key (λ (_) (random)) #:cache-keys? #t)}}. * In [[PHP (programming language)|PHP]] 5.3 and above the transform can be implemented by use of {{mono|array_walk}}, e.g. to work around the limitations of the unstable sort algorithms in PHP.<syntaxhighlight lang="php"> function spaceballs_sort(array& $a): void { array_walk($a, function(&$v, $k) { $v = array($v, $k); }); asort($a); array_walk($a, function(&$v, $_) { $v = $v[0]; }); }</syntaxhighlight> * In [[Elixir (programming language)|Elixir]], the {{mono|Enum.sort_by/2}} and {{mono|Enum.sort_by/3}} methods allow users to perform a Schwartzian transform for any module that implements the {{mono|Enumerable}} protocol. * In [[Raku (programming language)|Raku]], one needs to supply a comparator lambda that only takes 1 argument to perform a Schwartzian transform under the hood: <syntaxhighlight lang="Perl6">@a.sort( { $^a.Str } ) # or shorter: @a.sort(*.Str)</syntaxhighlight> would sort on the string representation using a Schwartzian transform, <syntaxhighlight lang="Perl6">@a.sort( { $^a.Str cmp $^b.Str } )</syntaxhighlight> would do the same converting the elements to compare just before each comparison. * In [[Rust (programming language)|Rust]], somewhat confusingly, the {{mono|slice::sort_by_key}} method does ''not'' perform a Schwartzian transform as it will not allocate additional storage for the key, it will call the key function for each value for each comparison. The {{mono|slice::sort_by_cached_key}} method will compute the keys once per element. * In [[Haskell (programming language)|Haskell]], the <code>sortOn</code> function from the base library performs a Schwartzian transform.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)