Created
April 5, 2012 00:53
-
-
Save tj/2306984 to your computer and use it in GitHub Desktop.
duff's device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define coro(b) \ | |
static int __cont = 0; \ | |
switch (__cont) { case 0:; \ | |
b } | |
#define yield(v) \ | |
do { \ | |
__cont = __LINE__; \ | |
return v; case __LINE__:; \ | |
} while (0) | |
int | |
odd() { | |
static int i = 1; | |
coro({ | |
while (1) { | |
yield(i); | |
i += 2; | |
} | |
}) | |
return 0; | |
} | |
int | |
main(void) { | |
for (int i = 0; i < 10; ++i) printf("%d\n", odd()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
3 | |
5 | |
7 | |
9 | |
11 | |
13 | |
15 | |
17 | |
19 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that's so horrible. :)