(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
Ask HN: What is the most beautiful piece of code you've ever read?
141–150 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#142 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?
#143Re: Ask HN: What is the most beautiful piece of code you've ever read?
#144John 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?
#145Pick 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…
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#146Pick 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's a special case of reservoir sampling.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#147For 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!
What do you mean by that?
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#148Pick 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.
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?
#149Pick 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?
#150 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.