RACSequence.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // RACSequence.h
  3. // ReactiveObjC
  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<__covariant ValueType>;
  12. NS_ASSUME_NONNULL_BEGIN
  13. /// Represents an immutable sequence of values. Unless otherwise specified, the
  14. /// sequences' values are evaluated lazily on demand. Like Cocoa collections,
  15. /// sequences cannot contain nil.
  16. ///
  17. /// Most inherited RACStream methods that accept a block will execute the block
  18. /// _at most_ once for each value that is evaluated in the returned sequence.
  19. /// Side effects are subject to the behavior described in
  20. /// +sequenceWithHeadBlock:tailBlock:.
  21. ///
  22. /// Implemented as a class cluster. A minimal implementation for a subclass
  23. /// consists simply of -head and -tail.
  24. @interface RACSequence<__covariant ValueType> : RACStream <NSCoding, NSCopying, NSFastEnumeration>
  25. /// The first object in the sequence, or nil if the sequence is empty.
  26. ///
  27. /// Subclasses must provide an implementation of this method.
  28. @property (nonatomic, strong, readonly, nullable) ValueType head;
  29. /// All but the first object in the sequence, or nil if there are no other
  30. /// objects.
  31. ///
  32. /// Subclasses must provide an implementation of this method.
  33. @property (nonatomic, strong, readonly, nullable) RACSequence<ValueType> *tail;
  34. /// Evaluates the full sequence to produce an equivalently-sized array.
  35. @property (nonatomic, copy, readonly) NSArray<ValueType> *array;
  36. /// Returns an enumerator of all objects in the sequence.
  37. @property (nonatomic, copy, readonly) NSEnumerator<ValueType> *objectEnumerator;
  38. /// Converts a sequence into an eager sequence.
  39. ///
  40. /// An eager sequence fully evaluates all of its values immediately. Sequences
  41. /// derived from an eager sequence will also be eager.
  42. ///
  43. /// Returns a new eager sequence, or the receiver if the sequence is already
  44. /// eager.
  45. @property (nonatomic, copy, readonly) RACSequence<ValueType> *eagerSequence;
  46. /// Converts a sequence into a lazy sequence.
  47. ///
  48. /// A lazy sequence evaluates its values on demand, as they are accessed.
  49. /// Sequences derived from a lazy sequence will also be lazy.
  50. ///
  51. /// Returns a new lazy sequence, or the receiver if the sequence is already lazy.
  52. @property (nonatomic, copy, readonly) RACSequence<ValueType> *lazySequence;
  53. /// Invokes -signalWithScheduler: with a new RACScheduler.
  54. - (RACSignal<ValueType> *)signal;
  55. /// Evaluates the full sequence on the given scheduler.
  56. ///
  57. /// Each item is evaluated in its own scheduled block, such that control of the
  58. /// scheduler is yielded between each value.
  59. ///
  60. /// Returns a signal which sends the receiver's values on the given scheduler as
  61. /// they're evaluated.
  62. - (RACSignal<ValueType> *)signalWithScheduler:(RACScheduler *)scheduler;
  63. /// Applies a left fold to the sequence.
  64. ///
  65. /// This is the same as iterating the sequence along with a provided start value.
  66. /// This uses a constant amount of memory. A left fold is left-associative so in
  67. /// the sequence [1,2,3] the block would applied in the following order:
  68. /// reduce(reduce(reduce(start, 1), 2), 3)
  69. ///
  70. /// start - The starting value for the fold. Used as `accumulator` for the
  71. /// first fold.
  72. /// reduce - The block used to combine the accumulated value and the next value.
  73. /// Cannot be nil.
  74. ///
  75. /// Returns a reduced value.
  76. - (id)foldLeftWithStart:(nullable id)start reduce:(id _Nullable (^)(id _Nullable accumulator, ValueType _Nullable value))reduce;
  77. /// Applies a right fold to the sequence.
  78. ///
  79. /// A right fold is equivalent to recursion on the list. The block is evaluated
  80. /// from the right to the left in list. It is right associative so it's applied
  81. /// to the rightmost elements first. For example, in the sequence [1,2,3] the
  82. /// block is applied in the order:
  83. /// reduce(1, reduce(2, reduce(3, start)))
  84. ///
  85. /// start - The starting value for the fold.
  86. /// reduce - The block used to combine the accumulated value and the next head.
  87. /// The block is given the accumulated value and the value of the rest
  88. /// of the computation (result of the recursion). This is computed when
  89. /// you retrieve its value using `rest.head`. This allows you to
  90. /// prevent unnecessary computation by not accessing `rest.head` if you
  91. /// don't need to.
  92. ///
  93. /// Returns a reduced value.
  94. - (id)foldRightWithStart:(nullable id)start reduce:(id _Nullable (^)(id _Nullable first, RACSequence *rest))reduce;
  95. /// Check if any value in sequence passes the block.
  96. ///
  97. /// block - The block predicate used to check each item. Cannot be nil.
  98. ///
  99. /// Returns a boolean indiciating if any value in the sequence passed.
  100. - (BOOL)any:(BOOL (^)(ValueType _Nullable value))block;
  101. /// Check if all values in the sequence pass the block.
  102. ///
  103. /// block - The block predicate used to check each item. Cannot be nil.
  104. ///
  105. /// Returns a boolean indicating if all values in the sequence passed.
  106. - (BOOL)all:(BOOL (^)(ValueType _Nullable value))block;
  107. /// Returns the first object that passes the block.
  108. ///
  109. /// block - The block predicate used to check each item. Cannot be nil.
  110. ///
  111. /// Returns an object that passes the block or nil if no objects passed.
  112. - (nullable ValueType)objectPassingTest:(BOOL (^)(ValueType _Nullable value))block;
  113. /// Creates a sequence that dynamically generates its values.
  114. ///
  115. /// headBlock - Invoked the first time -head is accessed.
  116. /// tailBlock - Invoked the first time -tail is accessed.
  117. ///
  118. /// The results from each block are memoized, so each block will be invoked at
  119. /// most once, no matter how many times the head and tail properties of the
  120. /// sequence are accessed.
  121. ///
  122. /// Any side effects in `headBlock` or `tailBlock` should be thread-safe, since
  123. /// the sequence may be evaluated at any time from any thread. Not only that, but
  124. /// -tail may be accessed before -head, or both may be accessed simultaneously.
  125. /// As noted above, side effects will only be triggered the _first_ time -head or
  126. /// -tail is invoked.
  127. ///
  128. /// Returns a sequence that lazily invokes the given blocks to provide head and
  129. /// tail. `headBlock` must not be nil.
  130. + (RACSequence<ValueType> *)sequenceWithHeadBlock:(ValueType _Nullable (^)(void))headBlock tailBlock:(nullable RACSequence<ValueType> *(^)(void))tailBlock;
  131. @end
  132. @interface RACSequence<__covariant ValueType> (RACStream)
  133. /// Returns a sequence that immediately sends the given value and then completes.
  134. + (RACSequence<ValueType> *)return:(nullable ValueType)value;
  135. /// Returns a sequence that immediately completes.
  136. + (RACSequence<ValueType> *)empty;
  137. /// A block which accepts a value from a RACSequence and returns a new sequence.
  138. ///
  139. /// Setting `stop` to `YES` will cause the bind to terminate after the returned
  140. /// value. Returning `nil` will result in immediate termination.
  141. typedef RACSequence * _Nullable (^RACSequenceBindBlock)(ValueType _Nullable value, BOOL *stop);
  142. /// Lazily binds a block to the values in the receiver.
  143. ///
  144. /// This should only be used if you need to terminate the bind early, or close
  145. /// over some state. -flattenMap: is more appropriate for all other cases.
  146. ///
  147. /// block - A block returning a RACSequenceBindBlock. This block will be invoked
  148. /// each time the bound sequence is re-evaluated. This block must not be
  149. /// nil or return nil.
  150. ///
  151. /// Returns a new sequence which represents the combined result of all lazy
  152. /// applications of `block`.
  153. - (RACSequence *)bind:(RACSequenceBindBlock (^)(void))block;
  154. /// Subscribes to `sequence` when the source sequence completes.
  155. - (RACSequence *)concat:(RACSequence *)sequence;
  156. /// Zips the values in the receiver with those of the given sequence to create
  157. /// RACTuples.
  158. ///
  159. /// The first `next` of each sequence will be combined, then the second `next`,
  160. /// and so forth, until either sequence completes or errors.
  161. ///
  162. /// sequence - The sequence to zip with. This must not be `nil`.
  163. ///
  164. /// Returns a new sequence of RACTuples, representing the combined values of the
  165. /// two sequences. Any error from one of the original sequence will be forwarded
  166. /// on the returned sequence.
  167. - (RACSequence *)zipWith:(RACSequence *)sequence;
  168. @end
  169. /// Redeclarations of operations built on the RACStream primitives with more
  170. /// precise type information.
  171. @interface RACSequence<__covariant ValueType> (RACStreamOperations)
  172. /// Maps `block` across the values in the receiver and flattens the result.
  173. ///
  174. /// Note that operators applied _after_ -flattenMap: behave differently from
  175. /// operators _within_ -flattenMap:. See the Examples section below.
  176. ///
  177. /// This corresponds to the `SelectMany` method in Rx.
  178. ///
  179. /// block - A block which accepts the values in the receiver and returns a new
  180. /// instance of the receiver's class. Returning `nil` from this block is
  181. /// equivalent to returning an empty sequence.
  182. ///
  183. /// Returns a new sequence which represents the combined sequences resulting
  184. /// from mapping `block`.
  185. - (RACSequence *)flattenMap:(__kindof RACSequence * _Nullable (^)(ValueType _Nullable value))block;
  186. /// Flattens a sequence of sequences.
  187. ///
  188. /// This corresponds to the `Merge` method in Rx.
  189. ///
  190. /// Returns a sequence consisting of the combined sequences obtained from the
  191. /// receiver.
  192. - (RACSequence *)flatten;
  193. /// Maps `block` across the values in the receiver.
  194. ///
  195. /// This corresponds to the `Select` method in Rx.
  196. ///
  197. /// Returns a new sequence with the mapped values.
  198. - (RACSequence *)map:(id _Nullable (^)(ValueType _Nullable value))block;
  199. /// Replaces each value in the receiver with the given object.
  200. ///
  201. /// Returns a new sequence which includes the given object once for each value in
  202. /// the receiver.
  203. - (RACSequence *)mapReplace:(nullable id)object;
  204. /// Filters out values in the receiver that don't pass the given test.
  205. ///
  206. /// This corresponds to the `Where` method in Rx.
  207. ///
  208. /// Returns a new sequence with only those values that passed.
  209. - (RACSequence<ValueType> *)filter:(BOOL (^)(id _Nullable value))block;
  210. /// Filters out values in the receiver that equal (via -isEqual:) the provided
  211. /// value.
  212. ///
  213. /// value - The value can be `nil`, in which case it ignores `nil` values.
  214. ///
  215. /// Returns a new sequence containing only the values which did not compare
  216. /// equal to `value`.
  217. - (RACSequence *)ignore:(nullable ValueType)value;
  218. /// Unpacks each RACTuple in the receiver and maps the values to a new value.
  219. ///
  220. /// reduceBlock - The block which reduces each RACTuple's values into one value.
  221. /// It must take as many arguments as the number of tuple elements
  222. /// to process. Each argument will be an object argument. The
  223. /// return value must be an object. This argument cannot be nil.
  224. ///
  225. /// Returns a new sequence of reduced tuple values.
  226. - (RACSequence *)reduceEach:(id _Nullable (^)())reduceBlock;
  227. /// Returns a sequence consisting of `value`, followed by the values in the
  228. /// receiver.
  229. - (RACSequence<ValueType> *)startWith:(nullable ValueType)value;
  230. /// Skips the first `skipCount` values in the receiver.
  231. ///
  232. /// Returns the receiver after skipping the first `skipCount` values. If
  233. /// `skipCount` is greater than the number of values in the sequence, an empty
  234. /// sequence is returned.
  235. - (RACSequence<ValueType> *)skip:(NSUInteger)skipCount;
  236. /// Returns a sequence of the first `count` values in the receiver. If `count` is
  237. /// greater than or equal to the number of values in the sequence, a sequence
  238. /// equivalent to the receiver is returned.
  239. - (RACSequence<ValueType> *)take:(NSUInteger)count;
  240. /// Zips the values in the given sequences to create RACTuples.
  241. ///
  242. /// The first value of each sequence will be combined, then the second value,
  243. /// and so forth, until at least one of the sequences is exhausted.
  244. ///
  245. /// sequences - The sequence to combine. If this collection is empty, the
  246. /// returned sequence will be empty.
  247. ///
  248. /// Returns a new sequence containing RACTuples of the zipped values from the
  249. /// sequences.
  250. + (RACSequence<ValueType> *)zip:(id<NSFastEnumeration>)sequence;
  251. /// Zips sequences using +zip:, then reduces the resulting tuples into a single
  252. /// value using -reduceEach:
  253. ///
  254. /// sequences - The sequences to combine. If this collection is empty, the
  255. /// returned sequence will be empty.
  256. /// reduceBlock - The block which reduces the values from all the sequences
  257. /// into one value. It must take as many arguments as the
  258. /// number of sequences given. Each argument will be an object
  259. /// argument. The return value must be an object. This argument
  260. /// must not be nil.
  261. ///
  262. /// Example:
  263. ///
  264. /// [RACSequence zip:@[ stringSequence, intSequence ]
  265. /// reduce:^(NSString *string, NSNumber *number) {
  266. /// return [NSString stringWithFormat:@"%@: %@", string, number];
  267. /// }];
  268. ///
  269. /// Returns a new sequence containing the results from each invocation of
  270. /// `reduceBlock`.
  271. + (RACSequence<ValueType> *)zip:(id<NSFastEnumeration>)sequences reduce:(id _Nullable (^)())reduceBlock;
  272. /// Returns a sequence obtained by concatenating `sequences` in order.
  273. + (RACSequence<ValueType> *)concat:(id<NSFastEnumeration>)sequences;
  274. /// Combines values in the receiver from left to right using the given block.
  275. ///
  276. /// The algorithm proceeds as follows:
  277. ///
  278. /// 1. `startingValue` is passed into the block as the `running` value, and the
  279. /// first element of the receiver is passed into the block as the `next` value.
  280. /// 2. The result of the invocation is added to the returned sequence.
  281. /// 3. The result of the invocation (`running`) and the next element of the
  282. /// receiver (`next`) is passed into `block`.
  283. /// 4. Steps 2 and 3 are repeated until all values have been processed.
  284. ///
  285. /// startingValue - The value to be combined with the first element of the
  286. /// receiver. This value may be `nil`.
  287. /// reduceBlock - The block that describes how to combine values of the
  288. /// receiver. If the receiver is empty, this block will never be
  289. /// invoked. Cannot be nil.
  290. ///
  291. /// Examples
  292. ///
  293. /// RACSequence *numbers = @[ @1, @2, @3, @4 ].rac_sequence;
  294. ///
  295. /// // Contains 1, 3, 6, 10
  296. /// RACSequence *sums = [numbers scanWithStart:@0 reduce:^(NSNumber *sum, NSNumber *next) {
  297. /// return @(sum.integerValue + next.integerValue);
  298. /// }];
  299. ///
  300. /// Returns a new sequence that consists of each application of `reduceBlock`. If
  301. /// the receiver is empty, an empty sequence is returned.
  302. - (RACSequence *)scanWithStart:(nullable id)startingValue reduce:(id _Nullable (^)(id _Nullable running, ValueType _Nullable next))reduceBlock;
  303. /// Combines values in the receiver from left to right using the given block
  304. /// which also takes zero-based index of the values.
  305. ///
  306. /// startingValue - The value to be combined with the first element of the
  307. /// receiver. This value may be `nil`.
  308. /// reduceBlock - The block that describes how to combine values of the
  309. /// receiver. This block takes zero-based index value as the last
  310. /// parameter. If the receiver is empty, this block will never
  311. /// be invoked. Cannot be nil.
  312. ///
  313. /// Returns a new sequence that consists of each application of `reduceBlock`.
  314. /// If the receiver is empty, an empty sequence is returned.
  315. - (RACSequence *)scanWithStart:(nullable id)startingValue reduceWithIndex:(id _Nullable (^)(id _Nullable running, ValueType _Nullable next, NSUInteger index))reduceBlock;
  316. /// Combines each previous and current value into one object.
  317. ///
  318. /// This method is similar to -scanWithStart:reduce:, but only ever operates on
  319. /// the previous and current values (instead of the whole sequence), and does
  320. /// not pass the return value of `reduceBlock` into the next invocation of it.
  321. ///
  322. /// start - The value passed into `reduceBlock` as `previous` for the
  323. /// first value.
  324. /// reduceBlock - The block that combines the previous value and the current
  325. /// value to create the reduced value. Cannot be nil.
  326. ///
  327. /// Examples
  328. ///
  329. /// RACSequence *numbers = [@[ @1, @2, @3, @4 ].rac_sequence;
  330. ///
  331. /// // Contains 1, 3, 5, 7
  332. /// RACSequence *sums = [numbers combinePreviousWithStart:@0 reduce:^(NSNumber *previous, NSNumber *next) {
  333. /// return @(previous.integerValue + next.integerValue);
  334. /// }];
  335. ///
  336. /// Returns a new sequence consisting of the return values from each application of
  337. /// `reduceBlock`.
  338. - (RACSequence *)combinePreviousWithStart:(nullable ValueType)start reduce:(id _Nullable (^)(ValueType _Nullable previous, ValueType _Nullable current))reduceBlock;
  339. /// Takes values until the given block returns `YES`.
  340. ///
  341. /// Returns a RACSequence of the initial values in the receiver that fail
  342. /// `predicate`. If `predicate` never returns `YES`, a sequence equivalent to
  343. /// the receiver is returned.
  344. - (RACSequence<ValueType> *)takeUntilBlock:(BOOL (^)(ValueType _Nullable x))predicate;
  345. /// Takes values until the given block returns `NO`.
  346. ///
  347. /// Returns a sequence of the initial values in the receiver that pass
  348. /// `predicate`. If `predicate` never returns `NO`, a sequence equivalent to the
  349. /// receiver is returned.
  350. - (RACSequence<ValueType> *)takeWhileBlock:(BOOL (^)(ValueType _Nullable x))predicate;
  351. /// Skips values until the given block returns `YES`.
  352. ///
  353. /// Returns a sequence containing the values of the receiver that follow any
  354. /// initial values failing `predicate`. If `predicate` never returns `YES`,
  355. /// an empty sequence is returned.
  356. - (RACSequence<ValueType> *)skipUntilBlock:(BOOL (^)(ValueType _Nullable x))predicate;
  357. /// Skips values until the given block returns `NO`.
  358. ///
  359. /// Returns a sequence containing the values of the receiver that follow any
  360. /// initial values passing `predicate`. If `predicate` never returns `NO`, an
  361. /// empty sequence is returned.
  362. - (RACSequence<ValueType> *)skipWhileBlock:(BOOL (^)(ValueType _Nullable x))predicate;
  363. /// Returns a sequence of values for which -isEqual: returns NO when compared to
  364. /// the previous value.
  365. - (RACSequence<ValueType> *)distinctUntilChanged;
  366. @end
  367. NS_ASSUME_NONNULL_END