Created
August 10, 2016 03:17
-
-
Save dbyrne/be92b011a94deeb60fc7ddbfac705768 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns orbits.core | |
(:require [clojure.core.async :as async :refer [<! >! <!! chan go-loop]])) | |
(defn print-primes [ch] | |
(dorun (repeatedly 35 #(println (<!! ch))))) | |
(defn gen-candidates [ch] | |
(go-loop [i 2] | |
(>! ch i) | |
(recur (inc i))) | |
ch) | |
(defn sift [m x y z] | |
(update m (+ x y) #(cons z %))) | |
(defn gen-primes [ints ch] | |
(go-loop [multiples {}] | |
(let [x (<! ints)] | |
(if (contains? multiples x) | |
(recur | |
(dissoc | |
(reduce | |
#(sift % x %2 %2) | |
multiples | |
(multiples x)) | |
x)) | |
(do | |
(>! ch x) | |
(recur (sift multiples x x x)))))) | |
ch) | |
(defn -main [] | |
(print-primes | |
(gen-primes | |
(gen-candidates (chan)) | |
(chan)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment