Live data from Hacker News

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

news.ycombinator.com

161–170 of 394 posts

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

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

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.

Is the user then not amazed?

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

#163

Earlier quoted context omitted.

The size of the buffer pointed to by s may be larger than the current string it holds. It may also be uninitialized.

That’s what strncpy() is for

Watch out: what strncpy does is usually not what you'd want it to do.

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

#164
Waiting for a keypress in ZX81 BASIC (maybe it's the opposite of elegant, but I remember it as the first time seeing a kind of easter egg in code):

    10 PAUSE 4E4
"PAUSE n

stops 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?

#165

For 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]

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

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

The mysterious maze algorithm of Atari game Entombed: https://www.bbc.com/future/article/20190919-the-maze-puzzle-...

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

#167
post #148

Earlier 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.

IMO it’s a lot clearer if it’s not in Perl

What isn’t?

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

#168

For 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]

In haskell you can also use the `product` function:

    factorial n = product [1..n]

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

#169

Earlier 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.

Never expected to find myself on the path to enlightenment after reading a line of BASIC.

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

#170

For 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…

That Rust is very nice.

But a more idiomatic C version would be

   long factorial(int n)
   {
     long result = 1;
     for (int c = 1; c 
Post reply on HN