CONCEPT
Schwartzian Transform
Schwartzian Transform
- An efficient algorithm and programming idiom used in Perl and other languages to optimize sorting a list of elements based on an expensive key-calculation function.
History and Origin
- Usenet Posting (1994): I originally demonstrated the idiom in a post to Usenet (
comp.unix.shell/comp.lang.perl) on December 16, 1994, to solve a sorting problem, noting that I was "speaking with a lisp in Perl." - Naming: The term "Schwartzian transform" was coined by Tom Christiansen in a follow-up response. He also jokingly proposed "The Black Transform" (a pun on the German meaning of Schwartz), but the original name became industry standard.
- Lisp Roots: The construct is a direct port of the classic "decorate-sort-undecorate" pattern from Lisp.
- Usenet Posting (1994): I originally demonstrated the idiom in a post to Usenet (
How It Works
- The transform optimizes sorting by ensuring that the sort key calculation is executed exactly once per list element instead of $O(N \log N)$ times.
- It follows a three-step functional pipeline (typically using a nested
map-sort-mapstructure): 1. Map (Decorate): Transform the list of values into a list of anonymous array references, each containing the original element and the computed sort key. 2. Sort: Sort the array of references based on the computed sort key. 3. Map (Undecorate): Extract the original elements from the sorted array references.
What links here
These facts are as Randal recalls them, but much time has passed for most of this. If you find a factual error, please email realmerlyn@gmail.com.