Created
July 4, 2013 02:28
-
-
Save theleoborges/5924461 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 async :refer :all]) | |
(defn fake-search [kind] | |
(fn [query] | |
(Thread/sleep (int (* (java.lang.Math/random) 1000))) | |
(str kind " result for " query))) | |
(def web (fake-search "Web")) | |
(def image (fake-search "Image")) | |
(def video (fake-search "Video")) | |
;; Google search 2.0 | |
;; http://talks.golang.org/2012/concurrency.slide#46 | |
(defn google2-0 [query] | |
(let [c (chan)] | |
(go (>! c (web query))) | |
(go (>! c (image query))) | |
(go (>! c (video query))) | |
(reduce (fn [results _] | |
(conj results (<!! (go (<! c))))) | |
[] | |
(range 3)))) | |
(google2-0 "Clojure") | |
;; Google search 2.1 | |
;; http://talks.golang.org/2012/concurrency.slide#47 | |
(defn google2-1 [query] | |
(let [c (chan) | |
t (timeout 500)] | |
(go (>! c (web query))) | |
(go (>! c (image query))) | |
(go (>! c (video query))) | |
(reduce (fn [results _] | |
(conj results (first (alts!! [c t])))) | |
[] | |
(range 3)))) | |
(google2-1 "Clojure") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment