TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: The most fun and beautiful code you know of?

26 点作者 pookleblinky将近 16 年前
What is the beautiful, interesting, suspenseful, fun code that you would rather curl up to read with a cup of coffee than most any other writing?<p>What is your "nightstand" code?

18 条评论

Xichekolas将近 16 年前
Funny seeing this, because I was at my local Borders earlier and decided to give that Beautiful Code book a shot. I've seen it on the shelf for quite a while, but those books don't tend to turn my crank, so to speak, so I had never before really considered it.<p>I only read the first and the third essay, but I must say, wow!<p>The third essay, for instance, takes a simple 12 line implementation of quicksort in C (which is already quite beautiful), and through a series of well explained transformations, arrives at a four line proof (as in, it's C code that is equivalent to the recurrence relation, and eventually the summation, we all solved in our CS classes) of the average case complexity (~1.4nlgn comparisons) of the quicksort he started with.<p>That is what I would consider "nightstand" code, or at least a nightstand essay about code. It was actually entertaining, to the point that you don't realize you are solving a recurrence relation in C form until the end, when the parallel becomes obvious.<p>I only had a chance to skim the other topics, but I'm actually considering buying it, because it really was a blast to read.
评论 #623508 未加载
评论 #623277 未加载
voisine将近 16 年前
UTF-8 encoding in 6 lines, from ezxml:<p><pre><code> if (c &#60; 0x80) *(s++) = c; // US-ASCII subset else { // multi-byte UTF-8 sequence for (b = 0, d = c; d; d /= 2) b++; // number of bits in c b = (b - 2) / 5; // number of bytes in payload *(s++) = (0xFF &#60;&#60; (7 - b)) | (c &#62;&#62; (6 * b)); // head while (b) *(s++) = 0x80 | ((c &#62;&#62; (6 * --b)) &#38; 0x3F); // payload }</code></pre>
kmavm将近 16 年前
The UNIX v6 kernel (Lions book). Every page brims with the excellent, <i>hard</i> tradeoffs required to shoehorn what we'd recognize as a modern system onto a 128K 16-bit machine.
Jeremysr将近 16 年前
My own code...<p>(Not because it's perfect and beautiful, but just because I wrote it and understand it.)
评论 #623197 未加载
评论 #623431 未加载
sauce71将近 16 年前
Duff's device. It opened up my eyes for the beauty of C and insanity of compilers: <a href="http://en.wikipedia.org/wiki/Duffs_device" rel="nofollow">http://en.wikipedia.org/wiki/Duffs_device</a>
dfranke将近 16 年前
Usually I find API documentation more inspiring than actual code. It's higher-bandwidth. That said:<p>- Some bits of <i>On Lisp</i><p>- Haskell's SYB and Parsec libraries<p>- MINIX (2; have yet to play with 3)
vivekamn将近 16 年前
Solutions in Programming Pearls. Each solution is iteratively improved with the proper reasoning.<p><a href="http://www.amazon.com/Programming-Pearls-2nd-ACM-Press/dp/0201657880" rel="nofollow">http://www.amazon.com/Programming-Pearls-2nd-ACM-Press/dp/02...</a>
nailer将近 16 年前
Anything that:<p>* Doesn't repeat itself. This includes languages which involve marking out blocks of code once for the interpreter and the second time for humans.<p>* Is short, because it doesn't reinvent common modules, and modularizes out general logic.<p>* Uses descriptive names, including using dictionary keys rather than vague list element numbers. A good test is if you can show the code to someone who doesn't work in computing and have them understand it. ZS had a talk on this where he showed lawyers SOx code and received feedback on the rules, changes to the order, etc.<p>* Uses tree structures for tree structured data (eg, etrees and xpath) rather than treating such data as strings and using RegExs.<p>* Has docstrings.
jmonegro将近 16 年前
Shakespeare. It exists. Seriously. It's as beautiful as Shakespeare, but not very fun, sadly.<p><a href="http://en.wikipedia.org/wiki/Shakespeare_(programming_language)" rel="nofollow">http://en.wikipedia.org/wiki/Shakespeare_(programming_langua...</a>
spitfire将近 16 年前
The art of computer programming.<p>Not because it's direct computer code, but because when I'm reading it past code i've written starts swirling in my head. Sort of one of those "ahhh fantastic" moments.
progLiker将近 16 年前
/* fun only <i>/ #const HellIsAHotAndFieryPlace=1 while (HellIsAHotAndFieryPlace) printf("The end is near\n") ;<p>/</i> yes it did make me laugh when I read it in Atari St user */
aristus将近 16 年前
Very few people will get this reference, but when I had access I used to read the genweb3 code. It's like a whodunnit because there are no comments. :)
keefe将近 16 年前
pseudocode all throughout cormen... I appreciate industrial code the same way I appreciate a ferrari - without looking at the engine
jmonegro将近 16 年前
Shakespeare. It exists. Seriously. It's as beautiful as Shakespeare, but not very fun, sadly.<p><a href="http://en.wikipedia.org/wiki/Shakespeare_(programming_language)" rel="nofollow">http://en.wikipedia.org/wiki/Shakespeare_(programming_langua...</a>
known将近 16 年前
<a href="http://lxr.linux.no" rel="nofollow">http://lxr.linux.no</a>
abyssknight将近 16 年前
c99shell. It's a PHP trojan horse of sorts written by Russian hackers. Beautiful, rare, dangerous and awesome.
maurycy将近 16 年前
OpenBSD, Django and code written by Niels Provos.
nostrademons将近 16 年前
Google.