Skip to content

@tbatchelli /core.clj
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Sieve of Erastothenes as a data-flow using core.async (http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
(ns sieve.core
(:require [clojure.core.async :as async :refer :all]]))
(defn counter-stream [n]
(let [c (chan)]
(go
(loop [i n]
(>! c i)
(recur (inc i))))
c))
(defn filter-multiples [p in]
(let [out (chan)]
(go
(loop []
(let [v (<! in)]
(when-not (zero? (mod v p))
(>! out v)))
(recur)))
out))
(defn sieve []
(let [primes (chan)]
(go
(loop [c (counter-stream 2)]
(let [p (<! c)]
(>! primes p)
(recur (filter-multiples p c)))))
primes))
(comment
(def primes (sieve))
(<!! primes)
2
(<!! primes)
3
etc...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.