-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCLinq.m
272 lines (230 loc) · 6.7 KB
/
OCLinq.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// OCLinq.m
// LINQForObjective-C
//
// Created by Yao Long on 7/18/16.
// Copyright © 2016 Yao Long. All rights reserved.
//
#import "OCLinq.h"
#import <objc/runtime.h>
@interface OCLinq ()
@property (nonatomic) NSEnumerator *enumerator;
- (NSArray *)allObjects;
@end
@implementation OCLinq
- (NSEnumerator *)objectEnumerator {
return self.enumerator;
}
- (NSArray *)allObjects {
return self.enumerator.allObjects;
}
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
return [self.enumerator countByEnumeratingWithState:state objects:buffer count:len];
}
+ (instancetype)from:(id)source {
NSEnumerator *e = nil;
if ([source isKindOfClass:[NSEnumerator class]]) {
e = source;
} else if ([source isKindOfClass:[NSArray class]] ||
[source isKindOfClass:[NSDictionary class]] ||
[source isKindOfClass:[NSSet class]]) {
e = [source performSelector:@selector(objectEnumerator)];
} else {
return nil;
}
return [self fromEnumerator:e];
}
+ (instancetype)fromEnumerator:(NSEnumerator *)enumerator {
OCLinq *instance = [[self alloc] init];
instance.enumerator = enumerator;
return instance;
}
- (instancetype)where:(OCLQPredicate)predicate {
NSMutableArray *result = [NSMutableArray array];
for (id item in self) {
if (predicate(item)) {
[result addObject:item];
}
}
return [self.class from:result];
}
- (instancetype)select:(OCLQTransform)transform {
NSMutableArray *result = [NSMutableArray array];
for (id item in self) {
id obj = transform(item);
if (obj != nil) {
[result addObject:obj];
}
}
return [self.class from:result];
}
- (instancetype)selectMany:(OCLQTransform)transform {
NSMutableArray *result = [NSMutableArray array];
for (id list in self) {
for (id item in list) {
id obj = transform(item);
if (obj != nil) {
[result addObject:obj];
}
}
}
return [self.class from:result];
}
- (instancetype)orderBy:(OCLQKeySelector)keySelector {
return [self.class from:[self.allObjects sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
id valueOne = keySelector(obj1);
id valueTwo = keySelector(obj2);
NSComparisonResult result = [valueOne compare:valueTwo];
return result;
}]];
}
- (instancetype)order {
return [self orderBy:^id(id item){return item;}];
}
- (instancetype)distinct {
NSMutableArray* result = [[NSMutableArray alloc] init];
for (id item in self) {
if (![result containsObject:item]) {
[result addObject:item];
}
}
return [self.class from:result];
}
- (instancetype)aggregate:(OCLQAggregateBlock)aggregate {
id result = nil;
for (id item in self) {
result = result == nil ? item : aggregate(item, result);
}
return result;
}
- (BOOL)any:(OCLQPredicate)predicate {
for (id item in self) {
if (predicate(item)) {
return YES;
}
}
return NO;
}
- (BOOL)all:(OCLQPredicate)predicate {
for (id item in self) {
if (!predicate(item)) {
return NO;
}
}
return YES;
}
- (id)average {
return [self.allObjects valueForKeyPath:@"@avg.self"];
}
- (id)max {
return [self.allObjects valueForKeyPath:@"@max.self"];
}
- (id)min {
return [self.allObjects valueForKeyPath:@"@min.self"];
}
- (id)sum {
return [self.allObjects valueForKeyPath:@"@sum.self"];
}
- (instancetype)skip:(NSUInteger)count {
NSArray *all = self.allObjects;
NSArray *array = nil;
if (count < all.count) {
NSRange range = {count, all.count - count};
array = [all subarrayWithRange:range];
} else {
array = @[];
}
return [self.class from:array];
}
- (instancetype)skipWhile:(OCLQPredicate)predicate {
NSUInteger count = 0;
for (id item in self) {
if (predicate(item)) {
count++;
} else {
break;
}
}
return [self skip:count];
}
- (instancetype)take:(NSUInteger)count {
NSArray *all = self.allObjects;
NSRange range = {0, count > all.count ? all.count : count};
return [self.class from:[all subarrayWithRange:range]];
}
- (instancetype)takeWhile:(OCLQPredicate)predicate {
NSUInteger count = 0;
for (id item in self) {
if (!predicate(item)) {
count++;
} else {
break;
}
}
return [self take:count];
}
- (id)firstOrDefault:(id)defaultObj {
NSArray *all = self.allObjects;
return all.count > 0 ? all.firstObject : defaultObj;
}
- (id)lastOrDefault:(id)defaultObj {
NSArray *all = self.allObjects;
return all.count > 0 ? all.lastObject : defaultObj;
}
- (instancetype)reverse {
return [self.class from:self.allObjects.reverseObjectEnumerator];
}
- (instancetype)contact:(OCLinq *)linq {
NSMutableArray *result = [NSMutableArray arrayWithArray:self.allObjects];
[result addObjectsFromArray:linq.allObjects];
return [self.class from:result];
}
- (NSDictionary *)groupBy:(OCLQKeySelector)keySelector {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (id item in self) {
id key = keySelector(item);
if (key != nil) {
NSMutableArray *arrayForKey = result[key];
if (arrayForKey == nil) {
arrayForKey = [NSMutableArray array];
result[key] = arrayForKey;
}
[arrayForKey addObject:item];
}
}
return result.copy;
}
- (instancetype)join:(OCLinq *)linq keySelector1:(OCLQKeySelector)keySelctor1 keySelector2:(OCLQKeySelector)keySelctor2 {
NSMutableArray *pairs = [NSMutableArray array];
for (id item1 in self) {
for (id item2 in linq) {
id first = keySelctor1(item1);
if ([first isEqual:keySelctor2(item2)]) {
OCLQPair *outter = [[OCLQPair alloc] init];
OCLQPair *inner = [[OCLQPair alloc] init];
inner.first = item1;
inner.second = item2;
outter.first = first;
outter.second = inner;
[pairs addObject:outter];
}
}
}
return [self.class from:pairs];
}
- (NSArray *)toArray {
return self.allObjects;
}
- (NSDictionary *)toDictionary:(OCLQKeySelector)keySelector {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (id item in self) {
dict[keySelector(item)] = item;
}
return dict.copy;
}
- (NSSet *)toSet {
return [NSSet setWithArray:self.allObjects];
}
@end
@implementation OCLQPair
@end