c语言语法生成器,C中的生成器

我得到了一些我无法理解的代码。

在将pow2s的g替换为地图的gen结构后,我陷入了困境。从那里,我无法看到它如何继续跟踪价值以及如何存储价值。

代码编译并运行。

有人可以帮我理解这段代码吗?谢谢!

PS:我正在学习C语言

它是从以下Python代码翻译而来的:

>>> def pow2s():

yield 1

for i in map((lambda x:2*x),pow2s()):

yield i

>>> def mymap(f,iter):

for i in iter:

yield f(i)和翻译的C代码:

#include

#include

struct gen { // generic structure, the base of all generators

int (*next)() ;

int continue_from ;

} ;

typedef int (*fptr)() ;

// Each iterator has 3 components: a structure, a constructor for the structure,

// and a next function

// map

struct mapgen { // structure for map

int (*next)() ;

int continue_from ; // not really required, provided for compatibility

fptr f ;

struct gen *g ;

} ;

int map_next(struct mapgen *p) { // next function for map

return p->f(p->g->next(p->g)) ;

}

struct gen *map(fptr f, struct gen *g) { // constructor for map iterator

struct mapgen *p = (struct mapgen *)malloc(sizeof(struct mapgen));

p->next = map_next;

p->continue_from = 0;

p->f = f;

p->g = g;

return (struct gen *)p ;

}

// powers of 2

struct pow2s { // structure

int (*next)() ;

int continue_from ;

struct gen *g ;

};

int times2(int x) { // anonymous lambda is translated into this

return 2*x ;

}

struct gen *pow2() ; // forward declaration of constructor

int pow2next(struct pow2s * p){ // next function for iterator

switch(p->continue_from) {

case 0:

p->continue_from = 1;

return 1;

case 1:

p->g = map(times2,pow2()) ;

p->continue_from = 2;

return p->g->next(p->g) ;

case 2:

p->continue_from = 2;

return p->g->next(p->g) ;

}

}

struct gen * pow2() { // constructor for pow2

struct pow2s * p = (struct pow2s *)malloc(sizeof(struct pow2s));

p->next = pow2next;

p->continue_from = 0;

return (struct gen *)p;

}

// in main, create an iterator and print some of its elements.

int main() {

int i ;

struct gen * p = pow2() ;

for(i=0;i<10;i++)

printf("%d ",p->next(p)) ;

printf("\n");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AutoFlowchart Version 2.4.3 ------------------------------------ what's is AutoFlowchart? -------------------------------------------------------------------- AutoFlowchart, the Professional sourcecode flowcharting tool. AutoFlowchart is a excellent tool to generate flowchart from sourcecode.Its flowchart can expand and shrink. and you can pre-define the the width , height,Horizontal spacing and vertical spacing. Move and zoom is also very easy. It can export the flowchart as a Microsoft Visio/Word file or a bitmap file. It can help programmers understand, document and visualize source code. It supports C,C++,VC++(Visual C++ .NET),Delphi(Object Pascal). In the future,It will support more languages. You can use it on Windows 9X/NT/me/XP. You can trial it for 50 times. Registration fee is $129. http://www.ezprog.com/order.htm what's new? -------------------------------------------------------------------- v2.4.3 [2008-07-12] 1. Add process to "return","continue" and "break"; 2. Revised the color when export to coloried Ms Word file; v2.4.1 [2008-04-21] 1. Add "read only" options; 2. Change multi language captions for "open project" and "save project"; v2.4 [2008-04-02] 1. Add "project" to AutoFlowchart to manage more files; v2.3 [2008-01-14] 1. auto show a detail code ; 2. use GDI+ to draw flowchart and draw text; 3. add search function; 4. change the arrowhead shape; 5. remove a bug when in Windows Server 2003; v2.0.3 [2007-12-06] 1. It's use a SDI window for flowchart instead of MDI, then it would be possible to display the code window on one monitor while displaying the flowchart on a second monitor! Thanks for Mr. Shahine Ghanbarzadeh! v2.0.2 [2007-11-29] 1. Add treatment to exceptional such as unmatched brackets. 2. Add treatment to special function which lines is more than 1000. Ms Visio is supported! Get Started -------------------------------------------------------------------- 1. Open a *.pas/*.c/*.cpp/*.afp file; 2. Double click the begin row of any statement ,then you can see a flowchart; 3. Click a apart of the FlowChart ,you can see the part of sentence of this block; 4. change the value of the first spinedit,you can see the current block. Register Notes: If you are a registered user, please put the license file to the install path. Check out our WWW Home Page: http://www.ezprog.com AutoFlowchart can be ordered for $79 from: CompuServe: ShareIt (#197238) Direct: http://www.shareit.com/product.html?productid=197238&languageid=1 Contact -------------------------------------------------------------------- support : support@ezprog.com sales : sals@ezprog.com Msn : support@ezprog.com _______ ____|__ | (R) --| | |------------------- | ____|__ | Association of | | |_| Shareware |__| o | Professionals -----| | |--------------------- |___|___| MEMBER
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值