Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[NEW] Collect/CollectWithIndex/Detect/Each/EachWithIndex/Inject/Rejec…

…t/Select category extensions for NSSet, tests to validate
  • Loading branch information...
commit ba5610750b35dd2f8b857e49441eafe10179bffe 1 parent b73ec59
@crafterm authored
View
2  MRCEnumerable.h
@@ -9,5 +9,7 @@
#import <Foundation/Foundation.h>
#import "NSArray+Utilities.h"
+#import "NSSet+Utilities.h"
#import "NSArray+Enumerable.h"
+#import "NSSet+Enumerable.h"
#import "NSDictionary+Enumerable.h"
View
2  NSDictionary+Enumerable.m
@@ -7,7 +7,7 @@
//
#import "NSDictionary+Enumerable.h"
-
+#import "NSArray+Utilities.h"
@implementation NSDictionary (Enumerable)
View
23 NSSet+Enumerable.h
@@ -0,0 +1,23 @@
+//
+// NSSet+Enumerable.h
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 27/03/11.
+// Copyright 2011 Red Artisan. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface NSSet (Enumerable)
+
+- (void)each:(void (^)(id obj))block;
+- (void)eachWithIndex:(void (^)(id obj, NSUInteger idx))block;
+- (NSSet *)collect:(id (^)(id obj))block;
+- (NSSet *)collectWithIndex:(id (^)(id obj, NSUInteger idx))block;
+- (id)inject:(id)m :(id (^)(id m, id obj))block;
+- (NSSet *)select:(BOOL (^)(id obj))block;
+- (NSSet *)reject:(BOOL (^)(id obj))block;
+- (id)detect:(BOOL (^)(id obj))block;
+
+@end
View
76 NSSet+Enumerable.m
@@ -0,0 +1,76 @@
+//
+// NSSet+Enumerable.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 27/03/11.
+// Copyright 2011 Red Artisan. All rights reserved.
+//
+
+#import "NSSet+Enumerable.h"
+#import "NSArray+Utilities.h"
+
+@implementation NSSet (Enumerable)
+
+- (void)each:(void (^)(id obj))block {
+ [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { block(obj); }];
+}
+
+- (void)eachWithIndex:(void (^)(id obj, NSUInteger idx))block {
+ NSUInteger idx = 0;
+
+ for (id object in self) {
+ block(object, idx++);
+ }
+}
+
+- (NSSet *)collect:(id (^)(id obj))block {
+ NSMutableSet * results = [NSMutableSet setWithCapacity:[self count]];
+ [self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { [results addObject:block(obj)]; }];
+ return results;
+}
+
+
+- (NSSet *)collectWithIndex:(id (^)(id obj, NSUInteger idx))block {
+ NSMutableSet * results = [NSMutableSet setWithCapacity:[self count]];
+ NSUInteger idx = 0;
+
+ for (id object in self) {
+ [results addObject:block(object, idx++)];
+ }
+
+ return results;
+}
+
+- (id)inject:(id)m :(id (^)(id m, id obj))block {
+ id result = m ? m : [[self allObjects] firstObject];
+
+ for (id object in self) {
+ result = block(result, object);
+ }
+
+ return result;
+}
+
+- (NSSet *)select:(BOOL (^)(id obj))block {
+ return [self inject:[NSMutableSet set] :^(id m, id obj) {
+ if (block(obj) == YES) [m addObject:obj];
+ return m;
+ }];
+}
+
+- (NSSet *)reject:(BOOL (^)(id obj))block {
+ return [self inject:[NSMutableSet set] :^(id m, id obj) {
+ if (block(obj) == NO) [m addObject:obj];
+ return m;
+ }];
+}
+
+- (id)detect:(BOOL (^)(id obj))block {
+ for (id object in self) {
+ if (block(object)) return object;
+ }
+
+ return nil;
+}
+
+@end
View
16 NSSet+Utilities.h
@@ -0,0 +1,16 @@
+//
+// NSSet+Utilities.h
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 29/03/11.
+// Copyright 2011 Red Artisan. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface NSSet (NSSet_Utilities)
+
+- (BOOL)empty;
+
+@end
View
18 NSSet+Utilities.m
@@ -0,0 +1,18 @@
+//
+// NSSet+Utilities.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 29/03/11.
+// Copyright 2011 Red Artisan. All rights reserved.
+//
+
+#import "NSSet+Utilities.h"
+
+
+@implementation NSSet (NSSet_Utilities)
+
+- (BOOL)empty {
+ return [self count] == 0;
+}
+
+@end
View
23 Tests/NSSet+Collect+Test.m
@@ -0,0 +1,23 @@
+//
+// NSSet+Collect+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_Collect_Test : GHTestCase
+@end
+
+@implementation NSSet_Collect_Test
+
+- (void)testCollectShouldCallTheBlockOverEachElementCollectingResults {
+ NSSet * set = [NSSet setWithObjects:@"dog", @"mouse", nil];
+ NSSet * results = [set collect:^(id obj) {
+ return [NSString stringWithFormat:@"%@ %d", obj, [obj length]];
+ }];
+ NSSet * expected = [NSSet setWithObjects:@"dog 3", @"mouse 5", nil];
+ GHAssertEqualObjects(expected, results, @"should be equal");
+}
+
+@end
View
23 Tests/NSSet+CollectWithIndex+Test.m
@@ -0,0 +1,23 @@
+//
+// NSSet+CollectWithIndex+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_CollectWithIndex_Test : GHTestCase
+@end
+
+@implementation NSSet_CollectWithIndex_Test
+
+- (void)testCollectShouldCallTheBlockOverEachElementCollectingResults {
+ NSSet * set = [NSSet setWithObjects:@"dog", @"mouse", nil];
+ NSSet * results = [set collectWithIndex:^(id obj, NSUInteger idx) {
+ return [NSString stringWithFormat:@"%@ %d", obj, idx];
+ }];
+ NSSet * expected = [NSSet setWithObjects:@"dog 0", @"mouse 1", nil];
+ GHAssertEqualObjects(expected, results, @"should be equal");
+}
+
+@end
View
22 Tests/NSSet+Detect+Test.m
@@ -0,0 +1,22 @@
+//
+// NSSet+Detect+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_Detect_Test : GHTestCase
+@end
+
+@implementation NSSet_Detect_Test
+
+- (void)testReturnTheFirstItemReturningTrueWithTheGivenBlock {
+ NSSet * set = [NSSet setWithObjects:@"dog", @"cat", nil];
+ NSString * result = [set detect:^(id obj) {
+ return (BOOL)([obj length] == 3);
+ }];
+ GHAssertEqualObjects(@"dog", result, @"should be equal");
+}
+
+@end
View
21 Tests/NSSet+Each+Test.m
@@ -0,0 +1,21 @@
+//
+// NSSet+Each+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_Each_Test : GHTestCase
+@end
+
+@implementation NSSet_Each_Test
+
+- (void)testEachShouldCallTheBlockOverEachElement {
+ NSSet * set = [NSSet setWithObject:@"dog"];
+ [set each:^(id obj) {
+ GHAssertEquals(@"dog", obj, @"should be equal");
+ }];
+}
+
+@end
View
22 Tests/NSSet+EachWithIndex+Test.m
@@ -0,0 +1,22 @@
+//
+// NSSet+EachWithIndex+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_EachWithIndex_Test : GHTestCase
+@end
+
+@implementation NSSet_EachWithIndex_Test
+
+- (void)testEachShouldCallTheBlockOverEachElement {
+ NSSet * set = [NSSet setWithObject:@"dog"];
+ [set eachWithIndex:^(id obj, NSUInteger idx) {
+ GHAssertEquals(@"dog", obj, @"should be equal");
+ GHAssertTrue(0 == idx, @"index should equal");
+ }];
+}
+
+@end
View
24 Tests/NSSet+Empty+Test.m
@@ -0,0 +1,24 @@
+//
+// NSSet+Utilities+Test
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_Empty_Test : GHTestCase
+@end
+
+@implementation NSSet_Empty_Test
+
+- (void)testEmptyShouldBeEmpty {
+ NSSet * emptySet = [NSSet set];
+ GHAssertTrue([emptySet empty], @"should be empty");
+}
+
+- (void)testNonEmptyArrayShouldNotBeEmpty {
+ NSSet * nonEmptySet = [NSSet setWithObject:@"element"];
+ GHAssertFalse([nonEmptySet empty], @"should not be empty");
+}
+
+@end
View
36 Tests/NSSet+Inject+Test.m
@@ -0,0 +1,36 @@
+//
+// NSSet+Inject+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_Inject_Test : GHTestCase
+@end
+
+@implementation NSSet_Inject_Test
+
+// it should call the block over each element, passing the results of previous block to the next
+
+- (void)testInjectShouldCallTheBlockOverEachElementPassingPreviousResultToNext {
+ NSSet * set = [NSSet setWithObjects:@"dog", @"mouse", nil];
+ NSSet * results = [set inject:[NSMutableDictionary dictionary] :^(id m, id obj) {
+ [m setValue:[NSString stringWithFormat:@"%d", [obj length]] forKey:obj];
+ return m;
+ }];
+ NSDictionary * expected = [NSDictionary dictionaryWithObjectsAndKeys:@"3", @"dog", @"5", @"mouse", nil];
+ GHAssertEqualObjects(expected, results, @"should be equal");
+}
+
+// it should use the first element in the set as the initial result when not provided
+
+- (void)testInjectShouldUseTheFirstElementInTheArrayAsTheInitialWhenNotProvided {
+ NSSet * set = [NSSet setWithObjects:@"", @"dog", @"mouse", nil];
+ NSString * results = [set inject:nil :^(id m, id obj) {
+ return (id)[m stringByAppendingString:obj];
+ }];
+ GHAssertEqualObjects(@"dogmouse", results, @"should be equal");
+}
+
+@end
View
23 Tests/NSSet+Reject+Test.m
@@ -0,0 +1,23 @@
+//
+// NSSet+Reject+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_Reject_Test : GHTestCase
+@end
+
+@implementation NSSet_Reject_Test
+
+- (void)testRejectShouldCollectElementsReturningFalseWithTheSuppliedBlock {
+ NSSet * set = [NSSet setWithObjects:@"dog", @"mouse", nil];
+ NSSet * results = [set reject:^(id obj) {
+ return (BOOL)([obj length] == 3);
+ }];
+ NSSet * expected = [NSSet setWithObject:@"mouse"];
+ GHAssertEqualObjects(expected, results, @"should be equal");
+}
+
+@end
View
23 Tests/NSSet+Select+Test.m
@@ -0,0 +1,23 @@
+//
+// NSSet+Select+Test.m
+// MRCEnumerable
+//
+// Created by Marcus Crafter on 16/11/10.
+// Copyright 2010 Red Artisan. All rights reserved.
+//
+
+@interface NSSet_Select_Test : GHTestCase
+@end
+
+@implementation NSSet_Select_Test
+
+- (void)testSelectShouldCollectElementsReturningTrueWithTheSuppliedBlock {
+ NSSet * set = [NSSet setWithObjects:@"dog", @"mouse", nil];
+ NSSet * results = [set select:^(id obj) {
+ return (BOOL)([obj length] == 3);
+ }];
+ NSSet * expected = [NSSet setWithObject:@"dog"];
+ GHAssertEqualObjects(expected, results, @"should be equal");
+}
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.