nst / nsarray-functional

Objective-C category to add Python-like map, filter and reduce methods to Cocoa NSArray.

This URL has Read+Write access

nsarray-functional / Functional.m
100644 21 lines (12 sloc) 0.661 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#import <Foundation/Foundation.h>
#import "NSArray+Functional.h"
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
NSArray *a = [NSArray arrayWithObjects:@"a", @"ab", @"abc", @"bc", @"c", nil];
 
NSLog(@"-- f1: %@", [a filterUsingSelector:@selector(hasPrefix:), @"a", nil]);
 
NSLog(@"-- m1: %@", [a mapUsingSelector:@selector(uppercaseString), nil]);
 
NSLog(@"-- m2: %@", [a mapUsingSelector:@selector(stringByReplacingOccurrencesOfString:withString:), @"b", @"+", nil]);
 
NSLog(@"-- r1: %@", [a reduceUsingSelector:@selector(stringByAppendingString:)]);
 
[pool drain];
 
    return 0;
}