入門core.async
- 24. timeout
(let [t (timeout 100)
begin (System/currentTimeMillis)]
(<!! t)
(println “Waited”
(- (System/currentTimeMillis)
begin)))
;; Waited 100
タイムアウト用のチャネルを生成する
- 27. alts!
一度に複数のチャネルから待ち受ける
(let [c1 (chan)
c2 (chan)]
(go (while true
(let [[v ch] (alts! [c1 c2])]
(println “received" v))))
(go (>! c1 "hi"))
(go (>! c2 "there")))
;; received hi
;; received there
待ち受けるチャネル
受信した値と受信元のチャネルが返る
- 34. 高レベルAPI
その他、into, merge, reduce, take 等が用意
されている
(defn split [p ch]
(let [tc (chan), fc (chan)]
(go (loop []
(let [v (<! ch)]
(if (nil? v)
(do (close! tc) (close! fc))
(when (>! (if (p v) tc fc) v)
(recur))))))
[tc fc]))
c.c.a/splitの定義(一部簡略化)
- 35. deprecated API vs transducers
高レベルAPIで提供されるような、チャネルか
ら得られる値を加工したいケースは多い
channel ??? channel
- 36. deprecated API vs transducers
core.asyncではチャネル用のmapやfilterも提
供していた
(let [ch (to-chan (range))]
[(<!! ch) (<!! ch) (<!! ch)])
;=> [0 1 2]
(let [ch (->> (to-chan (range))
(map< inc)
(map< #(* % %)))]
[(<!! ch) (<!! ch) (<!! ch)])
;=> [1 4 9]
- 37. deprecated API vs transducers
Clojure 1.7以降ではtransducerを使って同様の処理が書ける
チャネル生成時にtransducerを指定するとチャネルから取得
できる値はtransducerを適用した結果の値になる
(let [xform (comp (map inc)
(map #(* % %)))
ch (pipe (to-chan (range))
(chan 1 xform))]
[(<!! ch) (<!! ch) (<!! ch)])
;=> [1 4 9]
transducer