Created
April 23, 2014 08:44
-
-
Save anonymous/11207435 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
(use 'clojure.core.async) | |
(defn req [server query] | |
(go | |
(<! (timeout (rand-int 100))) | |
(str query " from " server))) | |
(defn fastest [query & servers] | |
(let [c (chan)] | |
(doseq [server servers] | |
(go | |
(>! c (<! (req server query))) | |
(close! c))) | |
c)) | |
;; go returns channel with return value | |
(defn search [query t] | |
(let [c (chan) | |
t (timeout t)] | |
(go (>! c (<! (fastest query "web1" "web2" "web3")))) | |
(go (>! c (<! (fastest query "img1" "img2")))) | |
(go (>! c (<! (fastest query "video1" "video2")))) | |
(go-loop [ret []] | |
(if (>= (count ret) 3) | |
ret | |
(recur | |
(conj ret (alt! [c t] ([v] v)))))))) | |
(<!! (search "abc" 90)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment