Created
February 1, 2010 04:05
-
-
Save jonsterling/291445 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ([delegate respondsToSelector:@selector(fooController:didBar:baz:qux:)]) { | |
[delegate fooController:self | |
didBar:@"barbarian" | |
baz:@"buzzbuzz" | |
qux:@"wtfreally?"]; | |
} | |
// vs. | |
[[delegate ifResponds] fooController:self | |
didBar:@"barbarian" | |
baz:@"buzzbuzz" | |
qux:@"wtfreally?"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id sliced = [self slice:NSMakeRange(0, [words count] - 1)]; | |
id mapped = [[sliced map] stringByAppendingString:@" "]; | |
id arr = [mapped arrayByAddingObject:[[words lastObject] | |
stringByAppendingString:@"."]]; | |
id sentence = [arr inject:^id(id memo, id obj) { | |
return [memo stringByAppendingString:obj]; | |
}]; | |
NSLog(@"sentence: %@", sentence); | |
// => "sentence: Dogs are pretty freaking awesome." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSArray *words = [NSArray arrayWithObjects: | |
@"Dogs",@"are", | |
@"pretty",@"freaking", | |
@"awesome",nil]; | |
NSMutableString *sentence = [[NSMutableString alloc] init]; | |
for (NSInteger i = 0; i < [words count]; i++) { | |
NSString *rawWord = [words objectAtIndex:i]; | |
NSString *word = (i < ([words count] - 1)) | |
? [rawWord stringByAppendingString:@" "] | |
: [rawWord stringByAppendingString:@"!"]; | |
[sentence appendString:word]; | |
} | |
NSLog(@"sentence: %@", sentence); | |
// => "sentence: Dogs are pretty freaking awesome." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id str = @"dog"; | |
BOOL go = YES; | |
[[str ifTrue:go] doSomethingStringlike]; | |
// or | |
[str ifTrue:go do:^(id obj) { | |
[obj doSomethingStringlike]; | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation JSRespondsProxy | |
- forwardingTargetForSelector:(SEL)aSelector { | |
return [[self target] ifTrue: | |
[[self target] respondsToSelector:aSelector]]; | |
} | |
- (void)forwardInvocation:(NSInvocation *)invocation { | |
[[invocation ifTrue: | |
[[self target] respondsToSelector:[invocation selector]]] | |
invokeWithTarget:[self target]]; | |
} | |
- (BOOL)respondsToSelector:(SEL)aSelector { | |
return YES; | |
} | |
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { | |
NSMethodSignature *signature = [[[self target] ifTrue: | |
[[self target] respondsToSelector:aSelector]] | |
methodSignatureForSelector:aSelector]; | |
if (signature == nil){ | |
SEL dummySelector = @selector(class); | |
signature = [NSObject methodSignatureForSelector:dummySelector]; | |
} | |
return signature; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation JSTargetedProxy | |
@synthesize target; | |
- initWithTarget:aTarget { | |
if (self != nil) { | |
[self setTarget:aTarget]; | |
} return self; | |
} | |
+ proxyWithTarget:aTarget { | |
return [[self alloc] initWithTarget:aTarget]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSArray (Functional) | |
- (void)each:(void (^)(id obj))aBlock; | |
- (void)eachInvocation:(NSInvocation *)invocation; | |
- each; | |
- map:(id (^)(id obj))aBlock; | |
- mapInvocation:(NSInvocation *)invocation; | |
- map; | |
- inject:(id (^)(id memo, id obj))aBlock; | |
- slice:(NSRange)aRange; | |
- join; | |
- join:sep; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- inject:(id (^)(id memo, id obj))aBlock { | |
__block id accumulator = [self objectAtIndex:0]; | |
[[self slice:NSMakeRange(1, [self count] - 1)] each:^(id obj) { | |
accumulator = aBlock(accumulator,obj); | |
}]; | |
return accumulator; | |
} | |
- slice:(NSRange)aRange { | |
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:aRange]; | |
return [self objectsAtIndexes:indexes]; | |
} | |
- join { | |
return [self join:@""]; | |
} | |
- join:sep { | |
return [self componentsJoinedByString:sep]; | |
/** | |
This is how it would be done if Cocoa didn't have this built-in: | |
**************************************************************** | |
id sliced = [self slice:NSMakeRange(0, [self count] - 1)]; | |
id mapped = [[sliced map] stringByAppendingString:sep]; | |
id arr = [mapped arrayByAddingObject:[self lastObject]]; | |
return [arr inject:^id(id memo, id obj) { return [memo stringByAppendingString:obj]; }]; | |
**/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation NSObject (Conditionals) | |
#pragma mark - | |
#pragma mark JSRespondsProxy | |
- ifResponds { | |
return [JSRespondsProxy proxyWithTarget:self]; | |
} | |
- ifTrue:(BOOL)condition { | |
return condition ? self : nil; | |
} | |
- ifFalse:(BOOL)condition { | |
return !condition ? self : nil; | |
} | |
- (void)ifTrue:(BOOL)condition do:(void (^)(id obj))aBlock { | |
if (condition) aBlock(self); | |
} | |
- (void)ifFalse:(BOOL)condition do:(void (^)(id obj))aBlock { | |
return [self ifTrue:!condition do:aBlock]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str := 'dog'. | |
go := true. | |
go ifTrue:[ | |
str doSomethingStringlike. | |
]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment