Last active
August 29, 2015 14:02
-
-
Save kouphax/fda4f36f1cfc48a265fe to your computer and use it in GitHub Desktop.
Filter Example; Pub Sub Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns boston.core | |
(:require [cljs.core.async :as async :refer [chan <! >! merge timeout filter<]]) | |
(:require-macros [cljs.core.async.macros :as m :refer [go-loop go]])) | |
(enable-console-print!) | |
(def in-channel-one (chan)) | |
(def in-channel-two (chan)) | |
(def in-channel-three (chan)) | |
(defn randomly-constantly [c cid] | |
(go-loop [] | |
(<! (timeout (* 1000 (rand-int 4)))) | |
(>! c (+ (* 10 cid) (rand-int 10))) | |
(recur))) | |
(randomly-constantly in-channel-one 1) | |
(randomly-constantly in-channel-two 2) | |
(randomly-constantly in-channel-three 3) | |
(def out-channel | |
(merge [in-channel-one | |
in-channel-two | |
in-channel-three])) | |
(defn filter-odds [c] | |
(filter< even? c)) | |
(def only-evens | |
(filter-odds out-channel)) | |
(go-loop [] | |
(println (<! only-evens)) | |
(recur)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns boston.core | |
(:require [cljs.core.async :as async :refer [chan <! >! merge timeout pub sub]]) | |
(:require-macros [cljs.core.async.macros :as m :refer [go-loop go]])) | |
(enable-console-print!) | |
(def pub-channel (chan)) | |
(def sub-channel (chan)) | |
(def publication | |
(pub pub-channel (fn [v] (:topic v)))) | |
; sub channel listens to topic :sausages | |
(sub publication :sausages sub-channel) | |
(defn randomly-constantly [c] | |
(go-loop [] | |
(<! (timeout (* 1000 (rand-int 4)))) | |
(let [output { :topic (if (even? (rand-int 2)) :sausages :not-sausages) | |
:value (rand-int 10) }] | |
(println "Generated " output) | |
(>! c output)) | |
(recur))) | |
(randomly-constantly pub-channel) | |
(go-loop [] | |
(println (<! sub-channel)) | |
(recur)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment