Created
May 19, 2014 20:15
-
-
Save LucasVidal/9a6520c120031789b9b2 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
//Smalltalk's "collect", haskell's "map" | |
- (NSArray *) arrayByEvaluatingBlock:(id(^)(id object))block { | |
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[self count]]; | |
for (id object in self) { | |
[array addObject:block(object)]; | |
} | |
return [array copy]; | |
} | |
//Smalltalk's and haskell's "filter" | |
- (NSArray *)arrayByFilteringWithBlock:(BOOL(^)(id object))block { | |
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[self count]]; | |
for (id object in self) { | |
if (block(object)) { | |
[array addObject:object]; | |
} | |
} | |
return [array copy]; | |
} | |
//Smalltalk's "inject into", haskell "foldl" | |
- (id)foldingWithBlock:(id(^)(id a, id b))block initialObject:(id)initalObject { | |
for (id object in self) { | |
initalObject = block(initalObject, object); | |
} | |
return initalObject; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment