I share the author's enthusiasm for coroutines. They're nice abstractions for all sorts of state-machine-like code and for concurrency (without parallelism).<p>> You could allocate a piece of memory for a coroutine stack; let the coroutines on it push and pop stack frames like ordinary function calls; and have a special ‘yield’ function that swaps out the stack pointer and switches over to executing on another stack. In fact, that’s not a bad way to add coroutines to a language that doesn’t already have them, because it doesn’t need the compiler to have any special knowledge of what’s going on. You could add coroutines to C in this way if you wanted to, and the approach would have several advantages over my preprocessor system.<p>In C minicoro is a nice library that provides just that: <a href="https://github.com/edubart/minicoro" rel="nofollow">https://github.com/edubart/minicoro</a><p>In Zig there's zigcoro: <a href="https://github.com/rsepassi/zigcoro" rel="nofollow">https://github.com/rsepassi/zigcoro</a><p>Another source I found enlightening on coroutines is "Coroutines in Lua": <a href="https://www.lua.org/doc/jucs04.pdf" rel="nofollow">https://www.lua.org/doc/jucs04.pdf</a>
I’ve been fascinated by coroutine code for ages – this was a great read.<p>For one of the best work projects I’ve done, I used Redux-Saga. It was so elegant for a heavily live-updating ui app. Using coroutines for making API requests, watching sockets, sending and receiving updates, and reacting to data conflicts felt so natural and all just came together so beautifully.<p>The term he uses for “turning functions inside out” is spot on for how they feel. In particular, the way a good coroutine only focuses on the pure state, while being independent of side effects and even time itself really expanded my view of code. It made testing and reasoning about logic so much easier too. It’s a model I really wish was more widespread in languages and the industry.
One past discussion:<p><a href="https://news.ycombinator.com/item?id=37354401">https://news.ycombinator.com/item?id=37354401</a> - September 2023 (62 comments)
Something which bothers me about coroutines is the pure boilerplate -- I don't even mean the ridiculous verbose coroutine definitions in C++20 but just the spread of "await" and "async" keywords all over the code, for instance in Python and Rust. I understand that these keywords are the compromise to introduce the feature into the language without paying for it when not using it, but I wonder if there is not a more concise way. Something not mentioned at all in the particle is the "what color is your function" problem (<a href="https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/" rel="nofollow">https://journal.stuffwithstuff.com/2015/02/01/what-color-is-...</a>) where I wonder whether there is a good solution.
> For example, here’s a tiny Python generator function that I put in a lot of my programs, because I often find I want to use it, and despite being so simple, it’s not in the standard library:<p>itertools.pairwise[0] comes pretty close.<p>[0] <a href="https://docs.python.org/3/library/itertools.html#itertools.pairwise" rel="nofollow">https://docs.python.org/3/library/itertools.html#itertools.p...</a>
I really don't like writing state machines by hand... So...<p>I was writing a web-based text adventure game, using async / await on the server...<p>I was writing a SDML text adventure game, with some graphics, using async / await on the control side...<p>Then I finally realized that since all of my commands are like Console WriteLine and Console ReadLine... that I could just do all of my logic in a thread of its own. And it stores UI state in a object... and uses two AutoResetEvents to keep track of which thread is in control.<p>I guess those are coroutines. One for logic and one for UI.<p>Makes my logic code as simple as could be, with no async / await or any other garbage. (I handle a couple top-level exceptions for closing connections or closing the app.)<p>And it's also trivial to test my code on an actual Console, before worrying about setting up my web server or my GUI environment, which has a bit of latency to it.