前段时间一直在写 swift, 最近 又来写 Objc ,发现 语法确实是差了点,表现力不足, 缺乏高阶函数, 这不 自力更生 写了一个~

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
#pragma mark - Map, filter, reduce, flatMap function without class restrictor
- (NSArray *)map:(id (^)(id obj))block {
    NSMutableArray *mutableArray = [NSMutableArray new];

    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [mutableArray addObject:block(obj)];
    }];
    return mutableArray;
}

- (NSArray *)filter:(BOOL (^)(id obj))block {
    NSMutableArray *mutableArray = [NSMutableArray new];

    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (block(obj) == YES) {
            [mutableArray addObject:obj];
        }
    }];
    return mutableArray;
}

- (id)reduce:(id)initial
       block:(id (^)(id obj1, id obj2))block {
    __block id obj = initial;

    [self enumerateObjectsUsingBlock:^(id _obj, NSUInteger idx, BOOL *stop) {
        obj = block(obj, _obj);
    }];
    return obj;
}

- (NSArray *)flatMap:(id (^)(id obj))block {
    NSMutableArray *mutableArray = [NSMutableArray new];

    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        id _obj = block(obj);
        if ([_obj isKindOfClass:[NSArray class]]) {
            NSArray *_array = [_obj flatMap:block];
            [mutableArray addObjectsFromArray:_array];
            return;
        }
        [mutableArray addObject:_obj];
    }];
    return mutableArray;
}

- (BOOL)contains:(BOOL (^)(id obj))block {
    __block BOOL contains = NO;

    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (block(obj) == YES) {
            contains = YES;
            *stop = YES;
        }
    }];
    return contains;
}

- (void)forEach:(void (^)(id obj))block {
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        block(obj);
    }];
}

下面这个是 筛选 nsarray 对象类型

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
#pragma mark - Map, filter, reduce, flatMap function with class restrictor
- (NSArray *)map:(id (^)(id obj))block
           class:(Class)aClass {
    NSMutableArray *mutableArray = [NSMutableArray new];

    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:aClass]) {
            [mutableArray addObject:block(obj)];
        }
    }];
    return mutableArray;
}

- (NSArray *)filter:(BOOL (^)(id obj))block
              class:(Class)aClass {
    NSMutableArray *mutableArray = [NSMutableArray new];

    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:aClass] &&
            block(obj) == YES) {
            [mutableArray addObject:obj];
        }
    }];
    return mutableArray;
}

- (id)reduce:(id)initial
       block:(id (^)(id obj1, id obj2))block
       class:(Class)aClass {
    __block id obj = initial;

    [self enumerateObjectsUsingBlock:^(id _obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:aClass] &&
            [_obj isKindOfClass:aClass]) {
            obj = block(obj, _obj);
        }
    }];
    return obj;
}

- (NSArray *)flatMap:(id (^)(id obj))block
               class:(Class)aClass {
    NSMutableArray *mutableArray = [NSMutableArray new];

    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        id _obj = block(obj);
        if ([_obj isKindOfClass:[NSArray class]]) {
            NSArray *_array = [_obj flatMap:block
                                      class:aClass];
            [mutableArray addObjectsFromArray:_array];
            return;
        }

        if ([_obj isKindOfClass:aClass]) {
            [mutableArray addObject:_obj];
        }
    }];
    return mutableArray;
}

- (BOOL)contains:(BOOL (^)(id obj))block
           class:(Class)aClass {
    __block BOOL contains = NO;

    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (![obj isKindOfClass:aClass]) {
            return;
        }

        if (block(obj) == YES) {
            contains = YES;
            *stop = YES;
        }
    }];
    return contains;
}

- (void)forEach:(void (^)(id obj))block
          class:(Class)aClass {
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (![obj isKindOfClass:aClass]) {
            return;
        }
        block(obj);
    }];
}