Skip to content

Instantly share code, notes, and snippets.

Avatar

Justin Spahr-Summers jspahrsummers

View GitHub Profile
View tuples.hs
data Tuple a b c d =
OneTuple a |
TwoTuple a b |
ThreeTuple a b c |
FourTuple a b c d
tuple1 :: Tuple a b c d -> Maybe a
tuple1 (OneTuple a) = Just a
tuple1 (TwoTuple a b) = Just a
tuple1 (ThreeTuple a b c) = Just a
View list.hs
data List a =
Nil |
Cons a (List a)
listLength :: Num b => List a -> b
listLength Nil = 0
listLength (Cons x xs) = 1 + listLength xs
listHead :: List a -> a
listHead (Cons x xs) = x
@jspahrsummers
jspahrsummers / gist:947835
Created Apr 29, 2011
Clang ivar write -O0
View gist:947835
.section __TEXT,__text,regular,pure_instructions
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
.section __TEXT,__const_coal,coalesced
.section __TEXT,__picsymbolstub4,symbol_stubs,none,16
.section __TEXT,__StaticInit,regular,pure_instructions
.syntax unified
.section __TEXT,__text,regular,pure_instructions
.align 2
.code 16
@jspahrsummers
jspahrsummers / gist:947836
Created Apr 29, 2011
Clang ivar write -Os
View gist:947836
.section __TEXT,__text,regular,pure_instructions
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
.section __TEXT,__const_coal,coalesced
.section __TEXT,__picsymbolstub4,symbol_stubs,none,16
.section __TEXT,__StaticInit,regular,pure_instructions
.syntax unified
.section __TEXT,__text,regular,pure_instructions
.align 2
.code 16
.thumb_func "-[IvarWriteAppDelegate application:didFinishLaunchingWithOptions:]"
@jspahrsummers
jspahrsummers / gist:947838
Created Apr 29, 2011
Clang ivar write source code
View gist:947838
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
asm("#before");
_window = nil;
asm("#after");
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
@jspahrsummers
jspahrsummers / scope.m
Created May 5, 2011
Following this behavior, how would you want to write it naturally?
View scope.m
- (NSString *)myMethod {
NSString *str = nil;
scope {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
scope(exit)
[pool drain];
str = [[NSMutableString alloc] initWithFormat:@"%i", 42];
@jspahrsummers
jspahrsummers / releaseOnExit.m
Created May 9, 2011
I don't think I'll actually do this, but it was fun to come up with.
View releaseOnExit.m
// syntactic sugar for @onExit
#define releaseOnExit \
ext_saveSelf]; @onExit { [ext_releaseObj release]; }; [(id)nil self
NSMutableString *str = [[@"foo" mutableCopy] releaseOnExit];
@jspahrsummers
jspahrsummers / gist:986175
Created May 23, 2011
Tentative coroutine syntax
View gist:986175
#define coroutine(ARGS) \
__block unsigned long ext_coroutine_line_ = 0; \
\
return ^(ARGS){ \
for (;; ext_coroutine_line_ = 0) \
switch (ext_coroutine_line_) \
default:
#define return_coroutine \
}
@jspahrsummers
jspahrsummers / gist:1005645
Created Jun 3, 2011
Concurrent map for NSArray
View gist:1005645
- (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;
}
View tuples.m
typedef id (^Tuple)(size_t);
#define tuple(...) \
tupleMake(__VA_ARGS__, nil)
#define emptyTuple \
tupleMake(nil)
Tuple tupleMake (id firstObject, ...) NS_REQUIRES_NIL_TERMINATION
{