forked from BlocksKit/BlocksKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSArray+BlocksKit.h
More file actions
executable file
·114 lines (85 loc) · 3.77 KB
/
Copy pathNSArray+BlocksKit.h
File metadata and controls
executable file
·114 lines (85 loc) · 3.77 KB
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
//
// NSArray+BlocksKit.h
// %PROJECT
//
#import "BKGlobals.h"
/** Block extensions for NSArray.
Both inspired by and resembling Smalltalk syntax, these utilities
allows for iteration of an array in a concise way that
saves quite a bit of boilerplate code for filtering or finding
objects or an object.
Includes code by the following:
- Robin Lu. <https://github.com/robin>. 2009. MIT.
- Michael Ash. <https://github.com/mikeash>. 2010. BSD.
- Aleks Nesterow. <https://github.com/nesterow>. 2010. BSD.
- Zach Waldowski. <https://github.com/zwaldowski>. 2011. MIT.
@see NSDictionary(BlocksKit)
@see NSSet(BlocksKit)
*/
@interface NSArray (BlocksKit)
/** Loops through an array and executes the given block with each object.
@param block A single-argument, void-returning code block.
*/
- (void)each:(BKSenderBlock)block;
/** Enumerates through an array concurrently and executes
the given block once for each object.
Enumeration will occur on appropriate background queues. This
will have a noticeable speed increase, especially on dual-core
devices, but you *must* be aware of the thread safety of the
objects you message from within the block. Be aware that the
order of objects is not necessarily the order each block will
be called in.
@param block A single-argument, void-returning code block.
*/
- (void)apply:(BKSenderBlock)block;
/** Loops through an array to find the object matching the block.
match: is functionally identical to select:, but will stop and return
on the first match.
@param block A single-argument, `BOOL`-returning code block.
@return Returns the object if found, `nil` otherwise.
@see select:
*/
- (id)match:(BKValidationBlock)block;
/** Loops through an array to find the objects matching the block.
@param block A single-argument, BOOL-returning code block.
@return Returns an array of the objects found, `nil` otherwise.
@see match:
*/
- (NSArray *)select:(BKValidationBlock)block;
/** Loops through an array to find the objects not matching the block.
This selector performs *literally* the exact same function as select: but in reverse.
This is useful, as one may expect, for removing objects from an array.
NSArray *new = [computers reject:^BOOL(id obj) {
return ([obj isUgly]);
}];
@param block A single-argument, BOOL-returning code block.
@return Returns an array of all objects not found, `nil` if all are excluded.
*/
- (NSArray *)reject:(BKValidationBlock)block;
/** Call the block once for each object and create an array of the return values.
This is sometimes referred to as a transform, mutating one of each object:
NSArray *new = [stringArray map:^id(id obj) {
return [obj stringByAppendingString:@".png"]);
}];
@param block A single-argument, object-returning code block.
@return Returns an array of the objects returned by the block.
*/
- (NSArray *)map:(BKTransformBlock)block;
/** Arbitrarily accumulate objects using a block.
The concept of this selector is difficult to illustrate in words. The sum can
be any NSObject, including (but not limited to) an NSString, NSNumber, or NSValue.
For example, you can concentate the strings in an array:
NSString *concentrated = [stringArray reduce:@"" withBlock:^id(id sum, id obj) {
return [sum stringByAppendingString:obj];
}];
You can also do something like summing the lengths of strings in an array:
NSNumber *sum = [stringArray reduce:nil withBlock:^id(id sum, id obj) {
return [NSNumber numberWithInteger: [sum integerValue] + obj.length];
}];
NSUInteger value = [sum integerValue];
@param initial The value of the reduction at its start.
@param block A block that takes the current sum and the next object to return the new sum.
@return An accumulated value.
*/
- (id)reduce:(id)initial withBlock:(BKAccumulationBlock)block;
@end