Last active
December 20, 2015 01:09
-
-
Save christianromney/6046749 to your computer and use it in GitHub Desktop.
Without comments to test Adrian
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
(ns async.core | |
(:require [clojure.core.async :as async :refer :all] | |
[clojure.data.generators :as gen]) | |
(:gen-class)) | |
(defrecord SyncMessage [text wake]) | |
(defmacro forever [& body] | |
`(while true | |
~@body)) | |
(defn boring | |
[talk] | |
(let [wake (chan) | |
noise #(str % ": " (gen/string "*" (inc (rand-int 15))))] | |
(go | |
(doseq [n (iterate inc 1)] | |
(>! talk (->SyncMessage (noise n) wake)) | |
(<! (timeout (int (* 500 (rand))))) | |
(<! wake))) | |
talk)) | |
(defn multiplex | |
[& ins] | |
(let [out (chan)] | |
(go | |
(forever | |
(let [[result _] (alts! ins)] | |
(>! out result)))) | |
out)) | |
(defn -main | |
[& args] | |
(let [src (multiplex (boring (chan)) | |
(boring (chan)))] | |
(dotimes [n 5] | |
(let [msg1 (<!! src) | |
msg2 (<!! src)] | |
(println (:text msg1)) | |
(println (:text msg2)) | |
(>!! (:wake msg1) true) | |
(>!! (:wake msg2) true))) | |
(println "\nYou're both boring, I'm leaving."))) |
Fixed performance bug in this version
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try to determine: