-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathNSArray+map.m
35 lines (31 loc) · 1.28 KB
/
NSArray+map.m
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
#import "YOLO.ph"
@implementation NSArray (YOLOMap)
- (NSArray *(^)(id))map {
return ^id(id frock) {
NSMethodSignature *sig = YOLOMS(frock);
id (^block)(id, NSUInteger) = ^{
switch (sig.numberOfArguments){
case 2: return ^(id a, NSUInteger b){ return ((id(^)(id))frock)(a); };
case 3:
return [sig getArgumentTypeAtIndex:2][0] == '@'
? ^(id a, NSUInteger b){ return ((id(^)(id, id))frock)(a, @(b)); }
: ^(id a, NSUInteger b){ return ((id(^)(id, NSUInteger))frock)(a, b); };
case 4:
return [sig getArgumentTypeAtIndex:2][0] == '@'
? ^(id a, NSUInteger b){ return ((id(^)(id, id, id))frock)(a, @(b), self); }
: ^(id a, NSUInteger b){ return ((id(^)(id, NSUInteger, id))frock)(a, b, self); };
default:
@throw @"Invalid argument count to map";
}
}();
id mapped[self.count];
NSUInteger ii = 0, jj = 0;
for (id mappable in self) {
id o = block(mappable, ii++);
if (o)
mapped[jj++] = o;
}
return [NSArray arrayWithObjects:mapped count:jj];
};
}
@end