Skip to content

Instantly share code, notes, and snippets.

View theoknock's full-sized avatar

James Alan Bush theoknock

View GitHub Profile
@theoknock
theoknock / block_pointers.m
Last active March 30, 2022 02:47
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).
/*
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
@theoknock
theoknock / block_generics.m
Last active April 5, 2022 12:57
Block generics.
// 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));
@theoknock
theoknock / block+parameter_t.m
Last active April 5, 2022 15:40
Passing both a block and its parameters via a const void * and back again
// 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
@theoknock
theoknock / poly_blocks.h
Last active April 20, 2022 20:37
Block_copy + CFBridgingRetain/Release
/*
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) {
@theoknock
theoknock / sequenced_block_literals.m
Last active April 20, 2022 20:31
Sequencing blocks and methods inside a return statement.
- (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));
@theoknock
theoknock / recursive_block-bitwise_counter.m
Last active February 3, 2023 15:30
Recursive block + Bitwise counter
- (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) {
@theoknock
theoknock / blk+param.m
Created April 28, 2022 23:40
A block and its parameter value with one pointer
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());
@theoknock
theoknock / generics.m
Last active February 3, 2023 15:29
Blocks that create and return generic references and pointers to objects of any type. Demonstrates the use of id and const void * and CFBridgingRetain and CFBridgingRelease for generic programming purposes.
// 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
@theoknock
theoknock / weight-distributed-random-calculation.c
Last active July 8, 2022 19:42
Generating weight-distributed random number calculations Blocks template
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())));
};
};
};
@theoknock
theoknock / ViewController.m
Created July 18, 2022 00:39
AVFAudio manual rendering, abbreviated. The most concise implementation of manual audio buffer rendering using AVFoundation, using only an AVAudioSourceNode and nothing else. Plays white noise for one second.
#import "ViewController.h"
@import AVFoundation;
@interface ViewController () {
AVAudioEngine * engine;
AVAudioSourceNode * whiteNoiseGenerator;
}
@end