RACSequence.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // RACSequence.h
  3. // ReactiveCocoa
  4. //
  5. // Created by Justin Spahr-Summers on 2012-10-29.
  6. // Copyright (c) 2012 GitHub. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "RACStream.h"
  10. @class RACScheduler;
  11. @class RACSignal;
  12. /// Represents an immutable sequence of values. Unless otherwise specified, the
  13. /// sequences' values are evaluated lazily on demand. Like Cocoa collections,
  14. /// sequences cannot contain nil.
  15. ///
  16. /// Most inherited RACStream methods that accept a block will execute the block
  17. /// _at most_ once for each value that is evaluated in the returned sequence.
  18. /// Side effects are subject to the behavior described in
  19. /// +sequenceWithHeadBlock:tailBlock:.
  20. ///
  21. /// Implemented as a class cluster. A minimal implementation for a subclass
  22. /// consists simply of -head and -tail.
  23. @interface RACSequence : RACStream <NSCoding, NSCopying, NSFastEnumeration>
  24. /// The first object in the sequence, or nil if the sequence is empty.
  25. ///
  26. /// Subclasses must provide an implementation of this method.
  27. @property (nonatomic, strong, readonly) id head;
  28. /// All but the first object in the sequence, or nil if the sequence is empty.
  29. ///
  30. /// Subclasses must provide an implementation of this method.
  31. @property (nonatomic, strong, readonly) RACSequence *tail;
  32. /// Evaluates the full sequence to produce an equivalently-sized array.
  33. @property (nonatomic, copy, readonly) NSArray *array;
  34. /// Returns an enumerator of all objects in the sequence.
  35. @property (nonatomic, copy, readonly) NSEnumerator *objectEnumerator;
  36. /// Converts a sequence into an eager sequence.
  37. ///
  38. /// An eager sequence fully evaluates all of its values immediately. Sequences
  39. /// derived from an eager sequence will also be eager.
  40. ///
  41. /// Returns a new eager sequence, or the receiver if the sequence is already
  42. /// eager.
  43. @property (nonatomic, copy, readonly) RACSequence *eagerSequence;
  44. /// Converts a sequence into a lazy sequence.
  45. ///
  46. /// A lazy sequence evaluates its values on demand, as they are accessed.
  47. /// Sequences derived from a lazy sequence will also be lazy.
  48. ///
  49. /// Returns a new lazy sequence, or the receiver if the sequence is already lazy.
  50. @property (nonatomic, copy, readonly) RACSequence *lazySequence;
  51. /// Invokes -signalWithScheduler: with a new RACScheduler.
  52. - (RACSignal *)signal;
  53. /// Evaluates the full sequence on the given scheduler.
  54. ///
  55. /// Each item is evaluated in its own scheduled block, such that control of the
  56. /// scheduler is yielded between each value.
  57. ///
  58. /// Returns a signal which sends the receiver's values on the given scheduler as
  59. /// they're evaluated.
  60. - (RACSignal *)signalWithScheduler:(RACScheduler *)scheduler;
  61. /// Applies a left fold to the sequence.
  62. ///
  63. /// This is the same as iterating the sequence along with a provided start value.
  64. /// This uses a constant amount of memory. A left fold is left-associative so in
  65. /// the sequence [1,2,3] the block would applied in the following order:
  66. /// reduce(reduce(reduce(start, 1), 2), 3)
  67. ///
  68. /// start - The starting value for the fold. Used as `accumulator` for the
  69. /// first fold.
  70. /// reduce - The block used to combine the accumulated value and the next value.
  71. /// Cannot be nil.
  72. ///
  73. /// Returns a reduced value.
  74. - (id)foldLeftWithStart:(id)start reduce:(id (^)(id accumulator, id value))reduce;
  75. /// Applies a right fold to the sequence.
  76. ///
  77. /// A right fold is equivalent to recursion on the list. The block is evaluated
  78. /// from the right to the left in list. It is right associative so it's applied
  79. /// to the rightmost elements first. For example, in the sequence [1,2,3] the
  80. /// block is applied in the order:
  81. /// reduce(1, reduce(2, reduce(3, start)))
  82. ///
  83. /// start - The starting value for the fold.
  84. /// reduce - The block used to combine the accumulated value and the next head.
  85. /// The block is given the accumulated value and the value of the rest
  86. /// of the computation (result of the recursion). This is computed when
  87. /// you retrieve its value using `rest.head`. This allows you to
  88. /// prevent unnecessary computation by not accessing `rest.head` if you
  89. /// don't need to.
  90. ///
  91. /// Returns a reduced value.
  92. - (id)foldRightWithStart:(id)start reduce:(id (^)(id first, RACSequence *rest))reduce;
  93. /// Check if any value in sequence passes the block.
  94. ///
  95. /// block - The block predicate used to check each item. Cannot be nil.
  96. ///
  97. /// Returns a boolean indiciating if any value in the sequence passed.
  98. - (BOOL)any:(BOOL (^)(id value))block;
  99. /// Check if all values in the sequence pass the block.
  100. ///
  101. /// block - The block predicate used to check each item. Cannot be nil.
  102. ///
  103. /// Returns a boolean indicating if all values in the sequence passed.
  104. - (BOOL)all:(BOOL (^)(id value))block;
  105. /// Returns the first object that passes the block.
  106. ///
  107. /// block - The block predicate used to check each item. Cannot be nil.
  108. ///
  109. /// Returns an object that passes the block or nil if no objects passed.
  110. - (id)objectPassingTest:(BOOL (^)(id value))block;
  111. /// Creates a sequence that dynamically generates its values.
  112. ///
  113. /// headBlock - Invoked the first time -head is accessed.
  114. /// tailBlock - Invoked the first time -tail is accessed.
  115. ///
  116. /// The results from each block are memoized, so each block will be invoked at
  117. /// most once, no matter how many times the head and tail properties of the
  118. /// sequence are accessed.
  119. ///
  120. /// Any side effects in `headBlock` or `tailBlock` should be thread-safe, since
  121. /// the sequence may be evaluated at any time from any thread. Not only that, but
  122. /// -tail may be accessed before -head, or both may be accessed simultaneously.
  123. /// As noted above, side effects will only be triggered the _first_ time -head or
  124. /// -tail is invoked.
  125. ///
  126. /// Returns a sequence that lazily invokes the given blocks to provide head and
  127. /// tail. `headBlock` must not be nil.
  128. + (RACSequence *)sequenceWithHeadBlock:(id (^)(void))headBlock tailBlock:(RACSequence *(^)(void))tailBlock;
  129. @end
  130. @interface RACSequence (Unavailable)
  131. - (id)foldLeftWithStart:(id)start combine:(id (^)(id accumulator, id value))combine __attribute__((unavailable("Renamed to -foldLeftWithStart:reduce:")));
  132. - (id)foldRightWithStart:(id)start combine:(id (^)(id first, RACSequence *rest))combine __attribute__((unavailable("Renamed to -foldRightWithStart:reduce:")));
  133. @end