-
Notifications
You must be signed in to change notification settings - Fork 15
/
NSArray+F.m
124 lines (98 loc) · 2.86 KB
/
NSArray+F.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
//
// NSArray+F.m
// Functional
//
// Created by Hannes Walz on 07.04.12.
// Copyright 2012 leuchtetgruen. All rights reserved.
//
#import "NSArray+F.h"
@implementation NSArray(F)
- (void) each:(VoidIteratorArrayBlock) block {
[F eachInArray:self withBlock:block];
}
- (void) eachWithIndex:(VoidIteratorArrayWithIndexBlock) block {
[F eachInArrayWithIndex:self withBlock:block];
}
- (NSArray *) map:(MapArrayBlock) block {
return [F mapArray:self withBlock:block];
}
- (id) reduce:(ReduceArrayBlock) block withInitialMemo:(id) memo {
return [F reduceArray:self withBlock:block andInitialMemo:memo];
}
- (NSArray *) bind:(BindArrayBlock) block
{
return [F bindArray:self withBlock:block];
}
- (NSArray *) filter:(BoolArrayBlock) block {
return [F filterArray:self withBlock:block];
}
- (NSArray *) reject:(BoolArrayBlock) block {
return [F rejectArray:self withBlock:block];
}
- (BOOL) isValidForAll:(BoolArrayBlock) block {
return [F allInArray:self withBlock:block];
}
- (BOOL) isValidForAny:(BoolArrayBlock) block {
return [F anyInArray:self withBlock:block];
}
- (NSNumber *) countValidEntries:(BoolArrayBlock) block {
return [F countInArray:self withBlock:block];
}
- (id) max:(CompareArrayBlock) block {
return [F maxArray:self withBlock:block];
}
- (id) min:(CompareArrayBlock) block {
return [F minArray:self withBlock:block];
}
- (NSArray *) dropWhile:(BoolArrayBlock) block {
return [F dropFromArray:self whileBlock:block];
}
// This is really just an alias
- (NSArray *) sort:(NSComparator) block {
return [self sortedArrayUsingComparator:block];
}
- (NSDictionary *) group:(MapArrayBlock) block {
return [F groupArray:self withBlock:block];
}
- (NSArray *) zip:(NSArray *) rhs
{
return [F zipArray:self with:rhs];
}
// Just a helper method
- (id) first {
return [self objectAtIndex:0];
}
- (NSArray *) tail {
return [self arrayFromIndexOn:1];
}
// Just a helper method
- (NSArray *) reverse {
return [[self reverseObjectEnumerator] allObjects];
}
+ (NSArray *) arrayFrom:(NSInteger) from To:(NSInteger) to {
NSMutableArray *mutArr = [NSMutableArray array];
for (NSInteger i=from; i <= to; i++) {
[mutArr addObject:[NSNumber numberWithInteger:i]];
}
return [NSArray arrayWithArray:mutArr];
}
- (id) flatten {
NSMutableArray *mtArr = [NSMutableArray array];
for (id a in self) {
if ([a respondsToSelector:@selector(flatten)]) {
[mtArr addObjectsFromArray:[a flatten]];
} else {
[mtArr addObject:a];
}
}
return [NSArray arrayWithArray:mtArr];
}
// Just a helper method
- (NSArray *) arrayUntilIndex:(NSInteger) idx {
return [self subarrayWithRange:NSMakeRange(0, idx)];
}
// Just a helper method
- (NSArray *) arrayFromIndexOn:(NSInteger) idx {
return [self subarrayWithRange:NSMakeRange(idx, [self count] - idx)];
}
@end