Skip to content

Adds map, filter, and reduce methods to common collection types

License

Notifications You must be signed in to change notification settings

chris-huxtable/CBHMapReduceKit

Repository files navigation

CBHMapReduceKit

release pod licence coverage

CBHMapReduceKit adds map, filter, and reduce methods to NSArray, NSSet, NSOrderedSet, NSDictionary and NSEnumerator.

Examples

Mapping:

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"];

Filtering:

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];

Reducing:

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;

Brief Outline of Methods

Mapping :

To NSArray:

- (NSArray<id> *)arrayByMapping:(nullable id (^)(ElementType object))transform;
- (NSMutableArray<id> *)mutableArrayByMapping:(nullable id (^)(ElementType object))transform;

To NSSet :

- (NSSet<id> *)setByMapping:(nullable id (^)(ElementType object))transform;
- (NSMutableSet<id> *)mutableSetByMapping:(nullable id (^)(ElementType object))transform;

To NSOrderedSet:

- (NSOrderedSet<id> *)orderedSetByMapping:(nullable id (^)(ElementType object))transform;
- (NSMutableOrderedSet<id> *)mutableOrderedSetByMapping:(nullable id (^)(ElementType object))transform;

Mutating:

- (instancetype)map:(ElementType (^)(ElementType object))transform;
- (instancetype)compactMap:(nullable ElementType (^)(ElementType object))transform;

Filtering:

To NSArray:

- (NSArray<ObjectType> *)arrayByFiltering:(BOOL (^)(ElementType object))predicate;
- (NSMutableArray<ObjectType> *)mutableArrayByFiltering:(BOOL (^)(ElementType object))predicate;

To NSSet:

- (NSSet<ObjectType> *)setByFiltering:(BOOL (^)(ElementType object))predicate;
- (NSMutableSet<ObjectType> *)mutableSetByFiltering:(BOOL (^)(ElementType object))predicate;

To NSOrderedSet:

- (NSOrderedSet<ObjectType> *)orderedSetByFiltering:(BOOL (^)(ElementType object))predicate;
- (NSMutableOrderedSet<ObjectType> *)mutableOrderedSetByFiltering:(BOOL (^)(ElementType object))predicate;

Mutating:

- (instancetype)filter:(BOOL (^)(ElementType object))predicate;

Reduce:

- (nullable id)initial:(nullable id)initial reduce:(nullable id (^)(id __nullable memo, ObjectType object))reduce;

Licence

CBHMapReduceKit is available under the ISC license.

About

Adds map, filter, and reduce methods to common collection types

Resources

License

Stars

Watchers

Forks

Packages

No packages published