Practicing simple async stuff is there any errors in my code, what can be improved? I know about transducers.
(defn channel-transform
[f]
(fn [in]
(let [out (chan)]
(go-loop []
(if-let [v (<! in)]
(do (>! out (f v))
(recur))
(close! out)))
out)))
(def upper-caser (channel-transform str/upper-case))
(def reverser (channel-transform str/reverse))
(comment
(let [in (async/to-chan ["hello" "world"])
out (reverser (upper-caser in))]
(go-loop []
(when-let [v (<! out)]
(println v)
(recur)))))
@nxtk i like it, I assume f
can't throw an exception and it's ok for out
to not have a buffer?
also, there's an if-some
https://clojuredocs.org/clojure.core/if-some
@danboykis its just an example i don't have any requirements, but that interesting to think about
for a lot of my code i ended up replacing if-let's with if-some's
whats the difference?
oh true vs not nil, so if-let fails on false bool
gotcha
right, if in
has a false
on it, if-some will work the way you expect
i guess putting false onto a channel is the same as storing logical false in a set
ouch
for most of my code it doesn't matter but it's an interesting corner case
watched few rob pike's talks, translating go examples into clojure atm
mind sharing the examples in go?
maybe there's a cool pattern i am not aware of that i should be using
its mostly basic stuff, in this one
any reason for missing counterparts of untap-all and unmix-all to add collections\seqs of channels to a mix\mult?
presumably because untap-all is able to untap channels you potentially don't even know about, but if you're using tap you have to know all the channels and can just map over the collection
note that untap-all doesn't take a collection of channels to untap
@markmarkmark thanks