This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-28
Channels
- # adventofcode (2)
- # bangalore-clj (3)
- # beginners (171)
- # boot (28)
- # chestnut (3)
- # cljs-dev (20)
- # cljsjs (5)
- # clojure (280)
- # clojure-austin (1)
- # clojure-czech (1)
- # clojure-dev (9)
- # clojure-dusseldorf (2)
- # clojure-greece (20)
- # clojure-italy (6)
- # clojure-poland (16)
- # clojure-russia (7)
- # clojure-serbia (4)
- # clojure-sg (1)
- # clojure-spec (18)
- # clojure-uk (153)
- # clojurescript (57)
- # core-async (9)
- # cursive (21)
- # data-science (29)
- # datomic (18)
- # dirac (8)
- # docker (6)
- # duct (1)
- # emacs (50)
- # fulcro (15)
- # hoplon (56)
- # klipse (3)
- # leiningen (14)
- # lumo (1)
- # off-topic (5)
- # onyx (13)
- # other-languages (14)
- # pedestal (1)
- # perun (5)
- # planck (17)
- # re-frame (10)
- # reagent (2)
- # ring (1)
- # spacemacs (51)
- # sql (14)
- # test-check (16)
- # testing (1)
- # unrepl (93)
is there a common pattern where a function returns either a channel that will receive the result of some computation or the result itself immediately
for instance if the result is cached in memory
@nickmbailey creating channels is cheap, so we generally just have use cases like that always return a channel.
NB: Creating a channel is cheap, go
blocks are not, so don't just do (go val)
, but actually create a channel and put your value onto it. That way your calling functions don't have to do a check to see if they returned a channel or not
We use a helper function for this behavior, which might help you unless someone else has a better suggestion:
(defn val-chan
"Helper function to make a value available on a channel without invoking
`go`. Significantly faster alternative, since it doesn't require thread
switching."
[value]
(let [c (async/chan 1)]
(when (some? value)
(async/put! c value))
(async/close! c)
c))
Do you typically have callers check if a value is immediately available?
My use case is a rest API and I want to return a 200 response with the value if it’s immediately available and a 202 with an id if it will need to be processed
https://clojuredocs.org/clojure.core.async/poll! ; a non-blocking take from a channel that will return nil
if if thing isn't yet available
go blocks aren't that expensive
something like (go 1)
is probably about as expensive as adding something to a hashmap