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_composition.m
Last active November 7, 2024 18:21
Block composition
typedef const bool (^ __strong object_block)(const bool);
object_block object_blk = ^ bool (const bool b) {
printf("object_blk state == %s\n", (b) ? "TRUE" : "FALSE");
return b;
};
object_block object_blk_2 = ^ bool (const bool b) {
printf("object_blk_2 state == %s\n", (b) ? "TRUE" : "FALSE");
return b;
};
object_block object_blk_3 = ^ bool (const bool b) {
@theoknock
theoknock / copying_blocks.m
Last active August 27, 2022 23:45
Copying blocks (unfinished)
typedef const void (^ const __strong object_block)(const bool);
object_block object_blk = ^ (const bool b) {
printf("object_blk state == %s\n", (b) ? "TRUE" : "FALSE");
};
object_block object_blk_2 = ^ (const bool b) {
printf("object_blk_2 state == %s\n", (b) ? "TRUE" : "FALSE");
};
object_block object_blk_3 = ^ (const bool b) {
printf("object_blk_3 state == %s\n", (b) ? "TRUE" : "FALSE");
};
@theoknock
theoknock / map_reduce_filter_block.m
Created August 16, 2022 22:33
Map-Reduce-Filter Block (In-Progress)
static void (^(^(^(^array_pointer_test)(const unsigned int))(CFTypeRef(^)(void)))(void(^)(CFTypeRef)))(CFTypeRef(^)(CFTypeRef)) = ^ (unsigned int object_count) {
typedef CFTypeRef objects[object_count];
typeof(objects) objects_ptr[object_count];
__block unsigned long (^recursive_block)(unsigned long);
(recursive_block = ^ unsigned long (unsigned long index) {
printf("index == %lu\n", index);
return (unsigned long)(index ^ 0UL) && (unsigned long)(recursive_block)(~-index);
})(object_count);
return ^ (CFTypeRef * objects_t) {
@theoknock
theoknock / bitwise_recursive_block.m
Last active September 6, 2022 15:25
Bitwise recursive block
__block unsigned long (^recursive_block)(unsigned long);
(recursive_block = ^ unsigned long (unsigned long index) {
printf("index == %lu\n", index);
return (unsigned long)(index ^ (unsigned long)(recursive_block)(~-index));
})(10);
@theoknock
theoknock / generic_id.m
Last active August 5, 2022 17:50
Generic function using id type
static int counter = 0;
// ...
id (^retainable_object_)(id(^)(void)) = ^ id (id(^object)(void)) {
return ^{
return object();
};
};
@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
@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 / 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 / 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 / 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) {