So I’m calling a library that gives me a core.async channel. I want to (lazily) map a function over this channel before returning it to the calling code. How can I make this happen?
I’d use core.async/map
, but it seems to be eager (e.g. (def ch (a/map inc [(a/to-chan (range 1000000000))]))
seems to hang)
Maybe with a pipeline+transducer: https://stackoverflow.com/questions/31286167/how-to-create-a-channel-from-another-with-transducers
Though I'm not sure why a/map
is not working for you
(def ch (a/map (fn [x] (println x) (inc x)) [(a/to-chan (range 1000000000))]))
=> #'user/ch
0
(a/<!! ch)
=> 1
1
Okay so I looked into this a bit closer. I’m also confused as to why map
is hanging, since it’s definitely not eager (I verified this by mapping a function that prints a message). The same goes for pipeline
. Either way, I decided to stick to map
; hopefully it won’t hang in more realistic scenarios 🙂 Thanks @fmjrey
Maybe you're experiencing chunking http://clojure-doc.org/articles/language/laziness.html#lazy-sequences-chunking
@wombawomba above, isn't a/to-chan going to hang? map's laziness doesn't come into play
that's not the issue, never mind