Skip to content

Instantly share code, notes, and snippets.

@kylesluder
kylesluder / WithoutFirst.swift
Created November 24, 2014 17:08
A version of dropFirst() that works on any SequenceType or GeneratorType
#!xcrun swift
func withoutFirst<T, G: GeneratorType where G.Element == T>(genGiver: () -> G) -> SequenceOf<T> {
return SequenceOf {
_ -> G in
var g = genGiver()
g.next()
return g
}
}
@kylesluder
kylesluder / AsyncAwait.swift
Last active May 15, 2020 06:01
A C#-style Async/Await implementation in Swift
// A much better version of C#-style Async/Await. It can return values!
import Cocoa
struct Await<T> {
// private
let group: dispatch_group_t
let getResult: () -> T
// public
func await() -> T { return getResult() }
@kylesluder
kylesluder / ObjCSuper.m
Last active August 29, 2015 13:56
An Objective-C analogue to the Python super class.
// % clang -framework Foundation -o ObjCSuper ObjCSuper.m SuperTrampoline.s
// % ./ObjCSuper
//
// 2014-02-15 23:03:32.498 ObjCSuper[1296:507] Subclass impl
// 2012014-02-15 23:03:32.498 ObjCSuper[1296:507] Subclass impl4-02-15 23:03:32.500 ObjCSuper[1296:507] b respondsToSelector:@selector(retain)? YES
// 2014-02-15 23:03:32.500 ObjCSuper[1296:507] b respondsToSelector:@selector(subclassMethod)? YES
// 2014-02-15 23:03:32.501 ObjCSuper[1296:507] Superclass impl
// 2014-02-15 23:03:32.501 ObjCSuper[1296:507] b_super respondsToSelector:@selector(retain)? YES
// 2014-02-15 23:03:32.501 ObjCSuper[1296:507] b_super respondsToSelector:@selector(subclassMethod)? NO
@kylesluder
kylesluder / cleanup.c
Created February 4, 2014 22:01
Neither of these runs the cleanup function
#include <stdio.h>
static void cleanup_func(int *foo)
{
printf("cleanup! %d\n", *foo);
}
int main(int argc, char **argv)
{
return 0;
@kylesluder
kylesluder / AutoNotifyTest.m
Last active August 29, 2015 13:55
A demonstration of an inefficiency in mutable array proxies that I think could be improved (rdar://problem/15966921)
// clang -framework Foundation -o AutoNotifyTest AutoNotifyTest.m
/***
Expected results:
willChange:valuesAtIndexes:forKey:
removeObjectFromBarAtIndex:
removeObjectFromBarAtIndex:
removeObjectFromBarAtIndex:
removeObjectFromBarAtIndex:
Analysis of sampling com.apple.security.pboxd (pid 20558) every 1 millisecond
Process: com.apple.security.pboxd [20558]
Path: /System/Library/PrivateFrameworks/RemoteViewServices.framework/XPCServices/com.apple.security.pboxd.xpc/Contents/MacOS/com.apple.security.pboxd
Load Address: 0x10c951000
Identifier: com.apple.security.pboxd
Version: 2.0 (80.7)
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Date/Time: 2013-09-26 16:43:26.188 -0700
// RUN: %clang -fsyntax-only %s 2>&1 | FileCheck %s
#import <Foundation/NSObject.h>
@protocol FooDelegate;
@interface Foo : NSObject
@property (weak) id<FooDelegate> delegate;
@end
@protocol FooDelegate <NSObject>
- (void)doSomething;
diffcount () {
awk '
BEGIN { minuscount=0; pluscount=0; }
/^-/ { minuscount = minuscount + 1; }
/^\+/ { pluscount = pluscount + 1; }
END { print "-" minuscount, "+" pluscount; }
'
}
@kylesluder
kylesluder / gist:3842250
Created October 5, 2012 20:40
Repeated Constraints
>>> -[<NSSplitView: 0x105e20130> resizeWithOldSuperviewSize:] superview from {543, 453} to {543, 493}
>>> -[<NSSplitView: 0x105e20130> setFrame:] from {{0, 0}, {543, 453}} to {{0, 0}, {0, 0}}
(gdb) po [self constraintsAffectingLayoutForOrientation:1]
<__NSArrayI 0x105bbfa00>(
<NSLayoutConstraint:0x105e19350 NSSplitView:0x105e20130.bottom == NSView:0x105e142a0.bottom>,
<NSLayoutConstraint:0x105e1c9a0 V:|-(0)-[NSSplitView:0x105e20130] (Names: '|':NSView:0x105e142a0 )>,
<NSLayoutConstraint:0x105e19350 NSSplitView:0x105e20130.bottom == NSView:0x105e142a0.bottom>
)
@kylesluder
kylesluder / gist:3835936
Created October 4, 2012 19:46
Split View Frame Sizing
On 10.7.5:
>>> -[<NSView: 0x108064e50> resizeWithOldSuperviewSize:] superview from {543, 475} to {543, 515}
>>> -[<NSView: 0x108064e50> setFrame:] from {{0, 0}, {543, 453}} to {{0, 0}, {543, 493}}
>>> -[<NSView: 0x108064e50> setFrameSize:] from {543, 453} to {543, 493}
>>> -[<NSView: 0x108064e50> resizeSubviewsWithOldSize:] from {543, 453} to {543, 493}
>>> -[<NSSplitView: 0x10806cb10> resizeWithOldSuperviewSize:] superview from {543, 453} to {543, 493}
>>> -[<NSSplitView: 0x10806cb10> setFrame:] from {{0, 0}, {543, 453}} to {{0, 0}, {0, 0}} /*** WTF??? ***/
>>> -[<NSSplitView: 0x10806cb10> setFrameSize:] from {543, 453} to {0, 0}
>>> ... and so on, causing an eventual constraint violation ...