Skip to content

Instantly share code, notes, and snippets.

@tj
Created April 5, 2012 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tj/2306984 to your computer and use it in GitHub Desktop.
Save tj/2306984 to your computer and use it in GitHub Desktop.
duff's device
#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());
}
1
3
5
7
9
11
13
15
17
19
@visnup
Copy link

visnup commented Apr 14, 2012

that's so horrible. :)

@tj
Copy link
Author

tj commented Apr 14, 2012

you love it hahaha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment