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...
Ask HN: What is the most beautiful piece of code you've ever read?
51–60 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#52 $ 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...
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#53Lexical 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?
#54Lexical 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 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?
#55Last 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?
#56For 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...
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?
#57Duff's Device: https://en.wikipedia.org/wiki/Duff%27s_device Very elegant use of the fall-through behavior of the swtich statement.
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?
#58Re: Ask HN: What is the most beautiful piece of code you've ever read?
#59 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?
#60There 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.