How do you emulate Python style generators in your favorite language? I found this one in Scheme. It must be interesting to see other implementations, especially in those languages that don't have first-class continuations.
解决方案
Here is an example in C++ that simulates generators using fibers:
The "yield return" iterator is a
language feature that was created for
one reason: simplicity. It is
generally much easier to iterate
across whole collectionl, storing all
context needed in local variables,
rather than crafting a complicated,
custom iterator object that stores its
state across subsequent retrieval
operations.
There are also the primitive C routines setjmp, longjmp to achieve similar results.
(Lua coroutines are implemented with the above method)
本文探讨了如何在不同编程语言中模拟Python风格的生成器。通过使用C++中的纤程(fibers)特性来实现类似的功能,并提及Lua协程的实现方式。文章还讨论了使用setjmp和longjmp等原始方法来达到相似的效果。

被折叠的 条评论
为什么被折叠?



