Fork me on GitHub
#core-async
<
2019-04-28
>
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
  [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)))))

danboykis17:04:34

@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

danboykis17:04:20

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

danboykis17:04:20

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

danboykis17:04:13

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

danboykis17:04:41

mind sharing the examples in go?

danboykis17:04:58

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?

markmarkmark18:04:05

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

markmarkmark18:04:40

note that untap-all doesn't take a collection of channels to untap