Skip to content

Instantly share code, notes, and snippets.

View jspahrsummers's full-sized avatar

Justin Spahr-Summers jspahrsummers

View GitHub Profile
@jspahrsummers
jspahrsummers / gist:3089293
Created July 11, 2012 09:33
What programming language or environment features are important to you?

Many Cocoa programmers, myself included, want a more modern programming language than Objective-C, and a more modern programming environment than Cocoa. There's been some talk about what would better fit the bill, and engineers are ready to build that solution, but the question first needs to be answered – what are the most important problems to solve?

I've created the following unordered list of hypothetical goals for improvement to Cocoa/Objective-C, and would like to see how important each one is to the community. If you're interested in sharing your opinion, please fork this gist and order the below list. Feel free to add anything that you think might be missing.

Thanks!

Potential Goals

  • Functional programming, where function application is the main way to invoke code. This does not refer to using objects in a functional style.
  • Referential transparency, also sometimes known as purity or immutability.
@jspahrsummers
jspahrsummers / gist:3114225
Created July 15, 2012 01:15
Hypothetical messaging primitive for Objective-Haskell
mutableStringWithString :: Id -> IO Id
mutableStringWithString str =
getClass "NSMutableString" <.> stringWithString str
arrayWithObjects :: [Id] -> IO Id
arrayWithObjects objs = do
getClass "NSArray" <.> arrayWithObjectsAndCount objs (length objs)
@jspahrsummers
jspahrsummers / gcd-deadlocks.m
Created September 19, 2012 19:19
Contrived example of two GCD queue-synchronized objects deadlocking
@implementation OuterClass
- (id)init {
self = [super init];
if (self == nil) return nil;
_outerQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
self.innerObject = [[InnerClass alloc] init];
self.innerObject.delegate = self;
@jspahrsummers
jspahrsummers / gist:3780283
Created September 25, 2012 13:26
Figuring out the object that a non-string, compiler-verified key path refers to
// doesn't work with __block variables
#define keypath2(PATH) \
do { \
const char *_path = strchr(# PATH, '.') + 1; \
\
id _capturingBlock = ^{ \
return PATH; \
}; \
\
struct _literal { \
@jspahrsummers
jspahrsummers / gist:3897415
Created October 16, 2012 05:52
Example usage of a CGRectDivide replacement macro
describe(@"CGRectDivide macro", ^{
CGRect rect = CGRectMake(10, 20, 30, 40);
it(@"should accept NULLs", ^{
CGRectDivide(rect, NULL, NULL, 10, CGRectMinXEdge);
});
it(@"should accept pointers", ^{
CGRect slice, remainder;
CGRectDivide(rect, &slice, &remainder, 10, CGRectMinXEdge);
@jspahrsummers
jspahrsummers / gist:3992377
Created November 1, 2012 07:58
Autoreleasing a CFType
// Assumes 'value' is retained.
CFTypeRef value = NULL;
__autoreleasing obj __attribute__((objc_precise_lifetime)) = CFBridgingRelease(value);
return (__bridge CFTypeRef)obj;
@jspahrsummers
jspahrsummers / gist:4085473
Created November 16, 2012 08:29
GitHub for Mac keychain errors
Error reading generic password: The user name or passphrase you entered is not correct.
Error reading internet password: The specified attribute does not exist.
Error reading generic password: An invalid record was encountered.
Error reading internet password: An invalid record was encountered.
@jspahrsummers
jspahrsummers / RectDivisionExample.m
Created December 12, 2012 04:23
An example of clean CGRectDivide-based layout.
- (void)resizeSubviewsWithOldSize:(CGSize)oldSize {
[super resizeSubviewsWithOldSize:oldSize];
CGRect availableRect = CGRectInset(self.bounds, GHGitSetupViewHorizontalMargin, GHGitSetupViewVerticalMargin);
CGFloat labelWidth = fmax(CGRectGetWidth(self.nameLabel.bounds), CGRectGetWidth(self.emailLabel.bounds));
CGRect nameRect = CGRectZero;
CGRectDivideWithPadding(availableRect, &nameRect, &availableRect, CGRectGetHeight(self.nameTextField.bounds), GHGitSetupViewControlVerticalPadding, CGRectMaxYEdge);
CGRectDivideWithPadding(nameRect, self.nameLabel.frame, self.nameTextField.frame, labelWidth, GHGitSetupViewControlHorizontalPadding, CGRectMinXEdge);
@jspahrsummers
jspahrsummers / gist:4305792
Last active April 29, 2021 01:55
CGRectDivideWithPadding illustrations

Simple visual illustrations of how CGRectDivideWithPadding works.

Slicing from CGRectMinXEdge
  +-------+---------+------------>
e |       |         |
d | slice | padding | remainder…
g |       |         |
e |       |         |
@jspahrsummers
jspahrsummers / macroview.sh
Last active December 10, 2015 07:38
This simple script will preprocess one or more source files, and try to indent them nicely such that expanded macros are made readable. This is useful for debugging the logic of rampant macros.
#!/bin/sh
#
# Example usage:
# ./macroview.sh -IThirdPartyHeadersToInclude file.m
clang -E -nostdinc -nobuiltininc "$@" 2>/dev/null | indent -bl -nce -nei