Live data from Hacker News

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

news.ycombinator.com

71–80 of 394 posts

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

#71
post #64

Earlier quoted context omitted.

This is O(n^2) though

All of JS's list processing functions are pretty inefficient, since at the bare minimum each one creates and copies to a new array (as opposed to, say, Rust iterators). You use them when elegance is more important than performance; N^2 is fine when N is eight.

[deleted]

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

#72
post #70

Earlier quoted context omitted.

Personally I like this one, although it only works for an array of primitive types. const unique = [...new Set(arr)];

The other advantage of the filter one is it can be stuck in the middle of a bunch of other list operations - map(), sort(), slice(), other filters(). Also, you can use findIndex() instead of indexOf() to match any arbitrary predicate, instead of exact equivalence.

Oh, for sure. I know .filter() has it's place, so if you need anything beyond making the array unique, I'd definitely use your solution.

The one I posted just blew my mind a little the first time I saw it, so I love to share it. I guess I never really considered spreading a set.

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

#74
post #24

The one that blew my mind when I was in college was a simplified version of quicksort in Haskell. It's just so elegant and clean. quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater) where lesser = filter ( = p) xs Now surely someone may come along and point out how this isn't a true quicksort[0] because it doesn't partition the elements in place, but…

Agreed that this is beautiful purely from the standpoint of how there is essentially no disconnect between the concept of the algorithm and the way it's expressed in code. It's almost the math/algo just translated directly into code (not commenting on the efficiency or other issues that may exist).

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

#75
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...

Wow! This is really cool. I just entered it quickly in a C64 emulator and it ran, creating a maze. Not sure, whether it would be a solvable one, but it is interesting to note, that the paths are really connected!

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

#76
Someone once posted a C program here on HN, where each line, even the comments, seemed to "line up" in 6- or 8- character blocks with a space in between. It made the whole program look sort of like a table.

I felt it was sort of like poetry. I unfortunately no longer have a link to it, and once looked very hard but couldn't find it.

I would be extremely appreciative of someone else saw it and had a link. If I recall correctly it was a type of interpreter, I remember it having code for parsing.

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

#77
post #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.

1/2^n, I believe (where n is the number of lines after the first one, otherwise 1/2^n+1). So halved every time: 100% with 1 line, 50% with 2 lines, 25% with 3 lines, and so on. Width is irrelevant (you can do this in your head comparing w=1 n=2 to w=2 n=2 for example).

Edited for clarity and accuracy.

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

#79
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/

This book is some nice addition to "Computer Culture".

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

#80
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/

Was kinda hoping the maze on the left was procedurally generated.
Post reply on HN