RACEagerSequence.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // RACEagerSequence.m
  3. // ReactiveObjC
  4. //
  5. // Created by Uri Baghin on 02/01/2013.
  6. // Copyright (c) 2013 GitHub, Inc. All rights reserved.
  7. //
  8. #import "RACEagerSequence.h"
  9. #import "NSObject+RACDescription.h"
  10. #import "RACArraySequence.h"
  11. @implementation RACEagerSequence
  12. #pragma mark RACStream
  13. + (RACSequence *)return:(id)value {
  14. return [[self sequenceWithArray:@[ value ] offset:0] setNameWithFormat:@"+return: %@", RACDescription(value)];
  15. }
  16. - (RACSequence *)bind:(RACSequenceBindBlock (^)(void))block {
  17. NSCParameterAssert(block != nil);
  18. RACStreamBindBlock bindBlock = block();
  19. NSArray *currentArray = self.array;
  20. NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:currentArray.count];
  21. for (id value in currentArray) {
  22. BOOL stop = NO;
  23. RACSequence *boundValue = (id)bindBlock(value, &stop);
  24. if (boundValue == nil) break;
  25. for (id x in boundValue) {
  26. [resultArray addObject:x];
  27. }
  28. if (stop) break;
  29. }
  30. return [[self.class sequenceWithArray:resultArray offset:0] setNameWithFormat:@"[%@] -bind:", self.name];
  31. }
  32. - (RACSequence *)concat:(RACSequence *)sequence {
  33. NSCParameterAssert(sequence != nil);
  34. NSCParameterAssert([sequence isKindOfClass:RACSequence.class]);
  35. NSArray *array = [self.array arrayByAddingObjectsFromArray:sequence.array];
  36. return [[self.class sequenceWithArray:array offset:0] setNameWithFormat:@"[%@] -concat: %@", self.name, sequence];
  37. }
  38. #pragma mark Extended methods
  39. - (RACSequence *)eagerSequence {
  40. return self;
  41. }
  42. - (RACSequence *)lazySequence {
  43. return [RACArraySequence sequenceWithArray:self.array offset:0];
  44. }
  45. - (id)foldRightWithStart:(id)start reduce:(id (^)(id, RACSequence *rest))reduce {
  46. return [super foldRightWithStart:start reduce:^(id first, RACSequence *rest) {
  47. return reduce(first, rest.eagerSequence);
  48. }];
  49. }
  50. @end