Skip to content

Instantly share code, notes, and snippets.

View jspahrsummers's full-sized avatar

Justin Spahr-Summers jspahrsummers

View GitHub Profile
@jspahrsummers
jspahrsummers / gist:1005645
Created June 3, 2011 00:44
Concurrent map for NSArray
- (NSArray *)mapWithOptions:(NSEnumerationOptions)options usingBlock:(id (^)(id obj))block {
NSUInteger count = [self count];
NSArray *result = nil;
if (options & NSEnumerationConcurrent) {
volatile id * restrict objects = malloc(sizeof(id) * count);
if (!objects) {
return nil;
}
typedef id (^Tuple)(size_t);
#define tuple(...) \
tupleMake(__VA_ARGS__, nil)
#define emptyTuple \
tupleMake(nil)
Tuple tupleMake (id firstObject, ...) NS_REQUIRES_NIL_TERMINATION
{
local ship = { entropy.loadtexture("data/ships/fighter/base.png") }
local glow = { entropy.loadtexture("data/ships/fighter/glow.png") }
local lights = { entropy.loadtexture("data/ships/fighter/lights.png") }
function shutdown ()
entropy.deletetexture(ship[1])
entropy.deletetexture(glow[1])
entropy.deletetexture(lights[1])
end
@jspahrsummers
jspahrsummers / gist:1334548
Created November 2, 2011 19:03
Returning multiple values using blocks
// only works with object types, but such is Cocoa
typedef id (^generator)(void);
- (generator)methodReturningMultipleValues {
__block int value = 0;
return ^{
switch (value++) {
case 0:
return [NSNumber numberWithInt:5];
@jspahrsummers
jspahrsummers / aspects.m
Created November 10, 2011 05:49
Sketch of aspect-oriented programming in Objective-C
/* Logger.m */
@aspect Logger
// called for any selector
- (void)before {
// '_cmd' is special -- the selector for the actual method with the advice
// applied to it
NSLog(@"starting -[%@ %@]", self, _cmd);
}
// only called for 'deposit:'
@jspahrsummers
jspahrsummers / gist:2268658
Last active October 2, 2015 15:28
AppKit classes not supporting weak references (on 10.7)
# In a class-dump of AppKit:
$ ack -l '\)(retain|release);' . | awk -F '.' '{ print $1 }'
__NSATSStringSegment
__NSFontTypefaceInfo
__NSSharedFontInstanceInfo
_NSCachedAttributedString
_NSStateMarker
NSATSGlyphStorage
NSATSTypesetter
@jspahrsummers
jspahrsummers / guessing-game.ll
Created April 16, 2012 18:44
Guessing game written in LLVM assembly
; run this with one of the following:
;
; lli guessing-game.ll
; llc guessing-game.ll && clang -o guessing-game guessing-game.s && ./guessing-game
;
; note: you may need to 'brew install llvm' first
; type representing a FILE handle in C (like stdin)
%FILE = type opaque
@jspahrsummers
jspahrsummers / VELAnimatedBinding.h
Created April 20, 2012 23:16
An example of creating animated bindings, using PROBinding and Velvet animations
//
// VELAnimatedBinding.h
// Velvet
//
// Created by Justin Spahr-Summers on 01.04.12.
// Copyright (c) 2012 Bitswift. All rights reserved.
//
#import <Proton/Proton.h>
#1 "Tests/EXTADTTest.m"
#1 "Tests/EXTADTTest.m" 1
#1 "<built-in>" 1
#1 "<built-in>" 3
#152 "<built-in>" 3
#1 "<command line>" 1
#1 "<built-in>" 2
#1 "Tests/EXTADTTest.m" 2