/BlocksKit/NSIndexSet+BlocksKit.h

http://github.com/zwaldowski/BlocksKit · C Header · 76 lines · 9 code · 9 blank · 58 comment · 0 complexity · 82dc530b8a118a6dfabd96860f3a2b1c MD5 · raw file

  1. //
  2. // NSIndexSet+BlocksKit.h
  3. // %PROJECT
  4. //
  5. #import "BKGlobals.h"
  6. /** Block extensions for NSIndexSet.
  7. Both inspired by and resembling Smalltalk syntax, these utilities
  8. allows for iteration of an array in a concise way that
  9. saves quite a bit of boilerplate code for filtering or finding
  10. objects or an object.
  11. Includes code by the following:
  12. - Robin Lu. <https://github.com/robin>. 2009. MIT.
  13. - Michael Ash. <https://github.com/mikeash>. 2010. BSD.
  14. - Zach Waldowski. <https://github.com/zwaldowski>. 2011. MIT.
  15. @see NSArray(BlocksKit)
  16. @see NSDictionary(BlocksKit)
  17. @see NSSet(BlocksKit)
  18. */
  19. @interface NSIndexSet (BlocksKit)
  20. /** Loops through an index set and executes the given block at each index.
  21. @param block A single-argument, void-returning code block.
  22. */
  23. - (void)each:(BKIndexBlock)block;
  24. /** Enumerates each index in an index set concurrently and executes the
  25. given block once per index.
  26. Enumeration will occur on appropriate background queues.
  27. Be aware that the block will not necessarily be executed
  28. in order for each index.
  29. @param block A single-argument, void-returning code block.
  30. */
  31. - (void)apply:(BKIndexBlock)block;
  32. /** Loops through an array and returns the index matching the block.
  33. @param block A single-argument, `BOOL`-returning code block.
  34. @return Returns the index if found, `NSNotFound` otherwise.
  35. @see select:
  36. */
  37. - (NSUInteger)match:(BKIndexValidationBlock)block;
  38. /** Loops through an index set and returns an all indexes matching the block.
  39. @param block A single-argument, BOOL-returning code block.
  40. @return Returns an index set of matching indexes found, `nil` otherwise.
  41. @see match:
  42. */
  43. - (NSIndexSet *)select:(BKIndexValidationBlock)block;
  44. /** Loops through an index set and returns an all indexes but the ones matching the block.
  45. This selector performs *literally* the exact same function as select: but in reverse.
  46. @param block A single-argument, BOOL-returning code block.
  47. @return Returns an index set of all indexes but those matched, `nil` if all are excluded.
  48. */
  49. - (NSIndexSet *)reject:(BKIndexValidationBlock)block;
  50. /** Call the block once for each index and create an index set with the new values.
  51. @param block A block that returns a new index for an index.
  52. @return An index set of the indexes returned by the block.
  53. */
  54. - (NSIndexSet *)map:(BKIndexTransformBlock)block;
  55. @end