NSArray+FLEX.h 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // NSArray+FLEX.h
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 9/25/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface NSArray<T> (Functional)
  10. /// Actually more like flatmap, but it seems like the objc way to allow returning nil to omit objects.
  11. /// So, return nil from the block to omit objects, and return an object to include it in the new array.
  12. /// Unlike flatmap, however, this will not flatten arrays of arrays into a single array.
  13. - (__kindof NSArray *)flex_mapped:(id(^)(T obj, NSUInteger idx))mapFunc;
  14. /// Like flex_mapped, but expects arrays to be returned, and flattens them into one array.
  15. - (__kindof NSArray *)flex_flatmapped:(NSArray *(^)(id, NSUInteger idx))block;
  16. - (instancetype)flex_filtered:(BOOL(^)(T obj, NSUInteger idx))filterFunc;
  17. - (void)flex_forEach:(void(^)(T obj, NSUInteger idx))block;
  18. /// Unlike \c subArrayWithRange: this will not throw an exception if \c maxLength
  19. /// is greater than the size of the array. If the array has one element and
  20. /// \c maxLength is greater than 1, you get an array with 1 element back.
  21. - (instancetype)flex_subArrayUpto:(NSUInteger)maxLength;
  22. + (instancetype)flex_forEachUpTo:(NSUInteger)bound map:(T(^)(NSUInteger i))block;
  23. + (instancetype)flex_mapped:(id<NSFastEnumeration>)collection block:(id(^)(T obj, NSUInteger idx))mapFunc;
  24. - (instancetype)flex_sortedUsingSelector:(SEL)selector;
  25. @end