Last active
October 24, 2017 14:34
-
-
Save aibrahim/85af843ef94c6cda544e596f4ba8b50a to your computer and use it in GitHub Desktop.
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
(require '[clojure.core.async :as a :refer [>! <! >!! <!! go chan buffer close! thread alts! alts!! timeout]]) | |
(defn remove-multipliers [n l] | |
(filter (fn [x] ((comp not zero?) (mod x n))) l)) | |
(def odds (rest (filter odd? (range 100000)))) | |
(def N (count odds)) | |
(def channels (mapv (fn [x] (chan)) (range N))) | |
(mapv (fn [c1 c2 x] (go (>! c2 (let [pass (<! c1) | |
[primes unprocessed] pass | |
a (first unprocessed)] | |
[(cons a primes) (remove-multipliers a unprocessed)] )))) channels (rest channels) odds) | |
(def c-filter (chan)) | |
(def c-print (chan)) | |
(go (>! c-filter (<! (last channels)))) | |
(go (>! c-print (let [[primes _] (<! c-filter)] (->> primes (filter (comp not nil?)) reverse)))) | |
(go (println (<! c-print))) | |
(>!! (first channels) [[] odds]) |
Author
aibrahim
commented
Oct 24, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment