Skip to content

Instantly share code, notes, and snippets.

View kastiglione's full-sized avatar

Dave Lee kastiglione

View GitHub Profile
void (^myBlock)(int);
__block __weak __typeof__(myBlock) myRecursiveBlock = myBlock = ^(int x) {
if (x > 5) {
return;
} else {
myRecursiveBlock(x + 1);
}
};
myBlock(1);
@kastiglione
kastiglione / gcd_map.mm
Last active January 6, 2017 20:52
Concurrent map benchmark
#import <Foundation/Foundation.h>
#import <libkern/OSAtomic.h>
@interface NSArray (ConcurrentMap)
- (NSArray*)semaphore_map:(id (^)(id))block;
- (NSArray*)serial_map:(id (^)(id))block;
- (NSArray*)spinlock_map:(id (^)(id))block;
@end
@implementation NSArray (ConcurrentMap)
@kastiglione
kastiglione / gist:5679834
Last active December 17, 2015 22:09 — forked from alloy/gist:5679340
- (NSArray *)mapConcurrent;
{
NSArray *originals = self.listOfObjects;
NSUInteger count = originals.count;
NSMutableArray *collected = [[NSMutableArray alloc] initWithCapacity:count];
for (NSUInteger i = 0; i < count; i++) {
[collected addObject:[NSNull null]];
}
#!/usr/bin/env casperjs
#
# Example: ./github-users.coffee Greenville
#
{Casper} = require 'casper'
class GitHubUserScraper extends Casper
USER_AGENT: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'
@kastiglione
kastiglione / gist:5475944
Last active December 16, 2015 18:09
Compose a signal corresponding to whether a source signal has sent an equal number of YESes and NOs.
[[[signal
// Map YES to 1, NO to -1
map:^(NSNumber* b) {
return @(b.boolValue ? 1 : -1);
}]
// Running sum of yes/1's and no/-1's
startWithScan:@0 combine:^(NSNumber* running, NSNumber* next) {
return @(running.intValue + next.intValue);
}]
// A 0-sum means equal number of YES/NO's; Map 0 to YES
@kastiglione
kastiglione / dynamic_rescue.rb
Last active December 14, 2015 12:08
Rescue on more than just exception type.
module UncaughtThrowError
def self.===(ex)
ArgumentError === ex && ex.message =~ /uncaught throw/
end
end
begin
throw :foo
rescue UncaughtThrowError
puts 'Success: Rescued UncaughtThrowError'
CGRect CGRectIntegralScaledEx(CGRect rect, CGFloat scale)
{
return CGRectMake(floorf(rect.origin.x * scale) / scale, floorf(rect.origin.y * scale) / scale, ceilf(rect.size.width * scale) / scale, ceilf(rect.size.height * scale) / scale);
}
CGRect CGRectIntegralScaled(CGRect rect)
{
return CGRectIntegralScaledEx(rect, [[UIScreen mainScreen] scale]);
}
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
a = NSEntityDescription.new
a.name = 'A'
b = NSEntityDescription.new
b.name = 'B'
bs = NSRelationshipDescription.new
bs.name = 'bs'
bs.destinationEntity = b
bs.ordered = true
@kastiglione
kastiglione / custom_store.rb
Created October 4, 2012 21:04
Setup a minimal NSIncrementalStore subclass for learning / experimenting with MacRuby
framework 'CoreData'
# Create a minimal model
$thing = NSEntityDescription.alloc.init
$thing.name = 'Thing'
$model = NSManagedObjectModel.alloc.init
$model.entities = [$thing]
# Create the custom store class
class CustomStore < NSIncrementalStore
@kastiglione
kastiglione / rubymotion-app-setup-after-hook.rb
Created July 28, 2012 22:14
RubyMotion App::setup after hook
class << Motion::Project::App
real_setup = instance_method(:setup)
define_method :setup do |&block|
real_setup.bind(self).call(&block)
# run after hook here
end
end