Skip to content

Instantly share code, notes, and snippets.

@DavidNix
Last active May 23, 2016 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidNix/bcab9981f4e5e88e4ea6d53503712e54 to your computer and use it in GitHub Desktop.
Save DavidNix/bcab9981f4e5e88e4ea6d53503712e54 to your computer and use it in GitHub Desktop.
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