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()<=(1/$.)}print $x'
For each line, pick that line as your random line if a random number (0<=n<1) is less than the reciprocal of the number of lines read so far ($.).
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 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.
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.)
It is that simple! It has practical benefits. For example, if you can call that code from somewhere specific in your program, which is handy for debugging. If your `eval` function takes a lexical environment, you can also implement a debugger easily.
To test whether your logic is working properly, make your repl print out a different character when it prompts for input. For example, the toplevel repl can print out "> " whereas the inner repl prints out "repl> " For bonus points, your repl should exit if you type Ctrl-D. That way you can go from the inner repl to the outer repl, and from there it should exit your program.
Yeah REPL comes from this definition of "reading, evaluation, printing, looping". But in lisp you write it in reverse order, "Loop, print, evaluate, read".
I'm not saying the acronym came before or after the LISP code :P
This is the first example I've seen in this thread that I would consider beautiful. The rest are neat tricks, but quite difficult to understand, so I wouldn't be happy to see them in production code.
Oh my God, his Lisp interpreter in Python: amazing. At the time I was just getting a grip with Python and starting in Lisp, it came at just the right moment in my autodidaction.
Really really good, although for a lot of people here it might be a little elementary (but then, the best code always feels elementary even when doing something advanced!)
Comments (380) 116 threads
Earlier commentsIt 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.
It's a special case of reservoir sampling.
https://en.wikipedia.org/wiki/Reservoir_sampling
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?
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.)
But at least now I know that it's called Reservoir Sampling. I had wondered how to generalize it to wanting N lines.
That to me is elegance. It's simple yet powerful, and just 4 words really.
I wondered: Can we now run the repl in the repl? So I typed:
It kept prompting me for input. Am I in a REPL in a REPL now? I typed: And the reply was: I'm not totally sure if I believe it though.To test whether your logic is working properly, make your repl print out a different character when it prompts for input. For example, the toplevel repl can print out "> " whereas the inner repl prints out "repl> "
For bonus points, your repl should exit if you type Ctrl-D. That way you can go from the inner repl to the outer repl, and from there it should exit your program.
"I am in a REPL in a REPL in a REPL,
Running all my code in nested loops..."
https://youtu.be/pVB_DI4ajKA
I'm not saying the acronym came before or after the LISP code :P
https://github.com/webyrd/quines/blob/master/pmatch.scm
I have to agree, this like rank #1 in my book!
I was just recently (re-)reading an article that goes in depth:
Lisp as the Maxwell’s equations of software
http://www.michaelnielsen.org/ddi/lisp-as-the-maxwells-equat...
Really really good, although for a lot of people here it might be a little elementary (but then, the best code always feels elementary even when doing something advanced!)
https://www.metalevel.at/lisprolog/ https://www.metalevel.at/lisprolog/lisprolog.pl
All comments shown.
Couldn't load more comments. Check your connection and try again.