Skip to content

Instantly share code, notes, and snippets.

@dbyrne
Created August 10, 2016 03:17
Show Gist options
  • Save dbyrne/be92b011a94deeb60fc7ddbfac705768 to your computer and use it in GitHub Desktop.
Save dbyrne/be92b011a94deeb60fc7ddbfac705768 to your computer and use it in GitHub Desktop.
(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