forked from petejkim/ConciseKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSArray+ConciseKit.m
74 lines (60 loc) · 1.72 KB
/
NSArray+ConciseKit.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* ConciseKit
* Copyright 2010 Peter Jihoon Kim
* Licensed under the MIT License.
*/
#import "NSArray+ConciseKit.h"
@implementation NSArray (ConciseKit)
- (id)$first {
return [self objectAtIndex:0];
}
- (id)$last {
return [self lastObject];
}
- (id)$at:(NSUInteger)index {
return [self objectAtIndex:index];
}
- (NSArray *)$each:(void (^)(id obj))block {
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj);
}];
return self;
}
- (NSArray *)$eachWithIndex:(void (^)(id obj, NSUInteger idx))block {
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj, idx);
}];
return self;
}
- (NSArray *)$eachWithStop:(void (^)(id obj, BOOL *stop))block {
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj, stop);
}];
return self;
}
- (NSArray *)$eachWithIndexAndStop:(void (^)(id obj, NSUInteger idx, BOOL *stop))block {
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj, idx, stop);
}];
return self;
}
- (NSArray *)$map:(id (^)(id obj))block {
__block NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[array addObject:block(obj)];
}];
return array;
}
- (NSArray *)$mapWithIndex:(id (^)(id obj, NSUInteger idx))block {
__block NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[array addObject:block(obj, idx)];
}];
return array;
}
@end
@implementation NSMutableArray (ConciseKit)
- (NSMutableArray *)$push:(id)anObject {
[self addObject:anObject];
return self;
}
@end