core-async

Ivan Koz 2019-04-28T09:01:32.090400Z

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)))))

2019-04-28T17:14:34.091200Z

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

2019-04-28T17:15:49.092200Z

also, there's an if-some https://clojuredocs.org/clojure.core/if-some

Ivan Koz 2019-04-28T17:15:54.092400Z

@danboykis its just an example i don't have any requirements, but that interesting to think about

2019-04-28T17:16:20.092800Z

for a lot of my code i ended up replacing if-let's with if-some's

Ivan Koz 2019-04-28T17:17:01.093500Z

whats the difference?

Ivan Koz 2019-04-28T17:17:17.093700Z

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

Ivan Koz 2019-04-28T17:17:20.093900Z

gotcha

2019-04-28T17:18:20.095200Z

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

Ivan Koz 2019-04-28T17:18:48.095800Z

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

Ivan Koz 2019-04-28T17:19:10.096300Z

ouch

2019-04-28T17:19:13.096500Z

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

Ivan Koz 2019-04-28T17:20:02.097100Z

watched few rob pike's talks, translating go examples into clojure atm

2019-04-28T17:21:41.097600Z

mind sharing the examples in go?

2019-04-28T17:21:58.098Z

maybe there's a cool pattern i am not aware of that i should be using

Ivan Koz 2019-04-28T17:22:37.098200Z

https://www.youtube.com/watch?v=f6kdp27TYZs

Ivan Koz 2019-04-28T17:22:56.098500Z

its mostly basic stuff, in this one

Ivan Koz 2019-04-28T18:02:51.100400Z

any reason for missing counterparts of untap-all and unmix-all to add collections\seqs of channels to a mix\mult?

markmarkmark 2019-04-28T18:35:05.102600Z

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

markmarkmark 2019-04-28T18:35:40.103Z

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

Ivan Koz 2019-04-28T18:39:56.103300Z

@markmarkmark thanks