CBHMapReduceKit
adds map, filter, and reduce methods to NSArray
, NSSet
, NSOrderedSet
, NSDictionary
and NSEnumerator
.
NSArray<NSNumber *> *array = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10];
NSArray<NSString *> *mapping = [array arrayByMapping:^id(NSNumber *object) {
NSUInteger value = [object unsignedIntValue];
return [NSString stringWithFormat:@"%lu", value + value];
}];
/// mapping => @[@"2", @"4", @"6", @"8", @"10", @"12" @"14", @"16", @"18", @"20"];
NSArray<NSNumber *> *array = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10];
NSArray<NSNumber *> *filtered = [array arrayByFiltering:^BOOL(NSNumber *object) {
return ( [object unsignedIntValue] % 2 == 0 );
}];
/// filtered => @[@2, @4, @6, @8, @10];
NSArray<NSNumber *> *array = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10];
NSNumber *reduction = [array initial:@0 reduce:^NSNumber *(NSNumber *memo, NSNumber *object) {
return @([memo unsignedIntegerValue] + [object unsignedIntValue]);
}];
/// reduction => @55;
- (NSArray<id> *)arrayByMapping:(nullable id (^)(ElementType object))transform;
- (NSMutableArray<id> *)mutableArrayByMapping:(nullable id (^)(ElementType object))transform;
- (NSSet<id> *)setByMapping:(nullable id (^)(ElementType object))transform;
- (NSMutableSet<id> *)mutableSetByMapping:(nullable id (^)(ElementType object))transform;
- (NSOrderedSet<id> *)orderedSetByMapping:(nullable id (^)(ElementType object))transform;
- (NSMutableOrderedSet<id> *)mutableOrderedSetByMapping:(nullable id (^)(ElementType object))transform;
- (instancetype)map:(ElementType (^)(ElementType object))transform;
- (instancetype)compactMap:(nullable ElementType (^)(ElementType object))transform;
- (NSArray<ObjectType> *)arrayByFiltering:(BOOL (^)(ElementType object))predicate;
- (NSMutableArray<ObjectType> *)mutableArrayByFiltering:(BOOL (^)(ElementType object))predicate;
- (NSSet<ObjectType> *)setByFiltering:(BOOL (^)(ElementType object))predicate;
- (NSMutableSet<ObjectType> *)mutableSetByFiltering:(BOOL (^)(ElementType object))predicate;
- (NSOrderedSet<ObjectType> *)orderedSetByFiltering:(BOOL (^)(ElementType object))predicate;
- (NSMutableOrderedSet<ObjectType> *)mutableOrderedSetByFiltering:(BOOL (^)(ElementType object))predicate;
- (instancetype)filter:(BOOL (^)(ElementType object))predicate;
- (nullable id)initial:(nullable id)initial reduce:(nullable id (^)(id __nullable memo, ObjectType object))reduce;
CBHMapReduceKit is available under the ISC license.