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
/* | |
An adaptable functor (generic programming) using Blocks. An adaptable functor takes a function (or block) argument of any return type and number of arguments, and can be passed as an argument to other predicate/functors without modifying their return types and arguments (similar to the Adapter object-oriented programming design pattern). | |
*/ | |
int (^int_blk)(int) = ^ (int i) { | |
printf("i == %d\n", i); | |
return (int)i; | |
}; | |
const void * (^const __strong int_blk_ptr)(const int (^const __strong)(int)) = ^ (const int(^const __strong int_blk_ptr_t)(int)) { // Initializes block pointer blk_ptr using block parameter blk_ptr_t |
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
// A random block, that takes a float and returns a float (any other variation of argument and return types will work) | |
float float_val = 3.14; | |
const float (^ const __strong radius_blk)(float) = ^ float (float i) { | |
return i; | |
}; | |
// A block that returns a pointer to any kind of block, which is an argument of a typeof(^{}) | |
const void * (^ const __strong const_void_blk_ptr)(typeof(^{})) = ^ (typeof(^{}) block) { | |
return Block_copy((const void *)CFBridgingRetain(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
// A block that saves both your parameter value and the block that processes it, which ... | |
// ... takes an unsigned long and returns a block that returns an unsigned long | |
const unsigned long (^ const __strong predicate_blk_x)(void) = ^ (unsigned long predicate) { | |
return ^ const unsigned long (void) { | |
return predicate; | |
}; | |
}(touch_property); // touch_property is an unsigned int declared and initialized within the scope of this block | |
// A block that return a const void * to the block above, which... | |
// allows both the block and its parameter (touch_property) to be passed together using a single pointer |
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
/* | |
Functors invoked with an argument supplied by the destination | |
*/ | |
typedef const float (^ const __strong blk)(const float); | |
blk b = ^ const float (const float f) { | |
return f; | |
}; | |
typedef const void * (^ const __strong blk_t)(blk); | |
blk_t b_ptr = ^ (blk b) { |
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
- (int)asdf { | |
return ^ (int y) { // waits to return int as required by the asdf method.... | |
int result = y * 2; // ...until all other block literals and methods return | |
printf("y == %d\n", y); // printf statements are inserted to indicate when a block or method returns (this returns 3rd) | |
return result; | |
}(^ int (int x) { | |
printf("x == %d\n", x); // returns 1st | |
[self recursive_block]; // returns 2nd | |
return x; | |
}(1)); |
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
- (void)recursive_block { | |
unsigned long (^recursive_block)(int); | |
unsigned long (^ const (* restrict recursive_block_t))(int) = &recursive_block; | |
__block unsigned long iterations = 30; | |
iterations = ~(1 << (iterations + 1)); | |
__block unsigned long iteration; | |
recursive_block = ^ (void(^print_block)(int, int)) { | |
return ^ unsigned long (int count) { |
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
const float (^(^blk)(float))(void) = ^ (float f) { | |
return ^ (float f) { | |
return ^ float { | |
return f; | |
}; | |
}(f); | |
}; | |
const float(^blk_param)(void) = blk(1.0); | |
printf("blk_param() == %f\n", blk_param()); | |
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
// Returns a generic pointer to an object of any type | |
const void * (^object_t)(const unsigned long) = ^ const void * (const unsigned long index) { | |
UIButton * button; | |
[button = [UIButton new] setTag:index]; | |
printf("\nbutton == %p\n\n", &button); | |
return (const id *)CFBridgingRetain(button); | |
}; | |
// Outer block retains a generic pointer reference to an object of any type for subsequent calls to inner block | |
// Inner block returns the object referenced by the generic pointer retained by outer 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
static double (^(^(^random_generator)(double(^(*))(double)))(double(^(*))(double)))(void) = ^ (double(^(*distributor))(double)) { | |
srand48((unsigned int)time(0)); | |
return ^ (double(^(*number))(double)) { | |
static double random; | |
return ^ double { | |
return (*number)((*distributor)((random = drand48()))); | |
}; | |
}; | |
}; |
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
#import "ViewController.h" | |
@import AVFoundation; | |
@interface ViewController () { | |
AVAudioEngine * engine; | |
AVAudioSourceNode * whiteNoiseGenerator; | |
} | |
@end |