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!
==Example== For example, to sort a list of files by their [[mac times|modification times]], a naive approach might be as follows: '''function''' naiveCompare(file a, file b) { '''return''' modificationTime(a) < modificationTime(b) } ''// Assume that sort(list, comparisonPredicate) sorts the given list using'' ''// the comparisonPredicate to compare two elements.'' sortedArray := sort(filesArray, naiveCompare) Unless the modification times are {{not a typo|memoized}} for each file, this method requires re-computing them every time a file is compared in the sort. Using the Schwartzian transform, the modification time is calculated only once per file. A Schwartzian transform involves the functional idiom described above, which does not use temporary arrays. The same algorithm can be written procedurally to better illustrate how it works, but this requires using temporary arrays, and is not a Schwartzian transform. The following example pseudo-code implements the algorithm in this way: '''for each''' file '''in''' filesArray insert array(file, modificationTime(file)) at end of transformedArray '''function''' simpleCompare(array a, array b) { '''return''' a[2] < b[2] } transformedArray := sort(transformedArray, simpleCompare) '''for each''' file '''in''' transformedArray insert file[1] at end of sortedArray
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)