Got it working with coroutines! I even managed to ...
# advent-of-code
t
Got it working with coroutines! I even managed to change my
IntCodeComputer
so that Day 2 and 5 work without changes to the caller. Inputs were changed from Lists to Channels (I didn't do the Receive/Send channel thing, but I should probably). To catch the final output of E, I wrote a dongle so to speak that listens on a channel and remembers the last value it received before passing it on. So I can do dongle(outputFromE, inputToA).
👍 2
j
Channel
implements both the
ReceiveChannel
and the
SendChannel
interface, so it's really just a matter of using the proper one as your parameter type for some extra type safety and signaling the intended use of the channel.
t
Yeah, I'm getting there. I miiiiiiight punt on that part because I've already spent a lot of time on this and I still need to write about it, and my wife has been VERY patient with me today so far. 🙂
j
Haha fair enough, and it's not like there isn't enough to write about already
s
Lovely work as always, @todd.ginsberg! I especially like the Spy you created (I cheated and cached last output inside my computer). It looks like a broadcast channel could be used as a many-to-one spy, perhaps?
Also 👏 for the retrofits needed to keep it compatible with earlier days
I've been using coroutines a little at work but the use-cases tend to be much more straightforward than this so it's nice to really see what they can do!
I wonder if `joinAll`/`launch` fits slightly better than `awaitAll`/`async` since you don't care about a result? But with launch I needed to specify a dispatcher each time
t
Oh, maybe so @stkent! I've only played with coroutines with no real end goal, so my use of them might not be 100% idiomatic. I do like that Spring + Kotlin uses coroutines to hide all the complicated reactive stuff. Reactivity with imperative code style.
j
Yeah, you can just
launch
them all, and the coroutineScope you declared won't finish until all the launched coroutines have, so you wouln't need any join or await at all
😍 1
t
Oooooh that's awesome.
Fundamentally misunderstood that.
j
Great writeup as always, btw, clear and to the point!
t
Thanks! I had like 1000 things going on yesterday afternoon so I'm happy I even got it done at all. 🙂
k
Interesting writeup, thanks! I wonder if you could replace the spy thing with a BroadCastChannel or something like that, but I must say the docs are kind of overwhelming.
j
That's exactly what I did, the final amplifier send it's output to a BroadcastChannel, with one subscription flowing back into the first amplifier, the other to a bit of code that saved the final value and reported it back
t
Hmmmm, I'll have to play with that. Sounds simpler.