Hacker News new | ask | show | jobs
by nstbayless 1094 days ago
"Deep coroutines:" where you can yield from a function called from the coroutine. Lua supports this, but python doesn't (as far as I can tell). Is there a term for this?

To the author: you could make the code even cleaner by moving the yield to within the action functions. Though maybe this won't work as well for parallel actions...

2 comments

In practice we actually do; I had to simplify for the article. We have a few utilities like a `runUntilDone` that make simple sequences easier to write. Example: https://github.com/frc-2175/2023RobotCode/blob/main/src/lua/...

I suppose we could make more utilities for running coroutines "in parallel", but I haven't really felt the need. At that point we usually have to worry about exit conditions and it feels natural to just write a loop.

For Python generators, you can yield from called functions by using yield from as in this (quick, not stellar) example:

  def first():
      yield 1
      yield from second()
      yield 4

  def second():
      yield 2
      yield 3

  print(list(first())) # collects all the results and prints them
  # Output: [1, 2, 3, 4]
But yeah, it doesn't work on a direct function call you have to know it's going to return a generator (or an iterable, like if it returns a list):

  def something():
     yield from something_else()

  def something_else():
     return [1,2,3,4]