|
10 | 10 | (def c (chan))
|
11 | 11 |
|
12 | 12 |
|
13 |
| - |
| 13 | +(chan) |
14 | 14 | ;; We can attach listeners via take!
|
15 | 15 |
|
16 | 16 | (take! c (fn [v] (println v)))
|
|
107 | 107 |
|
108 | 108 | (go (println "It works!" (<! (go 42))))
|
109 | 109 |
|
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 |
111 | 111 | ;; blocking promises to wait for channel values. That would mess up the fixed
|
112 | 112 | ;; size thread pool that go blocks dispatch in. So we must define two sets
|
113 | 113 | ;; of operations
|
|
173 | 173 |
|
174 | 174 | (<!! sbc) ;; returns 2
|
175 | 175 |
|
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 |
204 | 177 |
|
205 |
| -(>!! dbbc 0) |
206 |
| -(>!! dbbc 1) |
207 |
| -(>!! dbbc 2) |
| 178 | +(def c (chan)) |
208 | 179 |
|
209 |
| -(println @a-size) |
| 180 | +(close! c) |
210 | 181 |
|
211 |
| -(println (<!! dbbc) @a-size) |
| 182 | +(<!! c) |
212 | 183 |
|
213 | 184 | ;;;; Alt & Timeout ;;;;
|
214 | 185 |
|
|
225 | 196 | ;; Timeout is a channel that closes after X ms
|
226 | 197 | ;; (close! (chan 42))
|
227 | 198 |
|
228 |
| -(<!! (timeout 1000)) |
| 199 | +(<!! (timeout 2000)) |
229 | 200 |
|
230 | 201 | ;; Often used with alt
|
231 | 202 |
|
|
383 | 354 | (<!! (avg-cast-popularity 238))
|
384 | 355 |
|
385 | 356 |
|
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)) |
388 | 360 |
|
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)) |
393 | 369 |
|
394 | 370 |
|
395 | 371 |
|
|
591 | 567 |
|
592 | 568 | (ns cljs-examples
|
593 | 569 | (:require [cljs.core.async :refer [chan put! take! timeout] :as async]
|
| 570 | + [clojure.walk :refer [prewalk]] |
594 | 571 | [goog.net.XhrIo])
|
595 | 572 | (:require-macros [cljs.core.async.macros :refer [go]]))
|
596 | 573 |
|
|
610 | 587 | (def canvas (.getElementById js/document "canvas"))
|
611 | 588 |
|
612 | 589 |
|
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 |
| - |
626 | 590 | (def colors ["#FF0000"
|
627 | 591 | "#00FF00"
|
628 | 592 | "#0000FF"
|
|
650 | 614 |
|
651 | 615 | ;;; Same HTTP Examples, but in the browser
|
652 | 616 |
|
| 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. |
653 | 628 | (defn http-get [url]
|
654 | 629 | (let [c (chan 1)]
|
655 | 630 | (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)))) |
659 | 637 | c))
|
660 | 638 |
|
661 | 639 |
|
|
700 | 678 |
|
701 | 679 | (avg [1 2 3 4 5])
|
702 | 680 |
|
703 |
| -(defn get-helper [k col] |
704 |
| - (get col k)) |
705 |
| - |
706 | 681 | (defn avg-cast-popularity [id]
|
707 | 682 | (go
|
708 | 683 | (let [cast (->> (movie-cast id)
|
709 | 684 | <!
|
710 |
| - (get-helper "cast") |
711 |
| - (clojure.core/map (partial get-helper "id")) |
| 685 | + :cast |
| 686 | + (clojure.core/map :id) |
712 | 687 | (clojure.core/map people-by-id)
|
713 | 688 | (async/map vector)
|
714 | 689 | <!
|
715 |
| - (clojure.core/map (partial get-helper "popularity")) |
| 690 | + (clojure.core/map :popularity) |
716 | 691 | avg)]
|
717 | 692 | cast)))
|
718 | 693 |
|
|
0 commit comments