Skip to content

Instantly share code, notes, and snippets.

View theoknock's full-sized avatar

James Alan Bush theoknock

View GitHub Profile
@theoknock
theoknock / AggregateOperations.h
Last active September 22, 2022 23:43
Aggregate operations, functional programming style with Blocks
//
// AggregateOperations.h
// AggregateOperationsPlayground
//
// Created by Xcode Developer on 9/22/22.
//
#ifndef AggregateOperations_h
#define AggregateOperations_h
@theoknock
theoknock / generic_id_2.m
Last active September 20, 2022 23:46
Generic function using id type (2nd edition)
typedef const typeof(id(^)(void)) retained_object;
static id (^retainable_object)(id(^)(void)) = ^ id (id(^object)(void)) {
return ^{
return object();
};
};
typeof (retained_object) *(^(^retain_object)(id (^__strong)(void)))(void) = ^ (id(^retainable_object)(void)) {
typeof(retained_object) * object_address;
@theoknock
theoknock / speed_test.m
Last active September 19, 2022 15:10
Test the efficiency of recursive functions and loop constructs and conditionals
#import "Clocking.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#define CONDITION(x) ((clock_t)nil ^ x)
#define DECREMENT(x) (x = ~-x)
#define ADJUST_TIME(x) x / CLOCKS_PER_SEC
@theoknock
theoknock / flexible_pointer_array_member.h
Created September 11, 2022 17:02
Flexible *pointer* array members
typedef typeof(unsigned long (^ _Nonnull __strong (* _Nonnull))(unsigned long)) predicate_function_ptr;
struct predicate_functions_arr_struct
{
unsigned long len;
predicate_function_ptr functions[];
};
static void(^test_flexible_array_member)(void) = ^{
struct predicate_functions_arr_struct * p_functions = malloc(sizeof(struct predicate_functions_arr_struct) + (10 * sizeof(predicate_function_ptr)));
p_functions->len = 10;
//
// PredicateFunctions.h
// PredicateFunctions
//
// Created by Xcode Developer on 9/10/22.
//
#ifndef PredicateFunctions_h
#define PredicateFunctions_h
#import "SimpleBlockComposition.h"
typedef typeof(unsigned long(^)(unsigned long)) block;
typedef typeof(block *) block_t;
typedef typeof(block(^)(block)) block_object;
static block (^compose)(block, block) = ^ (block composition, block component) {
block temp_comp = composition;
block new_comp = ^ (unsigned long c) { printf("composing block chain...\n"); return component(temp_comp(c)); };
return new_comp;
@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);