Fork me on GitHub
#core-async
<
2015-08-11
>
exupero18:08:12

How can I map a hash of keywords to channels to a hash of keywords to the first value on those channels?

exupero18:08:18

I tried

(go
  (into {}
    (for [[k v] m]
      [k (<! v)])))

exupero18:08:54

But for wraps the <! within an inner function and out the jurisdiction of the go macro.

erik_price18:08:42

Totally untested, but I wonder if something like this might work?

(def firsts-only-chans
  (map (fn [[k c]]
         [k (async/take 1 %)])
       my-chans))

(def ks-to-vals-chan
  (map (fn [[k c]]
         (async/map (partial vector k) [c]))
       ks-to-chans))

(async/into {} ks-to-vals-chan)

erik_price18:08:17

The approach at least avoids the problem where code escapes the go block.

erik_price18:08:24

Maybe there is a much more straightforward transducer-based solution to this.

exupero20:08:09

Interesting. Based on that, I tried

(async/into {}
  (async/merge
    (map (fn [[k v]]
           (async/map (partial vector k) [v]))
         chans)))
but so far it's not working.