RACStream Class Reference
| Inherits from | NSObject |
| Declared in | RACStream.h RACStream.m |
Overview
An abstract class representing any stream of values.
This class represents a monad, upon which many stream-based operations can be built.
When subclassing RACStream, only the methods in the main @interface body need to be overridden. Operations built on the RACStream primitives.
These methods do not need to be overridden, although subclasses may occasionally gain better performance from doing so. This extension contains functionality to support naming streams for debugging.
Subclasses do not need to override the methods here.
Tasks
Other Methods
Other Methods
Operations Methods
-
– flattenMap: -
– flatten -
– map: -
– mapReplace: -
– filter: -
– ignore: -
– reduceEach: -
– startWith: -
– skip: -
– take: -
+ zip: -
+ zip:reduce: -
+ concat: -
– scanWithStart:reduce: -
– combinePreviousWithStart:reduce: -
– takeUntilBlock: -
– takeWhileBlock: -
– skipUntilBlock: -
– skipWhileBlock: -
– distinctUntilChanged -
+ join:block:
Extension Methods
-
nameproperty
Class Methods
concat:
Returns a stream obtained by concatenating streams in order.
+ (instancetype)concat:(id<NSFastEnumeration>)streamsDeclared In
RACStream.hjoin:block:
Combines a list of streams using the logic of the given block.
+ (instancetype)join:(id<NSFastEnumeration>)streams block:(RACStream *( ^ ) ( id , id ))blockDiscussion
streams - The streams to combine. block - An operator that combines two streams and returns a new one. The returned stream should contain 2-tuples of the streams' combined values.
Returns a combined stream.
Declared In
RACStream+Private.hreturn:
Lifts value into the stream monad.
+ (instancetype)return:(id)valueDiscussion
Returns a stream containing only the given value.
Declared In
RACStream.hzip:
Zips the values in the given streams to create RACTuples.
+ (instancetype)zip:(id<NSFastEnumeration>)streamsDiscussion
The first value of each stream will be combined, then the second value, and so forth, until at least one of the streams is exhausted.
streams - The streams to combine. These must all be instances of the same concrete class implementing the protocol. If this collection is empty, the returned stream will be empty.
Returns a new stream containing RACTuples of the zipped values from the streams.
Declared In
RACStream.hzip:reduce:
Zips streams using zip:, then reduces the resulting tuples into a single value using reduceEach:
+ (instancetype)zip:(id<NSFastEnumeration>)streams reduce:(id ( ^ ) ( ))reduceBlockDiscussion
streams - The streams to combine. These must all be instances of the same concrete class implementing the protocol. If this collection is empty, the returned stream will be empty. reduceBlock - The block which reduces the values from all the streams into one value. It must take as many arguments as the number of streams given. Each argument will be an object argument. The return value must be an object. This argument must not be nil.
Example:
[RACStream zip:@[ stringSignal, intSignal ] reduce:^(NSString string, NSNumber number) { return [NSString stringWithFormat:@“%@: %@”, string, number]; }];
Returns a new stream containing the results from each invocation of
reduceBlock.
Declared In
RACStream.hInstance Methods
bind:
Lazily binds a block to the values in the receiver.
- (instancetype)bind:(RACStreamBindBlock ( ^ ) ( void ))blockDiscussion
This should only be used if you need to terminate the bind early, or close over some state. flattenMap: is more appropriate for all other cases.
block - A block returning a RACStreamBindBlock. This block will be invoked each time the bound stream is re-evaluated. This block must not be nil or return nil.
Returns a new stream which represents the combined result of all lazy
applications of block.
Declared In
RACStream.hcombinePreviousWithStart:reduce:
Combines each previous and current value into one object.
- (instancetype)combinePreviousWithStart:(id)start reduce:(id ( ^ ) ( id previous , id current ))reduceBlockDiscussion
This method is similar to scanWithStart:reduce:, but only ever operates on
the previous and current values (instead of the whole stream), and does not
pass the return value of reduceBlock into the next invocation of it.
start - The value passed into reduceBlock as previous for the
first value.
reduceBlock - The block that combines the previous value and the current
value to create the reduced value. Cannot be nil.
Examples
RACSequence *numbers = @[ @1, @2, @3, @4 ].rac_sequence;
// Contains 1, 3, 5, 7
RACSequence *sums = [numbers combinePreviousWithStart:@0 reduce:^(NSNumber *previous, NSNumber *next) {
return @(previous.integerValue + next.integerValue);
}];
Returns a new stream consisting of the return values from each application of
reduceBlock.
Declared In
RACStream.hconcat:
Appends the values of stream to the values in the receiver.
- (instancetype)concat:(RACStream *)streamDiscussion
stream - A stream to concatenate. This must be an instance of the same
concrete class as the receiver, and should not be nil.
Returns a new stream representing the receiver followed by stream.
Declared In
RACStream.hdistinctUntilChanged
Returns a stream of values for which -isEqual: returns NO when compared to the previous value.
- (instancetype)distinctUntilChangedDeclared In
RACStream.hfilter:
Filters out values in the receiver that don’t pass the given test.
- (instancetype)filter:(BOOL ( ^ ) ( id value ))blockDiscussion
This corresponds to the Where method in Rx.
Returns a new stream with only those values that passed.
Declared In
RACStream.hflatten
Flattens a stream of streams.
- (instancetype)flattenDiscussion
This corresponds to the Merge method in Rx.
Returns a stream consisting of the combined streams obtained from the receiver.
Declared In
RACStream.hflattenMap:
Maps block across the values in the receiver and flattens the result.
- (instancetype)flattenMap:(RACStream *( ^ ) ( id value ))blockDiscussion
Note that operators applied after -flattenMap: behave differently from operators within -flattenMap:. See the Examples section below.
This corresponds to the SelectMany method in Rx.
block - A block which accepts the values in the receiver and returns a new
instance of the receiver’s class. Returning nil from this block is
equivalent to returning an empty signal.
Examples
[signal flattenMap:^(id x) { // Logs each time a returned signal completes. return [[RACSignal return:x] logCompleted]; }];
[[signal flattenMap:^(id x) { return [RACSignal return:x]; }] // Logs only once, when all of the signals complete. logCompleted];
Returns a new stream which represents the combined streams resulting from
mapping block.
Declared In
RACStream.hignore:
Filters out values in the receiver that equal (via -isEqual:) the provided value.
- (instancetype)ignore:(id)valueDiscussion
value - The value can be nil, in which case it ignores nil values.
Returns a new stream containing only the values which did not compare equal
to value.
Declared In
RACStream.hmap:
Maps block across the values in the receiver.
- (instancetype)map:(id ( ^ ) ( id value ))blockDiscussion
This corresponds to the Select method in Rx.
Returns a new stream with the mapped values.
Declared In
RACStream.hmapReplace:
Replace each value in the receiver with the given object.
- (instancetype)mapReplace:(id)objectDiscussion
Returns a new stream which includes the given object once for each value in the receiver.
Declared In
RACStream.hreduceEach:
Unpacks each RACTuple in the receiver and maps the values to a new value.
- (instancetype)reduceEach:(id ( ^ ) ( ))reduceBlockDiscussion
reduceBlock - The block which reduces each RACTuple’s values into one value. It must take as many arguments as the number of tuple elements to process. Each argument will be an object argument. The return value must be an object. This argument cannot be nil.
Returns a new stream of reduced tuple values.
Declared In
RACStream.hscanWithStart:reduce:
Combines values in the receiver from left to right using the given block.
- (instancetype)scanWithStart:(id)startingValue reduce:(id ( ^ ) ( id running , id next ))blockDiscussion
The algorithm proceeds as follows:
-
startingValueis passed into the block as therunningvalue, and the first element of the receiver is passed into the block as thenextvalue. - The result of the invocation is added to the returned stream.
- The result of the invocation (
running) and the next element of the receiver (next) is passed intoblock. - Steps 2 and 3 are repeated until all values have been processed.
startingValue - The value to be combined with the first element of the
receiver. This value may be nil.
block - A block that describes how to combine values of the
receiver. If the receiver is empty, this block will never be
invoked.
Examples
RACSequence *numbers = @[ @1, @2, @3, @4 ].rac_sequence;
// Contains 1, 3, 6, 10
RACSequence *sums = [numbers scanWithStart:@0 reduce:^(NSNumber *sum, NSNumber *next) {
return @(sum.integerValue + next.integerValue);
}];
Returns a new stream that consists of each application of block. If the
receiver is empty, an empty stream is returned.
Declared In
RACStream.hsetNameWithFormat:
Sets the name of the receiver to the given format string.
- (instancetype)setNameWithFormat:(NSString *)format, ...Discussion
This is for debugging purposes only, and won’t do anything unless the RAC_DEBUG_SIGNAL_NAMES environment variable is set.
Returns the receiver, for easy method chaining.
Declared In
RACStream.hskip:
Skips the first skipCount values in the receiver.
- (instancetype)skip:(NSUInteger)skipCountDiscussion
Returns the receiver after skipping the first skipCount values. If
skipCount is greater than the number of values in the stream, an empty
stream is returned.
Declared In
RACStream.hskipUntilBlock:
Skips values until the given block returns YES.
- (instancetype)skipUntilBlock:(BOOL ( ^ ) ( id x ))predicateDiscussion
Returns a stream containing the values of the receiver that follow any
initial values failing predicate. If predicate never returns YES,
an empty stream is returned.
Declared In
RACStream.hskipWhileBlock:
Skips values until the given block returns NO.
- (instancetype)skipWhileBlock:(BOOL ( ^ ) ( id x ))predicateDiscussion
Returns a stream containing the values of the receiver that follow any
initial values passing predicate. If predicate never returns NO, an
empty stream is returned.
Declared In
RACStream.hstartWith:
Returns a stream consisting of value, followed by the values in the
receiver.
- (instancetype)startWith:(id)valueDeclared In
RACStream.htake:
Returns a stream of the first count values in the receiver. If count is
greater than or equal to the number of values in the stream, a stream
equivalent to the receiver is returned.
- (instancetype)take:(NSUInteger)countDeclared In
RACStream.htakeUntilBlock:
Takes values until the given block returns YES.
- (instancetype)takeUntilBlock:(BOOL ( ^ ) ( id x ))predicateDiscussion
Returns a stream of the initial values in the receiver that fail predicate.
If predicate never returns YES, a stream equivalent to the receiver is
returned.
Declared In
RACStream.htakeWhileBlock:
Takes values until the given block returns NO.
- (instancetype)takeWhileBlock:(BOOL ( ^ ) ( id x ))predicateDiscussion
Returns a stream of the initial values in the receiver that pass predicate.
If predicate never returns NO, a stream equivalent to the receiver is
returned.
Declared In
RACStream.hzipWith:
Zips the values in the receiver with those of the given stream to create RACTuples.
- (instancetype)zipWith:(RACStream *)streamDiscussion
The first value of each stream will be combined, then the second value, and so forth, until at least one of the streams is exhausted.
stream - The stream to zip with. This must be an instance of the same
concrete class as the receiver, and should not be nil.
Returns a new stream of RACTuples, representing the zipped values of the two streams.
Declared In
RACStream.h