Created
April 19, 2013 03:52
-
-
Save jdpage/5418032 to your computer and use it in GitHub Desktop.
C continuation passing
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> | |
#include <stdlib.h> | |
#define NEXT(n) ((n)->next(n)) | |
typedef struct co_fib { | |
int (*next)(struct co_fib *); | |
int a; | |
int b; | |
} * co_fib_t; | |
int co_fib_next(struct co_fib *state) { | |
int t = state->a + state->b; | |
state->a = state->b; | |
state->b = t; | |
return state->a; | |
} | |
co_fib_t co_fib_new() { | |
co_fib_t f = (co_fib_t)malloc(sizeof(struct co_fib)); | |
f->a = 0; | |
f->b = 1; | |
f->next = co_fib_next; | |
} | |
void co_fib_del(co_fib_t f) { | |
free(f); | |
} | |
int main() { | |
co_fib_t f = co_fib_new(); | |
int k; | |
for (k = 0; k < 30; k++) { | |
printf("%d\n", NEXT(f)); | |
} | |
co_fib_del(f); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment