generator functions in another language

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 5 01:28:30 EDT 2008


En Mon, 05 May 2008 00:09:02 -0300, hdante <hdante at gmail.com> escribió:

>  Isn't this guy a bot ? :-) It's learning fast. I believe there is a
> "frame" in C, composed of its stack and globals. For generators in C,
> you may look for "coroutines". For example, see:
>
> http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
>
>  A sample code follows:
>
> #define crBegin static int state=0; switch(state) { case 0:
> #define crReturn(i,x) do { state=i; return x; case i:; } while (0)
> #define crFinish }
> int function(void) {
>     static int i;
>     crBegin;
>     for (i = 0; i < 10; i++)
>         crReturn(1, i);
>     crFinish;
> }
>
>  The "suspended state" is saved in the static variable. It's not
> necessary to save the complete "frame", only the suspended state.

Quoting the author himself, "this is the worst piece of C hackery ever seen"

-- 
Gabriel Genellina




More information about the Python-list mailing list