This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-18
Channels
- # adventofcode (62)
- # aws (5)
- # beginners (59)
- # calva (63)
- # cider (26)
- # cljdoc (1)
- # cljsrn (22)
- # clojure (99)
- # clojure-austin (1)
- # clojure-dev (19)
- # clojure-europe (4)
- # clojure-hamburg (2)
- # clojure-italy (3)
- # clojure-nl (23)
- # clojure-spec (2)
- # clojure-uk (85)
- # clojurescript (41)
- # core-async (17)
- # cursive (20)
- # data-science (11)
- # datascript (2)
- # datomic (31)
- # emacs (7)
- # figwheel (28)
- # figwheel-main (12)
- # graphql (2)
- # hyperfiddle (3)
- # juxt (1)
- # kaocha (2)
- # leiningen (5)
- # nrepl (13)
- # off-topic (45)
- # pathom (13)
- # pedestal (11)
- # re-frame (20)
- # reagent (10)
- # shadow-cljs (92)
- # spacemacs (9)
- # sql (39)
- # tools-deps (32)
- # unrepl (3)
can someone help me with this thing:
I’m trying to read off a channel and then if :updated-at
key of the value is less a second ago, then re-push the entire record back to the channel.
Been trying with go-loop
, split
, other things and still can’t figure this out
So idea is to wait until :updated-at
is in the past (for at least a second) and then return the value, otherwise keep the channel parked.
So if then queried with <!!
it would block until :updated-at
is 1sec ago
I feel like I’d want to write a fn that takes an input channel and returns a new output channel:
(defn stalify
[clock in]
(let [out (async/chan)]
(async/go-loop []
(let [x (async/<! in)]
(if (some? x)
(do
(when-let [pause-ms (compute-pause-if-necessary clock x)]
(async/<! (async/timeout pause-ms)))
(when (async/>! out x)
(recur)))
(async/close! out))))
out))
(Disclaimer: I’ve only written this, not tested it.)
@donaldball I don’t think I get this part though:
(when-let [pause-ms (compute-pause-if-necessary clock x)]
(async/<! (async/timeout pause-ms)))
But how exactly. I see it creates a channel that automatically closes after some time, what is it taking out of it though?
@ag it takes nothing out of it, when a channel closes the result of (async/<! (async/timeout pause-ms))
is nil
still don’t get it… sorry… it’s been long day and I’ve been dealing with this thing for a while now
@ag i wrote a blog post that goes into the details of it, maybe it'll help: http://danboykis.com/posts/core-async-timeout-channels/
why when I do something like this it doesn’t work as I expect:
(go-loop []
(let [x (<! ch)]
(when (some? x)
(>! ch x))
(recur)))
yes, I wanted to intentionally cause an infinite loop, but it’s not happening and I’m not sure why
@ag the (>! ch x)
is parking until something takes the value. that never happens since (presumably) the only thing taking from the channel is this same loop. use put!
instead of >!
or use a channel with a buffer of at least 1.