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?
161–170 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#162Intro to pretty much any language. It opens up so many possibilities ..
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#163Re: Ask HN: What is the most beautiful piece of code you've ever read?
#164 10 PAUSE 4E4
"PAUSE nstops computing & displays the picture for n frames of the television (at 50 frames per second, or 60 in America). n can be up to 32767, which gives you just under 11 minutes; if n is any bigger then it means 'PAUSE for ever'.
A pause can always be cut short by pressing a key"[0]
(4E4 = 4*10^4 = 40000)
[0] http://www.worldofspectrum.org/ZX81BasicProgramming/chap19.h...
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#165For me it's a function that calculates factorial using iterators in Rust: 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…
Nah, lots of other languages can do this.
Python:
def factorial(i):
return reduce(operator.mul, range(2, i+1), 1)
Ruby: def factorial(i)
(2..i).reduce(1, :*)
end
Haskell: factorial n = foldl (*) 1 [2..n]Re: Ask HN: What is the most beautiful piece of code you've ever read?
#166For 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?
#167Earlier quoted context omitted.
Does this algorithm guarantee uniform probability for all lines? Seems like the original ordering of the lines is a factor.
The OP should have mentioned that it's this algorithm: https://en.wikipedia.org/wiki/Reservoir_sampling IMO it's a lot clearer if it's not in Perl ... The pseudocode in Wikipedia also avoids division.
What isn’t?
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#168For me it's a function that calculates factorial using iterators in Rust: 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…
> In almost every other language this code would look messy or use some terrible recursion. Nah, lots of other languages can do this. Python: def factorial(i): return reduce(operator.mul, range(2, i+1), 1) Ruby: def factorial(i) (2..i).reduce(1, :*) end Haskell: factorial n = foldl (*) 1 [2..n]
factorial n = product [1..n]Re: Ask HN: What is the most beautiful piece of code you've ever read?
#169Earlier quoted context omitted.
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.
What is a maze? One could argue that if the user perceives a maze, then it produces a maze.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#170For me it's a function that calculates factorial using iterators in Rust: 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…
But a more idiomatic C version would be
long factorial(int n)
{
long result = 1;
for (int c = 1; c