Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active September 22, 2022 23:43
Show Gist options
  • Save theoknock/bcac5a5cd4751170ceb4ba3d28adcf83 to your computer and use it in GitHub Desktop.
Save theoknock/bcac5a5cd4751170ceb4ba3d28adcf83 to your computer and use it in GitHub Desktop.
Aggregate operations, functional programming style with Blocks
//
// AggregateOperations.h
// AggregateOperationsPlayground
//
// Created by Xcode Developer on 9/22/22.
//
#ifndef AggregateOperations_h
#define AggregateOperations_h
static unsigned long c = 1UL;
int int_val = 2;
const int (^ const __strong int_block)(int) = ^ int (int i) {
return i;
};
const void * (^ const __strong retain_block)(const void * _Nonnull) = ^ (const void * _Nonnull cb) {
return (const void *)CFBridgingRetain((__bridge id _Nullable)(cb));
};
const void * _Nonnull (^release_block)(const void * _Nonnull) = ^ (const void * _Nonnull retained_block) {
return (__bridge const void * _Nonnull)CFBridgingRelease(retained_block);
};
void(^retain_block_test)(void) = ^{
const void * i_block = retain_block((__bridge const void * _Nonnull)(int_block));
};
void(^release_block_test)(const void *) = ^ (const void * block) {
int (^ const __strong i_block)(int) = (__bridge int (^)(int))(release_block(block));
i_block(1);
};
typedef typeof(CFTypeRef * (^(^(^)(unsigned long))(void))(unsigned long)) AggregateDataStructure;
static CFTypeRef * (^(^(^aggregate_data_structure)(unsigned long))(void))(unsigned long) = ^ (unsigned long count) {
typeof(CFTypeRef *) objects_t[count];
return ^ (CFTypeRef * objects_ptr) {
return ^{
return ^ CFTypeRef * (unsigned long index) {
return ((CFTypeRef *)objects_ptr + index);
};
};
}((objects_t[0]));
};
static void (^(^(^aggregate_operations)(unsigned long))(const void *))(void(^)(CFTypeRef *)) = ^ (unsigned long object_count) {
return ^ (const void * retained_structure) {
CFTypeRef * (^(^(^ const __strong released_structure)(unsigned long))(void))(unsigned long) = (__bridge CFTypeRef * (^(^(^)(unsigned long))(void))(unsigned long))(release_block(retained_structure));
static CFTypeRef * (^(^source)(void))(unsigned long);
static CFTypeRef * (^stream)(unsigned long);
stream = (source = released_structure(object_count))();
return ^ (void(^aggregate_operation)(CFTypeRef *)) {
for (unsigned long index = 0; index < object_count; index++) {
aggregate_operation((stream)(index));
}
};
};
};
#endif /* AggregateOperations_h */
//
// ViewController.m
// AggregateOperationsPlayground
//
// Created by Xcode Developer on 9/22/22.
//
#import "ViewController.h"
#import "AggregateOperations.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize a data structure (i.e., source and stream)
__block void (^aggregate_operation)(void(^)(CFTypeRef *)) = aggregate_operations(10)(retain_block((__bridge const void * _Nonnull)(aggregate_data_structure)));
// Add aggregate operations to the pipeline
// Aggregate
aggregate_operation(^ (CFTypeRef * number_t){
*(number_t) = CFBridgingRetain((__bridge id _Nullable)(^ CFTypeRef {
__block NSNumber * number = [[NSNumber alloc] initWithUnsignedLong:c++];
printf("\t(aggregate %lu)\n", [number unsignedLongValue]);
return (__bridge CFTypeRef)(number);
})());
});
// Filter
aggregate_operation(^ (CFTypeRef * number_t){
*(number_t) = ([(__bridge NSNumber *)*(number_t) unsignedLongValue] % 2) ? *(number_t) : nil;
printf("Filtered number == %lu\n", [(__bridge NSNumber *)*(number_t) unsignedLongValue]);
});
// Reduce
aggregate_operation(^ (CFTypeRef * number_t){
if (*(number_t)) printf("\t(iterate %lu)\n", [(__bridge NSNumber *)*(number_t) unsignedLongValue]);
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment