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
42 lines (31 loc) · 906 Bytes
/
Copy pathNSMutableArray+BlocksKit.m
File metadata and controls
42 lines (31 loc) · 906 Bytes
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
//
// NSMutableArray+BlocksKit.m
// BlocksKit
//
#import "NSMutableArray+BlocksKit.h"
@implementation NSMutableArray (BlocksKit)
- (void)bk_performSelect:(BOOL (^)(id obj))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)bk_performReject:(BOOL (^)(id obj))block {
NSParameterAssert(block != nil);
return [self bk_performSelect:^BOOL(id obj) {
return !block(obj);
}];
}
- (void)bk_performMap:(id (^)(id obj))block {
NSParameterAssert(block != nil);
NSMutableArray *new = [self mutableCopy];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id value = block(obj) ?: [NSNull null];
if ([value isEqual:obj]) return;
new[idx] = value;
}];
[self setArray:new];
}
@end