Skip to content

Instantly share code, notes, and snippets.

View theoknock's full-sized avatar

James Alan Bush theoknock

View GitHub Profile
@theoknock
theoknock / combine_streams.h
Last active October 8, 2022 02:49
Stream composition
static void(^stream_composition_demo)(void) = ^{
typedef typeof(unsigned long) predicate;
typedef typeof(predicate(^)(predicate)) predicate_function;
typedef typeof(predicate(^(*))(predicate)) predicate_function_t;
typeof(predicate_function) stream_op;
typeof(predicate_function_t) stream_op_t = &stream_op;
typeof(predicate_function) predicate_op;
typeof(predicate_function_t) predicate_op_t = &predicate_op;
@theoknock
theoknock / bitwise_pointer_swapping.h
Last active October 7, 2022 20:50
Bitwise pointer swapping. Useful for, say, a producer-consumer mutually recursive trampoline
static void(^swap_pointers)(void) =
^{
typedef typeof(unsigned long(^)(unsigned long)) predicate_function;
typedef typeof(unsigned long(^*)(unsigned long)) predicate_function_t;
predicate_function func_a, func_b;
__block void * current = (predicate_function_t)&func_a;
__block void * swap = (predicate_function_t)&func_b;
func_a = ^ unsigned long (unsigned long predicate) {
@theoknock
theoknock / recursive_function.h
Last active October 6, 2022 01:05
Starts with the clock() time, counts down to zero, and then returns the difference between the end time and the start time. Compare the results between variations of the same code within the recursive function to determine the most efficient implementation.
clock_t recursive_function(clock_t count) {
// Code to measure goes here (lower numbers are better)
return (clock_t)(((((clock_t)nil ^ count) && recursive_function(~-count))));
}
static void (^speed_test)(void) = ^{
printf("recursive function == %lu\n", -clock() & (recursive_function(clock()) ^ clock()));
};
@theoknock
theoknock / logger.h
Created September 28, 2022 20:57
UITextView logging
// Adds log entries to a UITextView
typedef typeof(NSString *) LogEntry;
typedef typeof(NSMutableAttributedString *) LogEntryComposition;
typedef const typeof(LogEntryComposition * restrict) LogEntryCompositionPtr;
typedef typeof(void(^)(LogEntry)) LogEngine;
static void (^(^log_engine)(UITextView *))(LogEntry) = ^ (UITextView * text_view) {
__block LogEntryComposition entry_composition = [[NSMutableAttributedString alloc] init];
return ^ (LogEntry entry) {
dispatch_async(dispatch_get_main_queue(), ^{
NSAttributedString * attributed_entry = [[NSAttributedString alloc] initWithString:entry attributes:^ NSDictionary * (void) {
@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;