Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Coroutine
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Generators=== {{Main|Generator (computer programming)}} Generators, also known as semicoroutines,<ref name="Ralston2000">{{cite book|author=Anthony Ralston|title=Encyclopedia of computer science|url=https://books.google.com/books?id=yQ9LAQAAIAAJ|access-date=11 May 2013|year=2000|publisher=Nature Pub. Group|isbn=978-1-56159-248-7}}</ref> are a subset of coroutines. Specifically, while both can yield multiple times, suspending their execution and allowing re-entry at multiple entry points, they differ in coroutines' ability to control where execution continues immediately after they yield, while generators cannot, instead transferring control back to the generator's caller.<ref>See for example ''[https://docs.python.org/reference/index.html The Python Language Reference] {{Webarchive|url=https://web.archive.org/web/20121024054933/https://docs.python.org/reference/index.html |date=2012-10-24 }}'' "https://docs.python.org/reference/expressions.html#yieldexpr {{Webarchive|url=https://web.archive.org/web/20121026064102/https://docs.python.org/reference/expressions.html#yieldexpr |date=2012-10-26 }} 5.2.10. Yield expressions]":<br /> "All of this makes generator functions quite similar to coroutines; they yield multiple times, they have more than one entry point and their execution can be suspended. The only difference is that a generator function cannot control where should the execution continue after it yields; the control is always transferred to the generator's caller."</ref> That is, since generators are primarily used to simplify the writing of [[iterator]]s, the <code>yield</code> statement in a generator does not specify a coroutine to jump to, but rather passes a value back to a parent routine. However, it is still possible to implement coroutines on top of a generator facility, with the aid of a top-level dispatcher routine (a [[trampoline (computing)|trampoline]], essentially) that passes control explicitly to child generators identified by tokens passed back from the generators: ''var'' q := new queue '''generator''' produce '''loop''' '''while''' q is not full create some new items add the items to q '''yield''' '''generator''' consume '''loop''' '''while''' q is not empty remove some items from q use the items '''yield''' '''subroutine''' dispatcher ''var'' d := new dictionary('''generator''' β '''iterator''') d[produce] := '''start''' consume d[consume] := '''start''' produce ''var'' current := produce '''loop''' '''call''' current current := '''next''' d[current] '''call''' dispatcher A number of implementations of coroutines for languages with generator support but no native coroutines (e.g. Python<ref name="MertzIBM"/> before 2.5) use this or a similar model.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)