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...
No, it does not generate a maze. It generates a random sequence of \s and /s, that can trigger a maze being generated in a typical user's mind. This is the art of illusion.
Ask HN: What is the most beautiful piece of code you've ever read?
151–160 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#152John 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.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#153It is beautiful because it was my introduction to C that led to the world that I am in now.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#154Pick a random line from a file / stream without knowing how many lines there are to choose from in one pass without storing the lines that have been seen. perl -e 'while( ){$x=$_ if rand() For each line, pick that line as your random line if a random number (0 It hits my elegant bone. Only one line... rand < 1/1, pick it. Two lines, same as one, but the second line has a 1/2 change of replacing line one. Third line s…
Does this algorithm guarantee uniform probability for all lines? Seems like the original ordering of the lines is a factor.
It didn't sound plausible to me (because I misunderstood what the algorithm was; yay perl), hence quickly testing it, but it seems to work, and after appreciating what the algorithm actually is (see sibling replies), it not only works but it _should_ work. (There is a CS versus engineering joke in here, somewhere.)
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#155It really inspired me to get better at seeking out the fundamental operations of whatever I was implementing.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#156 fn factorial(i: u64) -> u64 {
(1..=i).product()
}
In almost every other language this code would look messy or use some terrible recursion.For example in C it would look something like this:
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c
Or with recursion: long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
In any case, I thin Rust looks better in every way with its cleaner syntax.Re: Ask HN: What is the most beautiful piece of code you've ever read?
#157For 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...
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#158Earlier quoted context omitted.
I've got a better code snippet: your exact same code snippet but for a language which will short circuit based on the lvalue of the assignment expression. Then t would never be able to overflow s (nor even eat its null terminator)
The size of the buffer pointed to by s may be larger than the current string it holds. It may also be uninitialized.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#159 : \ IMMEDIATE
#IB @ >IN !
; \ We can now comment!
Implementing a forth system is unbelievably fun because of gems like this.Re: Ask HN: What is the most beautiful piece of code you've ever read?
#160Regex for that is just great
^((?!word).)*$