-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e8f112
commit 41e95a5
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(require '[clojure.core.async :as async :refer [<! >! <!! >!! timeout chan alt! alts!! go]]) | ||
|
||
(defn fan-in [ins] | ||
(let [c (chan)] | ||
(future (while true | ||
(let [[x] (alts!! ins)] | ||
(>!! c x)))) | ||
c)) | ||
|
||
(defn fan-out [in cs-or-n] | ||
(let [cs (if (number? cs-or-n) | ||
(repeatedly cs-or-n chan) | ||
cs-or-n)] | ||
(future (while true | ||
(let [x (<!! in) | ||
outs (map #(vector % x) cs)] | ||
(alts!! outs)))) | ||
cs)) | ||
|
||
(let [cout (chan) | ||
cin (fan-in (fan-out cout (repeatedly 3 chan)))] | ||
(dotimes [n 10] | ||
(>!! cout n) | ||
(prn (<!! cin)))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
(require '[clojure.core.async :as async :refer [<! >! timeout chan alt! alts! go]]) | ||
|
||
(defn fan-in [ins] | ||
(let [c (chan)] | ||
(go (while true | ||
(let [[x] (alts! ins)] | ||
(>! c x)))) | ||
c)) | ||
|
||
(defn fan-out [in cs-or-n] | ||
(let [cs (if (number? cs-or-n) | ||
(repeatedly cs-or-n chan) | ||
cs-or-n)] | ||
(go (while true | ||
(let [x (<! in) | ||
outs (map #(vector % x) cs)] | ||
(alts! outs)))) | ||
cs)) | ||
|
||
(let [cout (chan) | ||
cin (fan-in (fan-out cout (repeatedly 3 chan)))] | ||
(go (dotimes [n 10] | ||
(>! cout n) | ||
(prn (<! cin)))) | ||
nil) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
(require '[clojure.core.async :as async :refer [<!! >!! timeout chan alt!!]]) | ||
|
||
(defn fake-search [kind] | ||
(fn [c query] | ||
(future | ||
(<!! (timeout (rand-int 100))) | ||
(>!! c [kind query])))) | ||
|
||
(def web1 (fake-search :web1)) | ||
(def web2 (fake-search :web2)) | ||
(def image1 (fake-search :image1)) | ||
(def image2 (fake-search :image2)) | ||
(def video1 (fake-search :video1)) | ||
(def video2 (fake-search :video2)) | ||
|
||
(defn fastest [query & replicas] | ||
(let [c (chan)] | ||
(doseq [replica replicas] | ||
(replica c query)) | ||
c)) | ||
|
||
(defn google [query] | ||
(let [c (chan) | ||
t (timeout 80)] | ||
(future (>!! c (<!! (fastest query web1 web2)))) | ||
(future (>!! c (<!! (fastest query image1 image2)))) | ||
(future (>!! c (<!! (fastest query video1 video2)))) | ||
(loop [i 0 ret []] | ||
(if (= i 3) | ||
ret | ||
(recur (inc i) (conj ret (alt!! [c t] ([v] v)))))))) | ||
|
||
(google "clojure") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
(require '[clojure.core.async :as async :refer [<! >! <!! timeout chan alt! go]]) | ||
|
||
(defn fake-search [kind] | ||
(fn [c query] | ||
(go | ||
(<! (timeout (rand-int 100))) | ||
(>! c [kind query])))) | ||
|
||
(def web1 (fake-search :web1)) | ||
(def web2 (fake-search :web2)) | ||
(def image1 (fake-search :image1)) | ||
(def image2 (fake-search :image2)) | ||
(def video1 (fake-search :video1)) | ||
(def video2 (fake-search :video2)) | ||
|
||
(defn fastest [query & replicas] | ||
(let [c (chan)] | ||
(doseq [replica replicas] | ||
(replica c query)) | ||
c)) | ||
|
||
(defn google [query] | ||
(let [c (chan) | ||
t (timeout 80)] | ||
(go (>! c (<! (fastest query web1 web2)))) | ||
(go (>! c (<! (fastest query image1 image2)))) | ||
(go (>! c (<! (fastest query video1 video2)))) | ||
(go (loop [i 0 ret []] | ||
(if (= i 3) | ||
ret | ||
(recur (inc i) (conj ret (alt! [c t] ([v] v))))))))) | ||
|
||
(<!! (google "clojure")) | ||
|