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!
==The Perl idiom== The general form of the Schwartzian transform is: <syntaxhighlight lang="perl"> @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] or $a->[0] cmp $b->[0] } map { [$_, foo($_)] } @unsorted; </syntaxhighlight> Here <code>foo($_)</code> represents an expression that takes <code>$_</code> (each item of the list in turn) and produces the corresponding value that is to be compared in its stead. Reading from right to left (or from the bottom to the top): * the original list <code>@unsorted</code> is fed into a <code>map</code> operation that wraps each item into a (reference to an anonymous 2-element) array consisting of itself and the calculated value that will determine its sort order (list of item becomes a list of [item, value]); * then the list of lists produced by <code>map</code> is fed into <code>sort</code>, which sorts it according to the values previously calculated (list of [item, value] β sorted list of [item, value]); * finally, another <code>map</code> operation unwraps the values (from the anonymous array) used for the sorting, producing the items of the original list in the sorted order (sorted list of [item, value] β sorted list of item). The use of anonymous arrays ensures that memory will be reclaimed by the Perl garbage collector immediately after the sorting is done.
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)