Fork me on GitHub
Ivan Koz09:04:32

Practicing simple async stuff is there any errors in my code, what can be improved? I know about transducers.

(defn channel-transform
  (fn [in]
    (let [out (chan)]
      (go-loop []
        (if-let [v (<! in)]
          (do (>! out (f v))
          (close! out)))

(def upper-caser (channel-transform str/upper-case))
(def reverser (channel-transform str/reverse))

  (let [in (async/to-chan ["hello" "world"])
        out (reverser (upper-caser in))]
    (go-loop []
      (when-let [v (<! out)]
        (println v)


@nxtk i like it, I assume f can't throw an exception and it's ok for out to not have a buffer?

Ivan Koz17:04:54

@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

Ivan Koz17:04:01

whats the difference?

Ivan Koz17:04:17

oh true vs not nil, so if-let fails on false bool


right, if in has a false on it, if-some will work the way you expect

Ivan Koz17:04:48

i guess putting false onto a channel is the same as storing logical false in a set


for most of my code it doesn't matter but it's an interesting corner case

Ivan Koz17:04:02

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

Ivan Koz17:04:56

its mostly basic stuff, in this one

Ivan Koz18:04:51

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