Skip to content

Commit c728e6a

Browse files
committed
updates for CodeMesh.io
1 parent 60af71e commit c728e6a

File tree

1 file changed

+39
-64
lines changed

1 file changed

+39
-64
lines changed

src/clojure_conj_talk/core.clj

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
(def c (chan))
1111

1212

13-
13+
(chan)
1414
;; We can attach listeners via take!
1515

1616
(take! c (fn [v] (println v)))
@@ -107,7 +107,7 @@
107107

108108
(go (println "It works!" (<! (go 42))))
109109

110-
;; Wait....why did we use <!? Well <!! is simply a function that uses
110+
;; Wait....why did we use <!! Well <!! is simply a function that uses
111111
;; blocking promises to wait for channel values. That would mess up the fixed
112112
;; size thread pool that go blocks dispatch in. So we must define two sets
113113
;; of operations
@@ -173,42 +173,13 @@
173173

174174
(<!! sbc) ;; returns 2
175175

176-
;;; Custom Buffers ;;;;
177-
178-
; Need not be thread-safe. They will only be accessed by a single
179-
; thread at a time
180-
181-
(require '[clojure.core.async.impl.protocols :as impl])
182-
(import '[java.util LinkedList])
183-
184-
(defn debug-buffer [n a]
185-
(let [buf (LinkedList.)
186-
dbb (reify
187-
impl/Buffer
188-
(full? [this]
189-
(= (.size buf) n))
190-
(remove! [this]
191-
(swap! a dec)
192-
(.removeLast buf))
193-
(add! [this itm]
194-
(swap! a inc)
195-
(.addFirst buf itm))
196-
clojure.lang.Counted
197-
(count [this]
198-
(.size buf)))]
199-
dbb))
200-
201-
(def a-size (atom 0))
202-
203-
(def dbbc (chan (debug-buffer 20 a-size)))
176+
;;; Closing a channel
204177

205-
(>!! dbbc 0)
206-
(>!! dbbc 1)
207-
(>!! dbbc 2)
178+
(def c (chan))
208179

209-
(println @a-size)
180+
(close! c)
210181

211-
(println (<!! dbbc) @a-size)
182+
(<!! c)
212183

213184
;;;; Alt & Timeout ;;;;
214185

@@ -225,7 +196,7 @@
225196
;; Timeout is a channel that closes after X ms
226197
;; (close! (chan 42))
227198

228-
(<!! (timeout 1000))
199+
(<!! (timeout 2000))
229200

230201
;; Often used with alt
231202

@@ -383,13 +354,18 @@
383354
(<!! (avg-cast-popularity 238))
384355

385356

386-
(time (dotimes [x 10]
387-
(<!! (http-get "http://www.imdb.com/xml/find?json=1&q=ben+afleck"))))
357+
(time (do (dotimes [x 10]
358+
(<!! (http-get "http://www.imdb.com/xml/find?json=1&q=ben+afleck")))
359+
nil))
388360

389-
(time (let [chans (doall (for [x (range 10)]
390-
(http-get "http://www.imdb.com/xml/find?json=1&q=ben+afleck")))]
391-
(doseq [c chans]
392-
(<!! c))))
361+
(time (do (->> (for [x (range 10)]
362+
(http-get "http://www.imdb.com/xml/find?json=1&q=ben+afleck"))
363+
doall
364+
async/merge
365+
(async/take 10)
366+
(async/into [])
367+
<!!)
368+
nil))
393369

394370

395371

@@ -591,6 +567,7 @@
591567

592568
(ns cljs-examples
593569
(:require [cljs.core.async :refer [chan put! take! timeout] :as async]
570+
[clojure.walk :refer [prewalk]]
594571
[goog.net.XhrIo])
595572
(:require-macros [cljs.core.async.macros :refer [go]]))
596573

@@ -610,19 +587,6 @@
610587
(def canvas (.getElementById js/document "canvas"))
611588

612589

613-
#_(defn ptake [chans]
614-
(let [chanc (count chans)
615-
a (atom chanc)
616-
results (atom (vec (repeat chanc nil)))
617-
result-chan (chan)]
618-
(dotimes [x chanc]
619-
(take! (nth chans x)
620-
(fn [v]
621-
(swap! results assoc x v)
622-
(if (= 0 (swap! a dec))
623-
(put! result-chan @results)))))
624-
result-chan))
625-
626590
(def colors ["#FF0000"
627591
"#00FF00"
628592
"#0000FF"
@@ -650,12 +614,26 @@
650614

651615
;;; Same HTTP Examples, but in the browser
652616

617+
(defn fixup-keys [x]
618+
(prewalk
619+
(fn [x]
620+
(if (map? x)
621+
(zipmap (map keyword (keys x))
622+
(vals x))
623+
x))
624+
x))
625+
626+
;; IO is a tad different in JS, so this function has to be re-written
627+
;; for ClojureScript.
653628
(defn http-get [url]
654629
(let [c (chan 1)]
655630
(goog.net.XhrIo/send url (fn [e]
656-
(let [xhr (.-target e)
657-
obj (.getResponseJson xhr)]
658-
(put! c (js->clj obj)))))
631+
(->> e
632+
.-target
633+
.getResponseJson
634+
js->clj
635+
fixup-keys
636+
(put! c))))
659637
c))
660638

661639

@@ -700,19 +678,16 @@
700678

701679
(avg [1 2 3 4 5])
702680

703-
(defn get-helper [k col]
704-
(get col k))
705-
706681
(defn avg-cast-popularity [id]
707682
(go
708683
(let [cast (->> (movie-cast id)
709684
<!
710-
(get-helper "cast")
711-
(clojure.core/map (partial get-helper "id"))
685+
:cast
686+
(clojure.core/map :id)
712687
(clojure.core/map people-by-id)
713688
(async/map vector)
714689
<!
715-
(clojure.core/map (partial get-helper "popularity"))
690+
(clojure.core/map :popularity)
716691
avg)]
717692
cast)))
718693

0 commit comments

Comments
 (0)