Skip to content

Instantly share code, notes, and snippets.

@bjhomer
bjhomer / 1 badDiff.diff
Last active August 29, 2015 14:05
Semantically poor lines shouldn't match
context
context
-AAAA
-AAAA
-AAAA
+BBBB
+BBBB
+BBBB
+BBBB
{
@bjhomer
bjhomer / gist:cc3f3155d3b109f61601
Created October 31, 2014 17:06
LLDB and Swift Arrays are not friends.
// What I would expect:
(lldb) p newPaths
([AnyObject]) $R1 = 2 values {
[0] = "This is a string",
[1] = "This is another string"
}
@bjhomer
bjhomer / odd.swift
Last active August 29, 2015 14:10
Swift shorthand argument syntax oddities
// The Swift Programming Language book from Apple states the following:
//
// Swift automatically provides shorthand argument names to inline closures,
// which can be used to refer to the values of the closure’s arguments by the
// names $0, $1, $2, and so on.
// In Xcode 6.1 and 6.2b1, it appears that this is only true IF the last argument
// is referenced using shorthand syntax somewhere in the closure body. See below
// for details.
protocol FooProtocol {
func blah()
}
struct Foo : FooProtocol {
func blah() {}
}
@bjhomer
bjhomer / playground.swift
Last active August 29, 2015 14:18
Disambiguating methods with external parameter names
import Cocoa
func curry<A, B, C> ( f: (A, B) -> C ) -> (A -> B -> C) {
return { x in
return { y in
return f(x, y)
}
}
}
@bjhomer
bjhomer / files.swift
Last active August 29, 2015 14:20
Magical initializer delegation
class Top {
let x: Int
init() { x = 3; println("Top.init") }
}
class Bottom: Top {
var z: String
// This doesn't call `super.init`, but Top.init still runs. Why?
init(dummy: Bool) { z = "hi" }
@bjhomer
bjhomer / 1-objc.h
Last active August 29, 2015 14:26
Block typedef broken-ness in Xcode 7 beta 5
#import <Foundation/Foundation.h>
// This is pretty simple, right?
typedef void(^SampleBlock)(NSString *str);
@bjhomer
bjhomer / gist:961065
Created May 8, 2011 03:17
Infinite recursion via NSLog
NSMutableArray *a = [NSMutableArray array];
[a addObject:a];
NSLog(@"a: %@", a);
@bjhomer
bjhomer / gist:1245520
Created September 27, 2011 16:19
Getting the current track in iTunes
#! /usr/local/bin/macruby
framework "ScriptingBridge"
iTunes = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes")
iTunes.currentTrack.name
@bjhomer
bjhomer / gist:1650079
Created January 20, 2012 22:57
Waiting on asynchronous results without blocking the run loop
// This is a useful bit of code for waiting for the results of an
// asynchronous operation. It's especially useful in unit tests.
// It keeps the run loop going, so it's not blocking delegate callbacks.
BOOL waitForBlock(NSTimeInterval timeout,
BOOL (^condition)(void)) {
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
BOOL val = condition();
while ( val == NO && [timeoutDate timeIntervalSinceNow] > 0 ) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate];