Live data from Hacker News

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

news.ycombinator.com

51–60 of 394 posts

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

#51
post #23

For me it's a BASIC one-liner which generates mazes 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 I found this specific one via slashdot[1], but something similar, which I've never managed to find/replicate, was used to generate mazes on the Atari 800 XL at my school when I was a kid. [1] https://developers.slashdot.org/story/12/12/01/1847244/how-d...

There’s actually an entire book on this at: https://10print.org/

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

#52
For me it's the unix example in [1] "AT&T Archives: The UNIX Operating System".

    $ makewords sentences | lowercase | sort | unique | mismatch -
It reads a file called sentences then prints the words that are not spelled correctly.

To me it's; concise, expressive, flexible, modular... Which makes it beautiful...

[1] https://youtu.be/tc4ROCJYbm0

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

#53
post #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]

You don't need the list comprehension for python - just 2 * sum(range(1, 13))

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

#54
post #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]

What’s quite elegant about Ruby (in and of itself, not in comparison to your examples) is how consistently it is implemented using its own simple set of core features, with very little magic or syntactic sugar. reduce, Enumerable, what the & operator is doing etc.

What’s so challenging about Ruby is how the language can be abused by library vendors to make all kinds of surprising magic and homegrown syntactic sugar.

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

#55
Loathe to be contrarian but code that is very boring, readable , coherent and comprehensive at scale is 'great code' and I'm completely unimpressed by these 10-liner 'wow' bits of algorithmic anachronism often mentioned in response to such questions.

Last time I checked into the TypeScript source, it was really nice to look at - this is very rare and admirable.

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

#56
post #23

For me it's a BASIC one-liner which generates mazes 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 I found this specific one via slashdot[1], but something similar, which I've never managed to find/replicate, was used to generate mazes on the Atari 800 XL at my school when I was a kid. [1] https://developers.slashdot.org/story/12/12/01/1847244/how-d...

What is the probability that it prints a completable[0] maze?

Expressed in terms of line width (w) and number of lines (n).

[0] Where there's a valid path from the first line to the last.

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

#57

Duff's Device: https://en.wikipedia.org/wiki/Duff%27s_device Very elegant use of the fall-through behavior of the swtich statement.

For those unaware, * do not use this on modern systems. *

Normal for-loops are much faster than they were when Duff's Device was invented, since they take advantage of modern branch prediction.

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

#59
I like this bit of JavaScript for uniquifying an array:

  array.filter((item, index, arr) => arr.indexOf(item) === index)
It works because indexOf returns the index of the first occurrence of the item, so you're asking whether this occurrence is the first occurrence.

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

#60
That's a way to loaded and bait like question. Why not ask for "the most elegant snippet" instead?

There are beautiful code bases like DOOM source or SAT solvers that are like super models beautiful. Complete and hard to improve. Marvel at from a distance.

There is beautiful code in the libraries everybody uses all the time. All the lib* code. Somebody to marry. Discover the good beauty over time.

And then there is the beautiful snippet of clever code at the bar, quick to love, but one you get to know him it's much more trouble than he's worth.

Post reply on HN