diff --git a/Classes/NSArray+ObjectiveSugar.h b/Classes/NSArray+ObjectiveSugar.h index 2b3ab37..ecd6b61 100644 --- a/Classes/NSArray+ObjectiveSugar.h +++ b/Classes/NSArray+ObjectiveSugar.h @@ -148,6 +148,21 @@ */ - (NSArray *)reject:(BOOL (^)(id object))block; +/** + Iterate through current array and evaluate the condition on each element + + @param A block that returns YES/NO + */ + +- (BOOL)all:(BOOL (^)(id object))condition; + +/** + Iterate through the current array and return true if element matches the condition given in the block + + @param A block that returns YES/NO + */ + +-(BOOL)any:(BOOL(^)(id object))condition; /** Recurse through self checking for NSArrays and extract all elements into one single array diff --git a/Classes/NSArray+ObjectiveSugar.m b/Classes/NSArray+ObjectiveSugar.m index d00b575..8da56b6 100644 --- a/Classes/NSArray+ObjectiveSugar.m +++ b/Classes/NSArray+ObjectiveSugar.m @@ -135,6 +135,24 @@ - (NSArray *)select:(BOOL (^)(id object))block { return array; } +-(BOOL)all:(BOOL (^)(id))condition { + + for (id object in self) { + if (!condition(object)) { + return false; + } + } + return true; +} + +-(BOOL)any:(BOOL (^)(id))condition { + for (id object in self) { + if (condition(object)) { + return true; + } + } + return false; +} - (id)detect:(BOOL (^)(id object))block { for (id object in self) { diff --git a/Classes/NSSet+ObjectiveSugar.h b/Classes/NSSet+ObjectiveSugar.h index 25c2dbb..6597db4 100644 --- a/Classes/NSSet+ObjectiveSugar.h +++ b/Classes/NSSet+ObjectiveSugar.h @@ -20,7 +20,8 @@ - (NSArray *)select:(BOOL (^)(id object))block; - (NSArray *)reject:(BOOL (^)(id object))block; - (NSArray *)map:(id (^)(id object))block; - +- (BOOL)all:(BOOL (^)(id object))condition; +- (BOOL)any:(BOOL (^)(id object))condition; - (NSArray *)sort; @end diff --git a/Classes/NSSet+ObjectiveSugar.m b/Classes/NSSet+ObjectiveSugar.m index fe8498d..8537e81 100644 --- a/Classes/NSSet+ObjectiveSugar.m +++ b/Classes/NSSet+ObjectiveSugar.m @@ -64,6 +64,26 @@ - (NSArray *)select:(BOOL (^)(id object))block { return array; } +-(BOOL)all:(BOOL (^)(id))condition { + + for (id object in self) { + if (!condition(object)) { + return false; + } + } + return true; +} + +-(BOOL)any:(BOOL (^)(id))condition { + + for (id object in self) { + if (condition(object)) { + return true; + } + } + return false; +} + - (NSArray *)reject:(BOOL (^)(id object))block { NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.count]; diff --git a/Example/ObjectiveSugar.xcworkspace/xcshareddata/ObjectiveSugar.xccheckout b/Example/ObjectiveSugar.xcworkspace/xcshareddata/ObjectiveSugar.xccheckout deleted file mode 100644 index 7944d2e..0000000 --- a/Example/ObjectiveSugar.xcworkspace/xcshareddata/ObjectiveSugar.xccheckout +++ /dev/null @@ -1,41 +0,0 @@ - - - - - IDESourceControlProjectFavoriteDictionaryKey - - IDESourceControlProjectIdentifier - D33A2B74-EF5E-434B-9D41-0462E9F67889 - IDESourceControlProjectName - ObjectiveSugar - IDESourceControlProjectOriginsDictionary - - 10E9E741-B65A-4CD7-B1BA-E8D0970019EE - ssh://github.com/mneorr/ObjectiveSugar.git - - IDESourceControlProjectPath - Example/ObjectiveSugar.xcworkspace - IDESourceControlProjectRelativeInstallPathDictionary - - 10E9E741-B65A-4CD7-B1BA-E8D0970019EE - ../.. - - IDESourceControlProjectURL - ssh://github.com/mneorr/ObjectiveSugar.git - IDESourceControlProjectVersion - 110 - IDESourceControlProjectWCCIdentifier - 10E9E741-B65A-4CD7-B1BA-E8D0970019EE - IDESourceControlProjectWCConfigurations - - - IDESourceControlRepositoryExtensionIdentifierKey - public.vcs.git - IDESourceControlWCCIdentifierKey - 10E9E741-B65A-4CD7-B1BA-E8D0970019EE - IDESourceControlWCCName - ObjectiveSugar - - - - diff --git a/Example/ObjectiveSugarTests/NSArrayCategoriesTests.m b/Example/ObjectiveSugarTests/NSArrayCategoriesTests.m index 0a52524..592fce7 100644 --- a/Example/ObjectiveSugarTests/NSArrayCategoriesTests.m +++ b/Example/ObjectiveSugarTests/NSArrayCategoriesTests.m @@ -40,6 +40,7 @@ [emptyArray.sample shouldBeNil]; }); + context(@"Iterating using block", ^{ it(@"iterates using -each:^", ^{ @@ -97,7 +98,12 @@ return [object integerValue] % 3 == 0; }] should] equal:@[ @3, @6, @9 ]]; }); - + it(@"-all returns true if all elements match the condition in the block", ^{ + [[@([sampleArray all:^BOOL(id object) { + return object!=nil; + }]) should] equal:@(YES)]; + }); + it(@"-detect returns the first element in NSArray for which block is true", ^{ [[[oneToTen detect:^BOOL(id object) { return [object intValue] % 3 == 0; diff --git a/Example/ObjectiveSugarTests/NSSetTests.m b/Example/ObjectiveSugarTests/NSSetTests.m index d5fe31a..b80ba4d 100644 --- a/Example/ObjectiveSugarTests/NSSetTests.m +++ b/Example/ObjectiveSugarTests/NSSetTests.m @@ -36,7 +36,11 @@ }]; [[duplicate should] beEmpty]; }); - + it(@"-all returns true if all elements match the condition in the block", ^{ + [[@([sampleSet all:^BOOL(id object) { + return object!=nil; + }]) should] equal:@(YES)]; + }); }); context(@"first, last, sample", ^{