Skip to content

Commit

Permalink
NSArray.any()
Browse files Browse the repository at this point in the history
Closes #9.
  • Loading branch information
mxcl committed Mar 1, 2014
1 parent 7e1211a commit d5c7a2f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
9 changes: 9 additions & 0 deletions NSArray+Ruby.m
Expand Up @@ -253,6 +253,15 @@ - (NSComparisonResult)compare:(NSArray *)array {
};
}

- (BOOL(^)(BOOL(^)(id o)))any {
return ^(BOOL(^block)(id o)){
for (id o in self)
if (block(o))
return YES;
return NO;
};
}

@end


Expand Down
8 changes: 8 additions & 0 deletions README.markdown
Expand Up @@ -435,6 +435,14 @@ BOOL rv = @[@1, @2, @3].none(NSString.class);
// rv => YES
```

###NSArray.any()
```objc
BOOL rv = @[@1, @2, @3].any(^BOOL(id o){
return [o intValue] == 3;
});
// rv => YES
```

###NSDictionary.extend()
```objc
id rv = @{@1: @1, @2: @4}.extend(@{@1: @9, @10: @100});
Expand Down
1 change: 1 addition & 0 deletions YOLO.h
Expand Up @@ -6,6 +6,7 @@

@interface NSArray (RubyEnumerable)
- (BOOL(^)(id blockOrClass))all;
- (BOOL(^)(BOOL (^)(id o)))any;
- (NSArray *(^)(void (^)(id o)))each;
- (NSArray *(^)(void (^)(id o, uint index)))eachWithIndex;
- (id(^)(BOOL (^)(id o)))find;
Expand Down
7 changes: 6 additions & 1 deletion tests
Expand Up @@ -459,6 +459,11 @@ int main() {
STAssertEqualObjects(aa, bb, @"");
}
- (void)testAny {
NSArray *aa = @[@1, @2, @3, @4];
STAssertTrue(aa.any(^BOOL(id o){ return [o intValue] == 4; }), @"");
}
@end
Expand All @@ -468,7 +473,7 @@ __EOBJC__
FRAMEWORKS = "/Applications/Xcode.app/Contents/Developer/Library/Frameworks"

File.open('/tmp/YOLOtests.m', 'w') do |f|
f.puts("\n") # make line numbers line up
f.puts("\n\n") # make line numbers line up
f.puts(OBJC)
end

Expand Down

0 comments on commit d5c7a2f

Please sign in to comment.