From aa66e795705b1aedccd434e260bf6a9f0fb4b48e Mon Sep 17 00:00:00 2001 From: Denis Lebedev Date: Sat, 14 Dec 2013 00:36:11 +0300 Subject: [PATCH 1/2] Added -groupBy to NSArray additions --- Classes/NSArray+ObjectiveSugar.h | 9 +++++++++ Classes/NSArray+ObjectiveSugar.m | 19 +++++++++++++++++++ .../NSArrayCategoriesTests.m | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Classes/NSArray+ObjectiveSugar.h b/Classes/NSArray+ObjectiveSugar.h index bcf7399..6a37a76 100644 --- a/Classes/NSArray+ObjectiveSugar.h +++ b/Classes/NSArray+ObjectiveSugar.h @@ -218,4 +218,13 @@ - (NSArray *)symmetricDifference:(NSArray *)array; +/** + Groups the collection by result of the block. + + @return A dictionary where the keys are the evaluated result from + the block and the values are arrays of elements in the collection that correspond to the key. +*/ + +- (NSDictionary *)groupBy:(id (^)(id object))block; + @end diff --git a/Classes/NSArray+ObjectiveSugar.m b/Classes/NSArray+ObjectiveSugar.m index 329326f..8d0770d 100644 --- a/Classes/NSArray+ObjectiveSugar.m +++ b/Classes/NSArray+ObjectiveSugar.m @@ -176,6 +176,25 @@ - (NSArray *)reverse { return self.reverseObjectEnumerator.allObjects; } +- (NSDictionary *)groupBy:(id (^)(id object))block { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + + for (id object in self) { + id key = block(object); + + if (![key conformsToProtocol:@protocol(NSCopying)]) { + [NSException raise:NSGenericException format:@"%@ does not conform to ", ^{ + [[theBlock(^{ + [@[@1] groupBy:^id(id object) { + return [[NSObject alloc] init]; + }]; + }) should] raiseWithName:NSGenericException]; + + }); + + }); }); From 8b5351909b74309102b84a97140febb7eec81aba Mon Sep 17 00:00:00 2001 From: Denis Lebedev Date: Mon, 16 Dec 2013 22:06:24 +0300 Subject: [PATCH 2/2] Add -dictionaryMap: method --- Classes/NSDictionary+ObjectiveSugar.h | 2 ++ Classes/NSDictionary+ObjectiveSugar.m | 11 +++++++++++ Example/ObjectiveSugarTests/NSDictionaryTests.m | 14 ++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Classes/NSDictionary+ObjectiveSugar.h b/Classes/NSDictionary+ObjectiveSugar.h index 1dce388..7f2b911 100644 --- a/Classes/NSDictionary+ObjectiveSugar.h +++ b/Classes/NSDictionary+ObjectiveSugar.h @@ -14,6 +14,8 @@ - (void)eachKey:(void (^)(id key))block; - (void)eachValue:(void (^)(id value))block; - (NSArray *)map:(id (^)(id key, id value))block; +- (NSDictionary *)dictionaryMap:(id (^)(id key, id value))block; + - (BOOL)hasKey:(id)key; @end diff --git a/Classes/NSDictionary+ObjectiveSugar.m b/Classes/NSDictionary+ObjectiveSugar.m index 96ddae7..5d7f5a2 100644 --- a/Classes/NSDictionary+ObjectiveSugar.m +++ b/Classes/NSDictionary+ObjectiveSugar.m @@ -41,6 +41,17 @@ - (NSArray *)map:(id (^)(id key, id value))block { return array; } +- (NSDictionary *)dictionaryMap:(id (^)(id key, id value))block { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + + [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + id result = block(key, obj); + dictionary[key] = result; + }]; + + return dictionary; +} + - (BOOL)hasKey:(id)key { return !!self[key]; } diff --git a/Example/ObjectiveSugarTests/NSDictionaryTests.m b/Example/ObjectiveSugarTests/NSDictionaryTests.m index 2b8a389..e6189aa 100644 --- a/Example/ObjectiveSugarTests/NSDictionaryTests.m +++ b/Example/ObjectiveSugarTests/NSDictionaryTests.m @@ -68,6 +68,20 @@ [[mapped should] equal:sampleDict.allValues]; }); + + it(@"maps new values to existing keys", ^{ + NSDictionary *result = [sampleDict dictionaryMap:^id(id key, id value) { + counter++; + return @([value integerValue] + 1); + }]; + + [[result should] equal:@{ + @"one" : @2, + @"two" : @3, + @"three" : @4 + }]; + + }); }); describe(@"Keys", ^{