Created
November 30, 2013 00:02
-
-
Save anonymous/7713596 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
(defn event [env type val] | |
(let [rc (async/chan)] | |
(async/<!! | |
(async/go | |
(async/>! (:queue @env) | |
{:type type :val val :rc rc :time (:now @env)}) | |
(async/<! rc) | |
(async/close! rc))))) | |
(time | |
(let [n 100000 | |
env (atom {:now 0 :queue (async/chan)})] | |
;; car | |
(async/go | |
(loop [] | |
;;(println "Driving at " (:now @env)) | |
(event env :timeout 5) | |
;;(println "Parking at " (:now @env)) | |
(event env :timeout 2) | |
(when (< (:now @env) n) (recur)))) | |
(async/<!! | |
(async/go | |
(loop [] | |
(let [e (<! (:queue @env)) | |
nt (+ (:val e) (:now @env))] | |
(when (< nt n) | |
(swap! env assoc :now nt) | |
(async/go (async/>! (:rc e) true)) | |
(recur)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment