Sequence.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #import <Foundation/Foundation.h>
  2. #import "Types.h"
  3. #import "Mappable.h"
  4. #import "Option.h"
  5. #import "Enumerable.h"
  6. #import "NSArray+OCTotallyLazy.h"
  7. @interface Sequence : NSObject <NSFastEnumeration, Mappable, Enumerable>
  8. - (Sequence *)initWith:(id <Enumerable>)enumerator;
  9. - (Sequence *)add:(id)value;
  10. - (Sequence *)cons:(id)value;
  11. - (Sequence *)cycle;
  12. - (Sequence *)drop:(int)toDrop;
  13. - (Sequence *)dropWhile:(PREDICATE)funcBlock;
  14. - (Option *)find:(PREDICATE)predicate;
  15. - (id)first;
  16. - (Sequence *)flatMap:(FUNCTION1)funcBlock;
  17. - (id)filter:(PREDICATE)filterBlock;
  18. - (Sequence *)flatten;
  19. - (id)fold:(id)value with:(FUNCTION2)functorBlock;
  20. - (void)foreach:(void (^)(id))funcBlock;
  21. - (Sequence *)grouped:(int)n;
  22. - (Sequence *)groupBy:(FUNCTION1)groupingBlock;
  23. - (id)head;
  24. - (Option *)headOption;
  25. - (Sequence *)join:(id<Enumerable>)toJoin;
  26. - (Sequence *)mapWithIndex:(id (^)(id, NSInteger))func;
  27. - (Sequence *)merge:(Sequence *)toMerge;
  28. - (Pair *)partition:(PREDICATE)predicate;
  29. - (id)reduce:(id (^)(id, id))functorBlock;
  30. - (id)second;
  31. - (Pair *)splitAt:(int)splitIndex;
  32. - (Pair *)splitOn:(id)splitItem;
  33. - (Pair *)splitWhen:(PREDICATE)predicate;
  34. - (Sequence *)tail;
  35. - (Sequence *)take:(int)n;
  36. - (Sequence *)takeWhile:(PREDICATE)funcBlock;
  37. - (NSDictionary *)toDictionary:(id (^)(id))valueBlock;
  38. - (NSString *)toString;
  39. - (NSString *)toString:(NSString *)separator;
  40. - (NSString *)toString:(NSString *)start separator:(NSString *)separator end:(NSString *)end;
  41. - (Sequence *)zip:(Sequence *)otherSequence;
  42. - (Sequence *)zipWithIndex;
  43. - (NSArray *)asArray;
  44. - (NSSet *)asSet;
  45. - (NSDictionary *)asDictionary;
  46. + (Sequence *)with:(id <Enumerable>)enumerable;
  47. @end
  48. static Sequence *sequence(id items , ...) {
  49. NSMutableArray *array = [NSMutableArray array];
  50. va_list args;
  51. va_start(args, items);
  52. for (id arg = items; arg != nil; arg = va_arg(args, id)) {
  53. [array addObject:arg];
  54. }
  55. va_end(args);
  56. return [Sequence with:array];
  57. }