Skip to content

Instantly share code, notes, and snippets.

View numist's full-sized avatar

Scott Perry numist

View GitHub Profile
@numist
numist / scheduler.c
Last active August 29, 2015 14:17
Simple work scheduler and test harness for microcontroller projects in C.
#include "scheduler.h"
#include <stddef.h>
#include <stdbool.h>
//
// Compatibility macros
//
// Missing from Arduino toolchain
#ifndef UINT16_MAX
@numist
numist / there_i_fixed_it.rb
Created November 13, 2014 17:48
Get the system time zone in Olson format using Ruby
def get_local_timezone_str
# Yes, this is actually a shell script…
olsontz = `if [ -f /etc/timezone ]; then
cat /etc/timezone
elif [ -h /etc/localtime ]; then
readlink /etc/localtime | sed "s/\\/usr\\/share\\/zoneinfo\\///"
else
checksum=\`md5sum /etc/localtime | cut -d' ' -f1\`
find /usr/share/zoneinfo/ -type f -exec md5sum {} \\; | grep "^$checksum" | sed "s/.*\\/usr\\/share\\/zoneinfo\\///" | head -n 1
fi`.chomp
@numist
numist / Exponentiation.swift
Last active April 5, 2024 19:28
Python-style exponentiation with the ** operator.
infix operator **: MultiplicationPrecedence
public func **<T: UnsignedInteger>(base: T, exponent: T) -> T {
return pow(base, exponent)
}
/// Implements pow() for integers using exponentiation by squaring
public func pow<T: BinaryInteger, U: UnsignedInteger>(_ base: T, _ exponent: U) -> T {
var result: T = 1
@numist
numist / Set.swift
Last active August 29, 2015 14:02
A quick implementation of Sets along with some operators for performing some set arithmetic.
import Foundation
struct SetGenerator<T:Hashable> : Generator {
var backingGenerator: DictionaryGenerator<T, Void>
init(backingGenerator: DictionaryGenerator<T, Void>) {
self.backingGenerator = backingGenerator;
}
@numist
numist / swift-repl.fish
Created June 9, 2014 15:55
A function for your fish configuration to run the Swift REPL without making Xcode6-beta your default toolchain.
function swift
set developer_dir $DEVELOPER_DIR
set -x DEVELOPER_DIR /Applications/Xcode6-Beta.app/Contents/Developer/
xcrun swift
if echo $developer_dir | grep "/"
set -x DEVELOPER_DIR $developer_dir
else
set -e DEVELOPER_DIR
end
end
@numist
numist / UIApplication+DelegateMultiDispatch.h
Created April 5, 2014 07:26
Simple category to UIApplication allowing objects to subscribe to application events via a UIApplicationDelegate-like protocol instead of notifications.
#import <UIKit/UIKit.h>
@protocol NNApplicationSubscriber <NSObject>
@optional
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;
- (void)applicationDidEnterBackground:(UIApplication *)application;
- (void)applicationDidFinishLaunching:(UIApplication *)application;
@numist
numist / gist:9218846
Created February 25, 2014 22:03 — forked from kyleve/gist:8213806
/**
Provides the ability to verify key paths at compile time.
If "keyPath" does not exist, a compile-time error will be generated.
Example:
// Verifies "isFinished" exists on "operation".
NSString *key = SQKeyPath(operation, isFinished);
// Verifies "isFinished" exists on self.
@numist
numist / example.m
Created January 3, 2014 06:59
Result memoization for object values that are both immutable and expensive to compute.
#import "memoize.h"
@implementation Example
- (id)null;
{
return NNMemoize(^{
sleep(1);
return [NSNull null];
});
@numist
numist / Poser.m
Created October 17, 2013 17:43 — forked from erisdev/Poser.m
#import <objc/runtime.h>
#import <stdlib.h>
// declare some of the Objective-C runtime's private parts where we can see them
typedef struct _NXMapTable NXMapTable;
extern NXMapTable *gdb_objc_realized_classes;
extern void *NXMapInsert(NXMapTable *table, const void *key, const void *value);
@implementation NSObject (Poser)
@numist
numist / gist:6929738
Last active December 25, 2015 06:09
No clang diagnostic pragmas, the only trick is stuffing NSAutoreleasePool into a local to avoid the deprecation-under-ARC warning.
// Built for ARC, remove the __bridge and it should work in MRC as well.
void *NNCFAutorelease(CFTypeRef cfObject)
{
if (cfObject) {
static Class arp = Nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
arp = NSClassFromString(@"NSAutoreleasePool");
Assert(arp);
});