Last active
May 23, 2016 16:58
-
-
Save DavidNix/bcab9981f4e5e88e4ea6d53503712e54 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
typedef id (^FnMapHandler)(id element, NSInteger index); | |
@interface NSArray (Functional) | |
- (NSArray *)fn_mapAndCompact:(FnMapHandler)mapHandler; | |
@end | |
@implementation NSArray (Functional) | |
- (NSArray *)fn_mapAndCompact:(FnMapHandler)mapHandler { | |
NSAssert(mapHandler != nil, @"Must pass hander to map method"); | |
NSMutableArray *mutableCollection = [NSMutableArray array]; | |
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
id result = mapHandler(obj, idx); | |
if (result != nil) { | |
[mutableCollection addObject:result]; | |
} | |
}]; | |
return [mutableCollection copy]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment