-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
NSDictionary+fmap.m
32 lines (28 loc) · 975 Bytes
/
NSDictionary+fmap.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
#import "YOLO.ph"
@implementation NSDictionary (YOLOFMap)
- (NSDictionary *(^)(id))fmap {
return ^(id frock) {
NSMethodSignature *sig = YOLOMS(frock);
id (^block)(id, id) = ^{
switch (sig.numberOfArguments){
case 2: return ^(id v, id k){ return ((id(^)(id))frock)(v); };
case 3: return ^(id v, id k){ return ((id(^)(id, id))frock)(v, k); };
case 4: return ^(id v, id k){ return ((id(^)(id, id, id))frock)(v, k, self); };
default:
@throw @"Invalid argument count to fmap";
}
}();
id keys[self.count];
id objs[self.count];
int ii = 0;
for (id key in self) {
id o = block(self[key], key);
if (o) {
keys[ii] = key;
objs[ii++] = o;
}
}
return [NSDictionary dictionaryWithObjects:objs forKeys:keys count:ii];
};
}
@end