Skip to content

Instantly share code, notes, and snippets.

@LucasVidal
Created May 19, 2014 20:15
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 LucasVidal/9a6520c120031789b9b2 to your computer and use it in GitHub Desktop.
Save LucasVidal/9a6520c120031789b9b2 to your computer and use it in GitHub Desktop.
//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