forked from robb/Underscore.m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSArrayWrapper.m
367 lines (300 loc) · 9.58 KB
/
USArrayWrapper.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// USArrayWrapper.m
// Underscore
//
// Created by Robert Böhnke on 5/13/12.
// Copyright (C) 2012 Robert Böhnke
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#import "Underscore.h"
#import "USArrayWrapper.h"
#import "USDictionaryWrapper.h"
@interface USArrayWrapper ()
- initWithArray:(NSArray *)array;
@property (readwrite, retain) NSArray *array;
@end
@implementation USArrayWrapper
#pragma mark Class methods
+ (USArrayWrapper *)wrap:(NSArray *)array
{
return [[USArrayWrapper alloc] initWithArray:[array copy]];
}
#pragma mark Lifecycle
- (id)init
{
return [super init];
}
- (id)initWithArray:(NSArray *)array
{
if (self = [super init]) {
self.array = array;
}
return self;
}
@synthesize array = _array;
- (NSArray *)unwrap
{
return [self.array copy];
}
#pragma mark Underscore methods
- (id)first
{
return self.array.count ? [self.array objectAtIndex:0] : nil;
}
- (id)last
{
return self.array.lastObject;
}
- (USArrayWrapper *(^)(NSUInteger))head
{
return ^USArrayWrapper *(NSUInteger count) {
NSRange range = NSMakeRange(0, MIN(self.array.count, count));
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
NSArray *result = [self.array objectsAtIndexes:indexSet];
return [[USArrayWrapper alloc] initWithArray:result];
};
}
- (USArrayWrapper *(^)(NSUInteger))tail
{
return ^USArrayWrapper *(NSUInteger count) {
NSRange range;
if (count > self.array.count) {
range = NSMakeRange(0, self.array.count);
} else {
range = NSMakeRange(self.array.count - count, count);
}
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
NSArray *result = [self.array objectsAtIndexes:indexSet];
return [[USArrayWrapper alloc] initWithArray:result];
};
}
- (USArrayWrapper *(^)(NSUInteger))drop
{
return ^USArrayWrapper *(NSUInteger count) {
NSUInteger start = MIN(count, self.array.count);
NSRange range = NSMakeRange(start, self.array.count - start);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
NSArray *result = [self.array objectsAtIndexes:indexSet];
return [[USArrayWrapper alloc] initWithArray:result];
};
}
- (NSUInteger (^)(id))indexOf
{
return ^NSUInteger (id obj) {
return [self.array indexOfObject:obj];
};
}
- (USArrayWrapper *)flatten
{
__weak NSArray *array = self.array;
__block NSArray *(^flatten)(NSArray *) = ^NSArray *(NSArray *input) {
NSMutableArray *result = [NSMutableArray array];
for (id obj in input) {
if ([obj isKindOfClass:[NSArray class]]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
[result addObjectsFromArray:flatten(obj)];
#pragma clang diagnostic pop
} else {
[result addObject:obj];
}
}
// If the outmost call terminates, nil the reference to flatten to break
// the retain cycle
if (input == array) {
flatten = nil;
}
return result;
};
return [USArrayWrapper wrap:flatten(self.array)];
}
- (USArrayWrapper *(^)(NSArray *))without
{
return ^USArrayWrapper *(NSArray *value) {
return self.reject(^(id obj){
return [value containsObject:obj];
});
};
}
- (USArrayWrapper *)shuffle
{
NSMutableArray *result = [self.array mutableCopy];
u_int32_t max = (u_int32_t)result.count;
NSAssert(result.count <= INT32_MAX , @"Precision lost in the downcast.");
for (NSInteger i = result.count - 1; i > 0; i--) {
[result exchangeObjectAtIndex:arc4random_uniform(max) withObjectAtIndex:i];
}
return [[USArrayWrapper alloc] initWithArray:result];
}
- (id (^)(id, UnderscoreReduceBlock))reduce
{
return ^USArrayWrapper *(id memo, UnderscoreReduceBlock function) {
for (id obj in self.array) {
memo = function(memo, obj);
}
return memo;
};
}
- (id (^)(id, UnderscoreReduceBlock))reduceRight
{
return ^USArrayWrapper *(id memo, UnderscoreReduceBlock function) {
for (id obj in self.array.reverseObjectEnumerator) {
memo = function(memo, obj);
}
return memo;
};
}
- (USArrayWrapper *(^)(UnderscoreArrayIteratorBlock))each
{
return ^USArrayWrapper *(UnderscoreArrayIteratorBlock block) {
for (id obj in self.array) {
block(obj);
}
return self;
};
}
- (USArrayWrapper *(^)(UnderscoreArrayMapBlock))map
{
return ^USArrayWrapper *(UnderscoreArrayMapBlock block) {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:self.array.count];
for (id obj in self.array) {
id mapped = block(obj);
if (mapped) {
[result addObject:mapped];
}
}
return [[USArrayWrapper alloc] initWithArray:result];
};
}
- (USArrayWrapper *(^)(UnderscoreIndexedArrayMapBlock))indexedMap
{
return ^USArrayWrapper *(UnderscoreIndexedArrayMapBlock block) {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:self.array.count];
[self.array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
id mapped = block(obj, idx);
if (mapped) {
[result addObject:mapped];
}
}];
return [[USArrayWrapper alloc] initWithArray:result];
};
}
- (USArrayWrapper *(^)(NSArray *array, UnderscoreArrayZipWithBlock block))zipWith
{
return ^USArrayWrapper *(NSArray *array, UnderscoreArrayZipWithBlock block) {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:self.array.count];
if(self.array.count <= array.count) {
[self.array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[result addObject:block(obj, array[idx])];
}];
} else {
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[result addObject:block(self.array[idx], obj)];
}];
}
return [[USArrayWrapper alloc] initWithArray:result];
};
}
- (USArrayWrapper *(^)(NSString *))pluck
{
return ^USArrayWrapper *(NSString *keyPath) {
return self.map(^id (id obj) {
return [obj valueForKeyPath:keyPath];
});
};
}
- (USArrayWrapper *)uniq
{
NSOrderedSet *set = [NSOrderedSet orderedSetWithArray:self.array];
return [[USArrayWrapper alloc] initWithArray:[set array]];
}
- (id (^)(UnderscoreTestBlock))find
{
return ^id (UnderscoreTestBlock test) {
for (id obj in self.array) {
if (test(obj)) {
return obj;
}
}
return nil;
};
}
- (USArrayWrapper *(^)(UnderscoreTestBlock))filter
{
return ^USArrayWrapper *(UnderscoreTestBlock test) {
return self.map(^id (id obj) {
return test(obj) ? obj : nil;
});
};
}
- (USArrayWrapper *(^)(UnderscoreTestBlock))reject
{
return ^USArrayWrapper *(UnderscoreTestBlock test) {
return self.filter(Underscore.negate(test));
};
}
- (BOOL (^)(UnderscoreTestBlock))all
{
return ^BOOL (UnderscoreTestBlock test) {
if (self.array.count == 0) {
return NO;
}
BOOL result = YES;
for (id obj in self.array) {
if (!test(obj)) {
return NO;
}
}
return result;
};
}
- (BOOL (^)(UnderscoreTestBlock))any
{
return ^BOOL (UnderscoreTestBlock test) {
BOOL result = NO;
for (id obj in self.array) {
if (test(obj)) {
return YES;
}
}
return result;
};
}
- (USArrayWrapper *(^)(UnderscoreSortBlock))sort
{
return ^USArrayWrapper *(UnderscoreSortBlock block) {
NSArray *result = [self.array sortedArrayUsingComparator:block];
return [[USArrayWrapper alloc] initWithArray:result];
};
}
- (USDictionaryWrapper *(^)(UnderscoreGroupingBlock))groupBy
{
return ^USDictionaryWrapper *(UnderscoreGroupingBlock block) {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for(id obj in self.array) {
id<NSCopying> groupIdentifier = block(obj);
NSArray *groupContents = result[groupIdentifier] ?: @[];
groupContents = [groupContents arrayByAddingObject:obj];
result[groupIdentifier] = groupContents;
}
return [USDictionaryWrapper wrap:result];
};
}
@end