Skip to content

Instantly share code, notes, and snippets.

@rnapier
Last active August 29, 2015 13:56
Show Gist options
  • Save rnapier/8938114 to your computer and use it in GitHub Desktop.
Save rnapier/8938114 to your computer and use it in GitHub Desktop.
// With dot syntax (and standard Xcode indentation, just hit "Cmd-I")
NSArray *tweets = Underscore.array(results)
.filter(Underscore.isDictionary)
.reject(^BOOL (NSDictionary *tweet) {
return [tweet[@"iso_language_code"] isEqualToString:@"en"];
})
.map(^NSString *(NSDictionary *tweet) {
NSString *name = tweet[@"from_user_name"];
NSString *text = tweet[@"text"];
return [NSString stringWithFormat:@"%@: %@", name, text];
})
.unwrap;
// With normal ObjC syntax, no dot-abuse. (And hit "Cmd-I" to indent)
NSArray *tweets = [[[[Underscore.array(tweets)
filter](Underscore.isDictionary)
reject](^BOOL (NSDictionary *tweet) {
return [tweet[@"iso_language_code"] isEqualToString:@"en"];
})
map](^NSString *(NSDictionary *tweet) {
NSString *name = tweet[@"from_user_name"];
NSString *text = tweet[@"text"];
return [NSString stringWithFormat:@"%@: %@", name, text];
})
unwrap];
// Dot syntax with best indentation (hit Cmd-] once to move all the lines over)
NSArray *tweets = Underscore.array(results)
.filter(Underscore.isDictionary)
.reject(^BOOL (NSDictionary *tweet) {
return [tweet[@"iso_language_code"] isEqualToString:@"en"];
})
.map(^NSString *(NSDictionary *tweet) {
NSString *name = tweet[@"from_user_name"];
NSString *text = tweet[@"text"];
return [NSString stringWithFormat:@"%@: %@", name, text];
})
.unwrap;
// Normal syntax with equivalent indentation (by hand), to take out the indenting issue
// Compare readability of ".map(" versus "map]("
// Also consider the impact of adding another filter to this chain.
NSArray *tweets = [[[[Underscore.array(tweets)
filter](Underscore.isDictionary)
reject](^BOOL (NSDictionary *tweet) {
return [tweet[@"iso_language_code"] isEqualToString:@"en"];
})
map](^NSString *(NSDictionary *tweet) {
NSString *name = tweet[@"from_user_name"];
NSString *text = tweet[@"text"];
return [NSString stringWithFormat:@"%@: %@", name, text];
})
unwrap];
// For total fairness, here's the traditional (non-functional, no blocks) approach:
NSMutableArray *tweets = [NSMutableArray new];
for (NSDictionary *tweet in results) {
if (! [tweet isKindOfClass:[NSDictionary class]]) {
continue;
}
if ([tweet[@"iso_language_code"] isEqualToString:@"en"]) {
continue;
}
NSString *name = tweet[@"from_user_name"];
NSString *text = tweet[@"text"];
[tweets addObject:[NSString stringWithFormat:@"%@: %@", name, text]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment