Codebase list core-async-clojure / upstream/0.3.443 examples / ex-altsgo.clj
upstream/0.3.443

Tree @upstream/0.3.443 (Download .tar.gz)

ex-altsgo.clj @upstream/0.3.443

7fdd091
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
(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)