Live data from Hacker News

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

news.ycombinator.com

141–150 of 394 posts

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

#141
post #117

(define eval-expr (lambda (expr env) (pmatch expr [`,x (guard (symbol? x)) (env x)] [`(lambda (,x) ,body) (lambda (arg) (eval-expr body (lambda (y) (if (eq? x y) arg (env y)))))] [`(,rator ,rand) ((eval-expr rator env) (eval-expr rand env))]))) There's an amazing talk about it: https://www.youtube.com/watch?v=OyfBQmvr2Hc

Once you've seen this one, it's difficult not to rank it first :)

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

#142
Pick 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 (0It 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 same as before but gets a 1/3 chance of taking the place of whichever line has survived the first two picks. At the end... you have your random line.

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

#144
post #7

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

it's the antithesis of maintainable code. But given that one is unlikely to want to change from calculating the inverse square root to inverse cube root or any other variation....

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

#145

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

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

#146

Pick 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 does.

It's a special case of reservoir sampling.

https://en.wikipedia.org/wiki/Reservoir_sampling

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

#147
post #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!

> the paths are really connected!

What do you mean by that?

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

#148

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

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.

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

#149

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

Perhaps surprisingly, yes, it does! This is called "reservoir sampling", and a blog post with some more details is available at https://blog.plover.com/prog/weighted-reservoir-sampling.htm....

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

#150
Bit tricks. Like

    public static int bitCount(int i) {
        i = i - ((i >>> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
        i = i + (i >>> 8);
        i = i + (i >>> 16);
        return i & 0x3f;
    }
It was like magic for me when I encountered it first time.
Post reply on HN