//
//  NSArray+GM.m
//  Gengmei
//
//  Created by wangyang on 2018/4/9.
//  Copyright © 2018年 更美互动信息科技有限公司. All rights reserved.
//

#import "NSArray+GM.h"


@implementation NSArray (GM)
- (NSArray *)arrayFromDeleteCondition:(BOOL (^)(id object))compare {
    NSMutableArray *array;
    if ([self isMemberOfClass:[NSMutableArray class]]) {
        array = (NSMutableArray *)self;
    } else {
        array = [self mutableCopy];
    }
    NSInteger deleteIndex = -1;
    for (int i = 0; i < array.count; i++) {
        if (compare(array[i])) {
            deleteIndex = i;
            break;
        }
    }
    if (deleteIndex != -1) {
        [array removeObjectAtIndex:deleteIndex];
    }
    return array;
}

// 对于不想添加到结果中的情况，就返回 nil
- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block {
    NSMutableArray *result = [NSMutableArray array];
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        id value = block(obj, idx);
        if (value) {
            [result addObject:value];
        }
    }];
    return result;
}
@end


@implementation NSMutableArray (GM)
- (void)deleteCondition:(BOOL (^)(id object))compare {
    NSInteger deleteIndex = -1;
    for (int i = 0; i < self.count; i++) {
        if (compare(self[i])) {
            deleteIndex = i;
            break;
        }
    }
    if (deleteIndex != -1) {
        [self removeObjectAtIndex:deleteIndex];
    }
}
@end
