Skip to content

Instantly share code, notes, and snippets.

@cwchentw
Created October 19, 2020 03:31
Show Gist options
  • Save cwchentw/f4c48ad3c29cff7b88e260f54339219a to your computer and use it in GitHub Desktop.
Save cwchentw/f4c48ad3c29cff7b88e260f54339219a to your computer and use it in GitHub Desktop.
Fibonacci Number in Clang Block
/* 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;
}
@cwchentw
Copy link
Author

You need both clang and blocksruntime on non-Apple platforms.

Compile blocksruntime into a static library; later, compile the program:

$ clang -o fibonacci fibonacci.c path/to/libBlocksRuntime.a -I path/to/BlocksRuntime -fblocks

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