forked from BlocksKit/BlocksKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSMutableArray+BlocksKit.m
More file actions
56 lines (38 loc) · 1.08 KB
/
Copy pathNSMutableArray+BlocksKit.m
File metadata and controls
56 lines (38 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// NSMutableArray+BlocksKit.m
// BlocksKit
//
#import "NSMutableArray+BlocksKit.h"
@implementation NSMutableArray (BlocksKit)
- (void)performSelect:(BKValidationBlock)block {
NSParameterAssert(block != nil);
NSIndexSet *list = [self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return !block(obj);
}];
if (!list.count)
return;
[self removeObjectsAtIndexes:list];
}
- (void)performReject:(BKValidationBlock)block {
NSParameterAssert(block != nil);
NSIndexSet *list = [self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return block(obj);
}];
if (!list.count)
return;
[self removeObjectsAtIndexes:list];
}
- (void)performMap:(BKTransformBlock)block {
NSParameterAssert(block != nil);
NSMutableArray *new = [self mutableCopy];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id value = block(obj);
if (!value)
value = [NSNull null];
if ([value isEqual:obj])
return;
[new replaceObjectAtIndex:idx withObject:value];
}];
[self setArray:[new autorelease]];
}
@end