Live data from Hacker News

Ask HN: What is the most beautiful piece of code you've ever read?

news.ycombinator.com

41–50 of 394 posts

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#42

Quicksort in Haskell is very elegant, though not truly quicksort (not sort in place): https://wiki.haskell.org/Introduction#Quicksort_in_Haskell

You are the second person to reference quicksort in Haskell in this thread. The other person even mentioned it not being true quicksort. Who knew quicksort in Haskell was such a beloved piece of code!

Back when HotOrNot was hot, there was a site like it for rating code snippets. The top-rated one, I think I remember, was a broken version of this sort function: it had left out the recursions or something (I don't remember exactly), which made it even more 'elegant'.

This wasn't the only case I noticed of highly-rated broken code there. It took me a long time to take this lesson to heart about especially slick-looking code of my own.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#43
Lexical Scanning In Go is a charming piece of programming:

https://m.youtube.com/watch?v=HxaD_trXwRE

A good debuggable piece of code explains what it is trying to do by being clearly written and conforming to a consistent and logical model. I don’t think I would ever have invented this type of lexer pattern in Go by myself, but would be very grateful to come across it in a code base I had to fix. Like Duff’s Device mentioned in this thread, it has just the right amount of cleverness without becoming inscrutable.

Also, if you’ll excuse some avuncular pride, my niece and I wrote some code yesterday. She asked me how many times grandma’s clock chimes every day and we ended up with the following Ruby:

  2 * (1..12).reduce(&:+)

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#45

Lexical Scanning In Go is a charming piece of programming: https://m.youtube.com/watch?v=HxaD_trXwRE A good debuggable piece of code explains what it is trying to do by being clearly written and conforming to a consistent and logical model. I don’t think I would ever have invented this type of lexer pattern in Go by myself, but would be very grateful to come across it in a code base I had to fix. Like Duff’s Device m…

Cool one liner! Here's some other languages:

Python: 2 * sum([x for x in range(1, 13)])

Haskell: 2 * sum [1..12]

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#46
I'd have to think long and hard for the most beautiful code I've ever read, but I think the classic K&R "strcpy" comes pretty close:

  void strcpy(char *s, char *t) {
      while (*s++ = *t++);
  }
It's short, elegant, and quite readable to the trained eye–a bit sharp too, but if you use it right it's quite functional.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#48
post #7

John Carmack's Fast Inverse Square Root: https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overv... . The first time I really and truly felt that people approach problems differently from how I, by default, go about them.

IIRC Carmack didn't write that code, as brilliant as he is.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#49
post #28

Curious why the comment about STL algorithms written by Alexander Stepanov was downvoted so hard it died? I’m not a C++ dev... is he hated or is the implementation that bad?

If you look at the comment history, it appears that there is a ban on almost all of the comments, which suggests an action by moderators: https://news.ycombinator.com/threads?id=wiineeth
Post reply on HN