如何用您喜欢的语言模拟Python样式生成器?我在Scheme中找到了这个.看到其他实现一定很有趣,特别是那些没有一流延续的语言.
我对语言设计有点迷茫,我现在正在玩自己的爱好语言.(http://rogeralsing.com/2010/04/14/playing-with-plastic/)
让我头脑流血的一件事是"生成器"和"yield"关键字.我知道C#使用AST转换将枚举器方法转换为状态机.
但它在其他语言中如何运作?有没有办法在没有AST转换的语言中获得生成器支持?例如,像Python或Ruby这样的语言是否采用AST转换来解决这个问题?
(问题是如何在不同语言的引擎下实现生成器,而不是如何在其中一个中编写生成器)
我有点理解这一点,至少是生成器的功能(我在Python中使用过它们).我理解switch语句及其内容是如何形成的.但是,我收到了这些错误.
test.cpp: In constructor 'Foo::descent::descent(int)':
test.cpp:46: error: invalid use of nonstatic data member 'Foo::index_'
test.cpp: In member function 'bool Foo::descent::operator()(std::string&)':
test.cpp:50: error: invalid use of nonstatic data member 'Foo::bars_'
test.cpp:50: error: invalid use of nonstatic data member 'Foo::index_'
test.cpp:51: error: invalid use of nonstatic data member 'Foo::index_'
test.cpp:51: error: invalid use of nonstatic data member 'Foo::bars_'
test.cpp:52: error: invalid use of nonstatic data member 'Foo::index_'
Run Code Online (Sandbox Code Playgroud)
这是代码.如果您有更好的方法来解决这个问题,请务必分享.
#include <math.h>
#include <string>
#include <vector>
#include <iostream>
#ifndef __generator_h__
#define __generator_h__
// generator/continuation …Run Code Online (Sandbox Code Playgroud)