Skip to content

Instantly share code, notes, and snippets.

View jonsterling's full-sized avatar

Jon Sterling jonsterling

View GitHub Profile
// Author: Pierre Bernard, Jonathan Sterling
// Source: http://www.bernard-web.com/pierre/blog/index.php?id=2624434753771423706
// Caveat: Consider using http://github.com/andrep/RMModelObject instead.
@implementation NSObject (PropertyDealloc)
- (void)releaseProperties {
Class class = [self class];
unsigned int pCount;
objc_property_t *properties = class_copyPropertyList(class, &pCount);
- (void)saveManagedObjectContext {
id result = [[FKEither return:self.managedObjectContext] bind:^(id moc) {
NSError *err = nil;
[moc save:&err];
return [FKEither right:@"Success!" orLeft:err];
}];
NSLog(@"Saving managedObjectContext: %@", [result value]);
}
// hopefully => Saving managedObjectContext: Success!
/* NOTES:
Fun use of a higher-order-block and some maybe objects.
*/
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock {
Function (^blockForEncoding)(NSStringEncoding) = ^Function (NSStringEncoding enc) {
return [^(id d) { return [[[NSString alloc] initWithData:d encoding:enc] autorelease]; } copyrelease];
};
id utf8Str = [[FKMaybe return:CDATABlock] map:blockForEncoding(NSUTF8StringEncoding)];
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Channel</title>
<subtitle>A subtitle.</subtitle>
<description>This is an example feed to be used in an application for O'Reilly Media. Some more text to make this go multi-line in the header label.</description>
<link href="http://example.org/feed/" rel="self" />
<link href="http://example.org/" />
<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
@jonsterling
jonsterling / JSSettingsController.h
Created July 24, 2010 22:04
JSSettingsController
//
// JSSettingsController.h
//
// Created by Jon on 7/24/10.
// Copyright (c) 2010 Jonathan Sterling. All rights reserved.
//
@interface JSSettingsController : NSObject
@end
-- This is a really terrible parser I'm working on to learn Haskell & Parsec.
-- Sorry if it makes your eyes bleed.
module Main where
import Control.Monad(liftM)
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Expr
import qualified Text.ParserCombinators.Parsec.Token as P
import Text.ParserCombinators.Parsec.Language
@jonsterling
jonsterling / JSProtocolChecker.h
Created July 26, 2010 03:08
JSProtocolChecker = NSProtocolChecker that works on iOS. Maybe some day I'll find a use for this.
//
// JSProtocolChecker.h
//
// Created by Jon on 7/25/10.
// Copyright (c) 2010 Jonathan Sterling. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface JSProtocolChecker : NSProxy
#include <stdio.h>
int main () {
size_t n = 100;
int odds[n];
size_t i;
for(i = 0; i < n; ++i) {
odds[i] = 2 * i + 1;
}
@jonsterling
jonsterling / Objectify.h
Created August 20, 2010 06:37
Objectify
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <objc/message.h>
#import <string>
#import <map>
#import <typeinfo>
#define $(value) box<typeof(value)>(value)
using namespace std;
template <typename T> NSArray *fk::arr (T obj1, ...) NS_REQUIRES_NIL_TERMINATION;
template <typename T> NSArray *fk::lift_arr (T obj);
// we started with the following, which would be better represented as a C-array of ints:
[NSArray arrayWithObjects:
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:5],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:45],
nil];