▲ | The history of the Schwartzian Transform (2016)(perl.com) | ||||||||||||||||||||||||||||||||||||||||
53 points by mooreds 9 days ago | 19 comments | |||||||||||||||||||||||||||||||||||||||||
▲ | RodgerTheGreat 5 days ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||
I wrote a blog post some time ago which contrasts the "Schwartzian transform" with the slightly clearer alternative used by most APL-family languages and (in my opinion) an even cleaner approach which is possible in languages with a first-class table type: https://beyondloom.com/blog/rankingoffruits.html | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
▲ | oofabz 5 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
I find it interesting that the transform was controversial in the '90s. Today, it seems like a normal solution to the problem to me, and the controversy seems silly. I have much experience with the map function from Javascript. It is too simple to be objectionable. But in the '90s, I would also have had trouble understanding the transform. Lambdas/closures were unheard of to everyone except Lisp dweebs. Once I figured out what the code was doing, I would be suspicious of its performance and memory consumption. This was 1994! Kilobytes mattered and optimal algorithmic complexity was necessary for anything to be usable. Much safer to use a well understood for loop. I have plenty of experience making those fast, and that's what map() must be doing under the hood anyway. But I would have been wrong! map() isn't doing anything superfluous and I can't do it faster myself. The memory consumption of the temporary decorated array is worth it to parse the last word N times instead of N log N times. Lisp is certainly a slow language compared to C, but that's not because of its lambdas! | |||||||||||||||||||||||||||||||||||||||||
▲ | ubercore 5 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
I believe I first came across this in the early xslt days, when I had a manager that wanted to write all software with an XML requirements spec, then use xslt to simultaneously generate code AND documentations. 1. It did not work because of course it didn't work 2. This meant that all data output of the system would done in XML and transformed, so I got to know xslt quite well 3. It did give me a fun moment at a conference where a speaker asked "Who knows the Schwartzian transform or has used it?" and only me and my coworkers raised their hands | |||||||||||||||||||||||||||||||||||||||||
▲ | jiggawatts 5 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
I was curious to see what other languages do by default (or not), so I quickly tested a few: C# evaluates the lambda only once per item, in input order: https://dotnetfiddle.net/oyG4Cv Java uses "Comparators", which are called repeatedly: https://www.jdoodle.com/ia/1IO5 Rust has sort_by_key() which is encouraging... but also calls the key transformation many times: https://play.rust-lang.org/?version=stable&mode=debug&editio... C++20 with std::range also calls the lambda repeatedly: https://godbolt.org/z/raa1766PG Obviously, the Schwartzian Transform can be manually implemented in any modern language elegantly, but it's interesting that only C# uses it implicitly! PS: The fancy solution would be a temporary "Hashset<T,K>" used to map values to the transformed sort keys, so that the mapping Foo(T t): K would only be called once for each unique input value 't'. That way the transformation could be invoked less than O(n) times! | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
▲ | mont_tag 5 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
Python's key-functions nicely encapsulate the whole process. | |||||||||||||||||||||||||||||||||||||||||
▲ | Sharlin 5 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
> Some people want Perl to be accessible at first glance to someone who doesn’t know the language. …oh boy. In retrospect it doesn’t seem like these people prevailed. | |||||||||||||||||||||||||||||||||||||||||
▲ | zippyman55 5 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
Thank you for posting! I always felt like such a badass in the late 1990s when I used this. | |||||||||||||||||||||||||||||||||||||||||
▲ | ygritte 5 days ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||
I'm so grateful for the Lisp translation. Perl is such a write-only language, my hair curls up just from looking at it. | |||||||||||||||||||||||||||||||||||||||||
|