本文介绍了各种语言的Python生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在您最喜欢的语言中模拟Python样式生成器?我在Scheme中找到了。看到其他实现必须很有趣,尤其是那些没有第一类继承的语言。

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.

推荐答案

下面是C ++中使用光纤模拟生成器的示例:

Here is an example in C++ that simulates generators using fibers:

还有原语 C例程来获得类似的结果。

(Lua 使用上述方法实现)

There are also the primitive C routines setjmp, longjmp to achieve similar results.
(Lua coroutines are implemented with the above method)

这篇关于各种语言的Python生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:51