Created
May 20, 2013 12:03
-
-
Save msgodf/5611845 to your computer and use it in GitHub Desktop.
Concurrent prime number sieve. I wanted something that took a while, so that the agent could be running in the background for some time. I used an atom so that I could monitor the sieve externally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def sieve (atom [])) | |
(def siever (agent sieve)) | |
(send-off siever | |
(fn [x test-set] | |
(doall | |
(map (fn [n] | |
(if (not (some | |
#(zero? (mod n %)) | |
@x)) | |
(swap! x (partial cons n)))) | |
test-set)) | |
x) | |
(range 2 30000)) | |
(dotimes [n 100] ((prn (count @sieve)) (Thread/sleep 500))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment