Last active
December 28, 2015 08:39
-
-
Save viperscape/7473059 to your computer and use it in GitHub Desktop.
Quickly look at a core.async channel with only momentary blocking, pulls the data if it exists and thus alters the channel.Drain! will repeatedly call peek! until a maximum number of elements is pulled or until the channel is empty.
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
;;uses core.async and refers chan, >!, go, alts!! and timeout functions | |
(def c (chan)) | |
(go (>! c {:msg "hi"})) | |
(defn peek! [ch & args] | |
"quickly looks at a channel for new incoming data, pulls it if it exists, quits otherwise; | |
returns seq if specifying max elements to pull" | |
(if-not (nil? args) | |
(let [s (remove nil? (repeatedly (first args) #(peek! ch)))] (if-not (empty? s) s nil)) | |
(let [[m ch] (alts!! [ch (timeout 0)])] m))) | |
(peek! c) ;; {:msg "hi"} | |
(go (>! c {:msg "hi"})) | |
(go (>! c {:msg "hey"})) | |
(go (>! c {:msg "hello"})) | |
(go (>! c {:msg "holla"})) | |
(peek! c 20) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment