Python’s xrange generator in C using Co-Routine

Generator using Co-routine in C

int xrange(int n);

int xrange(int n) {
    static int i, state = 0;
    switch (state) {
        case 0: goto BEGIN;
        case 1: goto YIELD;
        case 2: return 0;
    }
BEGIN:
    for (i = 1; i <=n; i++) {
        state =(i==n ? 2 :1);
        return i;
YIELD:;
    }
}

int main()
{
    int i=0,j=0;

    while(i=xrange(10))
    {
        printf("%d\n",i);
    }
    return 0;
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.