core-async

wombawomba 2020-03-06T08:57:54.233600Z

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?

wombawomba 2020-03-06T09:17:00.235Z

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)

fmjrey 2020-03-06T12:03:15.235600Z

Maybe with a pipeline+transducer: https://stackoverflow.com/questions/31286167/how-to-create-a-channel-from-another-with-transducers

fmjrey 2020-03-06T12:09:30.236100Z

Though I'm not sure why a/map is not working for you

fmjrey 2020-03-06T12:12:19.237300Z

(def ch (a/map (fn [x] (println x) (inc x)) [(a/to-chan (range 1000000000))]))
=> #'user/ch
0

(a/<!! ch)
=> 1
1

wombawomba 2020-03-06T12:39:14.240900Z

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

fmjrey 2020-03-06T12:43:56.241300Z

Maybe you're experiencing chunking http://clojure-doc.org/articles/language/laziness.html#lazy-sequences-chunking

2020-03-06T16:19:39.241700Z

@wombawomba above, isn't a/to-chan going to hang? map's laziness doesn't come into play

2020-03-06T16:25:45.242300Z

that's not the issue, never mind