Created
October 19, 2020 03:31
-
-
Save cwchentw/f4c48ad3c29cff7b88e260f54339219a to your computer and use it in GitHub Desktop.
Fibonacci Number in Clang Block
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
/* copyright (c) 2020 Michael chen. | |
Licensed under Apache 2.0 */ | |
#include <stdio.h> | |
#include <Block.h> | |
typedef unsigned (^uint_block)(void); | |
typedef uint_block (^uint_closure)(void); | |
int main(void) | |
{ | |
uint_closure fib_generator = ^(void) { | |
__block unsigned a = 0; | |
__block unsigned b = 1; | |
return Block_copy(^(void) { | |
unsigned out = a; | |
a = b; | |
b = a + out; | |
return out; | |
}); | |
}; | |
uint_block fib = fib_generator(); | |
{ | |
size_t i; | |
for (i = 0; i < 12; ++i) | |
printf("%u\n", fib()); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need both clang and blocksruntime on non-Apple platforms.
Compile blocksruntime into a static library; later, compile the program: