NSArray+Functional.m 884 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // NSArray+Functional.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 9/25/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "NSArray+Functional.h"
  9. @implementation NSArray (Functional)
  10. - (instancetype)flex_mapped:(id (^)(id, NSUInteger))mapFunc {
  11. NSMutableArray *map = [NSMutableArray new];
  12. [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  13. id ret = mapFunc(obj, idx);
  14. if (ret) {
  15. [map addObject:ret];
  16. }
  17. }];
  18. return map.copy;
  19. }
  20. - (NSArray *)flex_filtered:(BOOL (^)(id, NSUInteger))filterFunc {
  21. return [self flex_mapped:^id(id obj, NSUInteger idx) {
  22. return filterFunc(obj, idx) ? obj : nil;
  23. }];
  24. }
  25. - (void)flex_forEach:(id (^)(id, NSUInteger))block {
  26. [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  27. block(obj, idx);
  28. }];
  29. }
  30. @end