Ask HN: What's the most elegant piece of code you've seen?
Lots of people have spent years writing programs spanning platforms, servers, services, and languages. However, efficient and elegant code is far and few between.
What code has stood out to you for being elegant and efficient? Why or why not?
90 comments
[ 2.7 ms ] story [ 160 ms ] thread10 PRINT CHR$(205.5+RND(1)); : GOTO 10
https://www.youtube.com/watch?v=m9joBLOZVEo
A lot of other code he writes as well.
1. https://github.com/montanaflynn/toy-spelling-corrector
That's really an example of how arbitrary human thought processes are. When you release the constraint that your code has to have some human-comprehensible analog, you might arrive at interesting results.
[0] https://en.wikipedia.org/wiki/Fast_inverse_square_root
Shifting an IEEE754 floating point number does not have that effect.[0] The fact that it doesn't do that is the source of the "mystery" of fast inverse square root.
[0] https://gist.github.com/jessedhillon/386fa964e822e529f6c1
https://en.m.wikipedia.org/wiki/Fast_inverse_square_root
[1] http://kotaku.com/5975610/the-exceptional-beauty-of-doom-3s-...
[2] https://github.com/id-Software/DOOM-3/blob/master/neo/game/a...
It looks like Github doesn't explicitly set tab-size so it defaults to 8. 4 seems to work better.
[1]: http://stackoverflow.com/questions/8833953/how-to-change-tab...
[2]: https://github.com/id-Software/DOOM-3/blob/master/neo/game/a...
It was, IIRC, only 3 C++ classes and just a few hundred lines of code. It outsourced much of the distribution, task running, and disk-access tasks to other Google infrastructure, and only focused on running the computation, collecting results for each key, and distributing to the reducers.
The current (as of ~2012, so not that current anymore) version of MapReduce is much faster and more reliable, but there's a certain elegance to starting a trillion-dollar industry with a few hundred lines of code.
There was another doozy, also by Jeff Dean, in the current (again, as of 2012) MapReduce code. It was an external sorting algorithm, and like most external sorts, it worked by writing a bunch of whole-machine-RAM sized temporary files and then performing an N-way merge. But how did it sort the machine's RAM? Using the STL qsort() function, of course! But how do you sort ~64GB of data efficiently using a standard-library function? He'd written a custom comparator that compared whole records at a time, using IIRC compiler intrinsics that compiled down into SIMD instructions and did some sort of Duff's-Device like unrolling to account for varying key lengths. It was a very clever mix of stock standard library functions with highly-optimized, specialized code.
[1] Think I read it in the Python Cookbook, 2nd Edition, which is a very good book, BTW.
I think quite a few languages adopted it as their default sort.
Interesting that others also used it.
http://stackoverflow.com/questions/4018332/is-java-7-using-t...
The original source code was solid and well documented in the parts I’ve seen, but the remarkable thing to me is the way they distribute it: it comes as a single ANSI C file and a single matching header, which they refer to as the “amalgamation”. It therefore works just about anywhere, has no packaging or dependency hell issues, can be be incorporated into any build process in moments, and can be statically linked and fully optimised.
https://github.com/antirez/redis/blob/unstable/src/dict.c
Unsurprisingly, these were games:
1) A C64 "SnakeByte-like" game, whose exact name I forgot. It was written entirely in BASIC 2.0 (so you could list and read the source code), and with me having no C64 manual, and no English, it was a true revelation. So much fun and beauty emerging from such a concise, approachable program!
2) An ancient five-in-a-row implementation, I think in BASIC again or maybe Pascal. I remember the shock after seeing how simple the code was, compared to its (surprisingly good) playing strength and speed. My github user name, "piskvorky", is an echo of this old experience :-)
The underlying appeal seems to be a combination of simple, elegant rules giving rise to complex and fun behaviour. That, to me, is elegance.
NoNoNo
factorial 0 = 1
factorial n = n * factorial (n - 1)
one of my favorite intermediate recursive function is powerset
http://www.worldofbooks.com/catalog/product/view/id/2366045/...
It's not overly clever, but it's incredibly clear and easy to understand.
Perhaps they just followed in the footsteps of the masters, but whatever the reason, the code is, in opinion, incredibly nice to work with.
Much of the NetBSD code base.
Pick a hack by Oleg Kiselyov.
The Commodore KERNAL.
https://github.com/0intro/plan9
> A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked 'why use Linux rather than Plan 9?' and the professor answered: Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write.
But see also the HN thread :
Code which every programmer must read before dying
https://news.ycombinator.com/item?id=2466129
Probably not that efficient in terms of raw CPU grunt though...
[1] https://en.wikipedia.org/wiki/SSH_Communications_Security
https://github.com/rackt/redux/tree/master/src
A truly elegant solution to state management.
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)