-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
NSArray+reduce.h
31 lines (22 loc) · 903 Bytes
/
NSArray+reduce.h
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
#import <Foundation/NSArray.h>
@interface NSArray (YOLOReduce)
/**
Reduces the receiver to a single value.
The usual example of reduce is to sum all values in an array.
id rv = @[@1, @2, @3, @4].reduce(^(NSNumber *memo, NSNumber *obj){
return @(memo.intValue + obj.intValue);
});
// rv => @10
Generally `-inject` is more useful. Though, when appropriate, `reduce`
is more elegant.
// find the longest word
id longest = @[@"cat", @"sheep", @"bear"].reduce(^(NSString *memo, NSString *word){
return memo.length > word.length ? memo : word;
});
// longest => @"sheep"
@param memo initialized to the first value in receiver, but on subsequent iterations is the value returned from the given block.
@param obj the value for each element in the receiver, though never the first object.
@see -inject
*/
- (id(^)(id (^)(id memo, id obj)))reduce;
@end