This repository was archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBumpTableUtils.m
60 lines (50 loc) · 1.7 KB
/
BumpTableUtils.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#import "BumpTableUtils.h"
@implementation BumpTableUtils
static NSString *indentedString(NSString *s) {
static NSString *n = @"\n";
static NSString *nIndent = @"\n ";
return [s stringByReplacingOccurrencesOfString:n
withString:nIndent];
}
+ (NSString *)indentedDescriptionForObject:(NSObject *)obj {
if ([obj conformsToProtocol:@protocol(NSFastEnumeration)]) {
NSMutableString *s = [NSMutableString stringWithString:@"{\n"];
for (NSObject *o in (id<NSFastEnumeration>)obj) {
[s appendFormat:@"%@\n", [[o class] indentedDescriptionForObject:o]];
}
[s appendString:@"}"];
return indentedString(s);
} else {
return indentedString([self description]);
}
}
+ (NSArray *)mapArray:(NSArray *)arr withBlock:(BumpTableNSArrayUtilTransform)block {
NSMutableArray *toRet = [NSMutableArray arrayWithCapacity:[arr count]];
for (id obj in arr) {
[toRet addObject:block(obj)];
}
return toRet;
}
+ (int)reduceArray:(NSArray *)arr
withInt:(int)starter
block:(BumpTableNSArrayUtilCombinerInt)block {
for (id obj in arr) {
starter = block(starter, obj);
}
return starter;
}
+ (int)sumArray:(NSArray *)arr withBlock:(BumpTableNSArrayUtilIntTransform)block {
return [[self class] reduceArray:arr withInt:0 block:^int(int soFar, id obj) {
return soFar + block(obj);
}];
}
+ (NSArray*)filterArray:(NSArray *)arr withBlock:(BumpTableNSArrayUtilPredicate)block {
NSMutableArray *toRet = [NSMutableArray array];
for (id obj in arr) {
if (block(obj)) {
[toRet addObject:obj];
}
}
return toRet;
}
@end