TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Philosophy of Coroutines (2023)

96 pointsby HeliumHydrideabout 2 months ago

7 comments

prnglabout 2 months ago
I share the author&#x27;s enthusiasm for coroutines. They&#x27;re nice abstractions for all sorts of state-machine-like code and for concurrency (without parallelism).<p>&gt; 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:&#x2F;&#x2F;github.com&#x2F;edubart&#x2F;minicoro" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;edubart&#x2F;minicoro</a><p>In Zig there&#x27;s zigcoro: <a href="https:&#x2F;&#x2F;github.com&#x2F;rsepassi&#x2F;zigcoro" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rsepassi&#x2F;zigcoro</a><p>Another source I found enlightening on coroutines is &quot;Coroutines in Lua&quot;: <a href="https:&#x2F;&#x2F;www.lua.org&#x2F;doc&#x2F;jucs04.pdf" rel="nofollow">https:&#x2F;&#x2F;www.lua.org&#x2F;doc&#x2F;jucs04.pdf</a>
评论 #43497522 未加载
评论 #43497530 未加载
评论 #43499111 未加载
tkcrannyabout 2 months ago
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.
Jtsummersabout 2 months ago
One past discussion:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37354401">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37354401</a> - September 2023 (62 comments)
ktpsnsabout 2 months ago
Something which bothers me about coroutines is the pure boilerplate -- I don&#x27;t even mean the ridiculous verbose coroutine definitions in C++20 but just the spread of &quot;await&quot; and &quot;async&quot; 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 &quot;what color is your function&quot; problem (<a href="https:&#x2F;&#x2F;journal.stuffwithstuff.com&#x2F;2015&#x2F;02&#x2F;01&#x2F;what-color-is-your-function&#x2F;" rel="nofollow">https:&#x2F;&#x2F;journal.stuffwithstuff.com&#x2F;2015&#x2F;02&#x2F;01&#x2F;what-color-is-...</a>) where I wonder whether there is a good solution.
评论 #43505929 未加载
评论 #43537602 未加载
asplakeabout 2 months ago
Recently updated: [Simon Tatham, initial version 2023-09-01, last updated 2025-03-25]
robertlagrantabout 2 months ago
&gt; 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:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;itertools.html#itertools.pairwise" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;itertools.html#itertools.p...</a>
VikingCoderabout 2 months ago
I really don&#x27;t like writing state machines by hand... So...<p>I was writing a web-based text adventure game, using async &#x2F; await on the server...<p>I was writing a SDML text adventure game, with some graphics, using async &#x2F; 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 &#x2F; await or any other garbage. (I handle a couple top-level exceptions for closing connections or closing the app.)<p>And it&#x27;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.
评论 #43500262 未加载
评论 #43506862 未加载