Skip to content

Commit

Permalink
Add initial examples
Browse files Browse the repository at this point in the history
  • Loading branch information
puredanger committed Jun 27, 2013
1 parent 7e8f112 commit 41e95a5
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/ex-alts.clj
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))))
25 changes: 25 additions & 0 deletions examples/ex-altsgo.clj
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)
33 changes: 33 additions & 0 deletions examples/ex-async.clj
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")
34 changes: 34 additions & 0 deletions examples/ex-go.clj
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"))

0 comments on commit 41e95a5

Please sign in to comment.