This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-02-08
Channels
- # aleph (11)
- # arachne (7)
- # aws (1)
- # bangalore-clj (4)
- # beginners (24)
- # boot (128)
- # bristol-clojurians (23)
- # cider (1)
- # cljs-dev (43)
- # cljsrn (6)
- # clojure (178)
- # clojure-austin (3)
- # clojure-chicago (1)
- # clojure-dusseldorf (14)
- # clojure-finland (15)
- # clojure-france (6)
- # clojure-italy (18)
- # clojure-portugal (2)
- # clojure-russia (67)
- # clojure-spec (148)
- # clojure-uk (55)
- # clojurescript (199)
- # core-async (4)
- # cursive (18)
- # datascript (5)
- # datomic (120)
- # devcards (3)
- # dirac (53)
- # emacs (11)
- # events (3)
- # gsoc (7)
- # jobs (1)
- # lein-figwheel (25)
- # leiningen (5)
- # lumo (12)
- # off-topic (29)
- # om (174)
- # om-next (2)
- # onyx (7)
- # perun (10)
- # protorepl (6)
- # re-frame (12)
- # remote-jobs (1)
- # ring (19)
- # ring-swagger (25)
- # rum (6)
- # spacemacs (13)
- # sql (3)
- # untangled (88)
- # yada (7)
I made some changes to make it work correctly with closed channels, here is what I have so far for my two utility functions:
(defn map-pipe
"Takes values from in-chan and puts a mapped value to out-chan if it exists. This map can be updated at any time by passing a function of one argument through update-chan. If update-chan or out-chan is closed, values are no longer taken from in-chan or put to out-chan. If in-chan is closed, out-chan is closed but update-chan is not."
[in-chan out-chan update-chan]
(async/go-loop [store {}]
(let [[[k v :as msg] chan] (async/alts! [update-chan in-chan] :priority true)]
(if (= chan update-chan)
(when-not (nil? msg)
(recur (msg store)))
(if (nil? msg)
(close! out-chan)
(do
(when-let [v (get store k)]
(when (async/>! out-chan v)
(recur store)))
(recur store)))))))
(defn throttle-pipe
"Pipes a finite number of values from in-chan to out-chan. This number can be updated at any time by passing a function of one argument through update-chan. If update-chan or out-chan is closed, values are no longer piped. If in-chan is closed, out-chan is closed but update-chan is not."
[in-chan out-chan update-chan]
(async/go-loop [allowance 0]
(let [[msg chan] (async/alts! (if (< 0 allowance) [update-chan in-chan] [update-chan]) :priority true)]
(if (= chan update-chan)
(when-not (nil? msg)
(recur (msg allowance)))
(if (nil? msg)
(close! out-chan)
(when (async/>! out-chan msg)
(recur (dec allowance))))))))
I also renamed them to better represent what they actually are. I was wondering if it seems weird to not close the pipes if the update-chan is closed though. What do you think?