Created
November 15, 2009 05:22
-
-
Save Sija/235033 to your computer and use it in GitHub Desktop.
Making NSArray more ruby-ish
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-Blocks.h | |
// Handy codebits | |
// | |
// Created by Sijawusz Pur Rahnama on 15/11/09. | |
// Copyleft 2009 Sijawusz Pur Rahnama. Some rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
static NSComparisonResult sortUsingBlock(id arg1, id arg2, NSComparisonResult (^block)(id, id)); | |
@interface NSArray (Blocks) | |
- (BOOL) all:(BOOL (^)(id))block; | |
- (BOOL) every:(BOOL (^)(id))block; /// @ref self::all() | |
- (BOOL) any:(BOOL (^)(id))block; | |
- (BOOL) some:(BOOL (^)(id))block; /// @ref self::any() | |
- (void) each:(void (^)(id))block; | |
- (NSArray *) sort:(NSComparisonResult (^)(id, id))block; | |
- (id) find:(BOOL (^)(id))block; | |
- (id) detect:(BOOL (^)(id))block; /// @ref self::find() | |
- (NSArray *) select:(BOOL (^)(id))block; | |
- (NSArray *) findAll:(BOOL (^)(id))block; /// @ref self::select() | |
- (NSArray *) filter:(BOOL (^)(id))block; /// @ref self::select() | |
- (NSArray *) reject:(BOOL (^)(id))block; | |
- (NSArray *) partition:(BOOL (^)(id))block; | |
- (NSArray *) map:(id (^)(id))block; | |
- (NSArray *) collect:(id (^)(id))block; /// @ref self::map() | |
@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
// | |
// NSArray-Blocks.m | |
// Handy codebits | |
// | |
// Created by Sijawusz Pur Rahnama on 15/11/09. | |
// Copyleft 2009 Sijawusz Pur Rahnama. Some rights reserved. | |
// | |
#import "NSArray-Blocks.h" | |
static NSComparisonResult sortUsingBlock(id arg1, id arg2, NSComparisonResult (^block)(id, id)) { | |
return block(arg1, arg2); | |
} | |
@implementation NSArray (Blocks) | |
- (BOOL) all:(BOOL (^)(id))block { | |
BOOL truth = YES; | |
for (id obj in self) { | |
truth = truth && block(obj); | |
} | |
return truth; | |
} | |
- (BOOL) every:(BOOL (^)(id))block { | |
return [self all:block]; | |
} | |
- (BOOL) any:(BOOL (^)(id))block { | |
BOOL truth = NO; | |
for (id obj in self) { | |
truth = truth || block(obj); | |
} | |
return truth; | |
} | |
- (BOOL) some:(BOOL (^)(id))block { | |
return [self any:block]; | |
} | |
- (void) each:(void (^)(id))block { | |
for (id obj in self) { | |
block(obj); | |
} | |
} | |
- (NSArray *) sort:(NSComparisonResult (^)(id, id))block { | |
return [self sortedArrayUsingFunction:&sortUsingBlock context:block]; | |
} | |
- (id) find:(BOOL (^)(id))block { | |
for (id obj in self) { | |
if (block(obj)) return obj; | |
} | |
return nil; | |
} | |
- (id) detect:(BOOL (^)(id))block { | |
return [self find:block]; | |
} | |
- (NSArray *) select:(BOOL (^)(id))block { | |
NSMutableArray *new = [NSMutableArray array]; | |
for (id obj in self) { | |
if (block(obj)) [new addObject:obj]; | |
} | |
return new; | |
} | |
- (NSArray *) findAll:(BOOL (^)(id))block { | |
return [self select:block]; | |
} | |
- (NSArray *) filter:(BOOL (^)(id))block { | |
return [self select:block]; | |
} | |
- (NSArray *) reject:(BOOL (^)(id))block { | |
NSMutableArray *new = [NSMutableArray array]; | |
for (id obj in self) { | |
if (!block(obj)) [new addObject:obj]; | |
} | |
return new; | |
} | |
- (NSArray *) partition:(BOOL (^)(id))block { | |
NSMutableArray *ayes = [NSMutableArray array]; | |
NSMutableArray *noes = [NSMutableArray array]; | |
for (id obj in self) { | |
if (block(obj)) { | |
[ayes addObject:obj]; | |
} else { | |
[noes addObject:obj]; | |
} | |
} | |
return [NSArray arrayWithObjects:ayes, noes, nil]; | |
} | |
- (NSArray *) map:(id (^)(id))block { | |
NSMutableArray *new = [NSMutableArray arrayWithCapacity:[self count]]; | |
for (id obj in self) { | |
id newObj = block(obj); | |
[new addObject:newObj ? newObj : [NSNull null]]; | |
} | |
return new; | |
} | |
- (NSArray *) collect:(id (^)(id))block { | |
return [self map:block]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for this!