RACSequence Class Reference
Inherits from | RACStream : NSObject |
Conforms to | NSCoding NSCopying NSFastEnumeration |
Declared in | RACSequence.h RACSequence.m |
Overview
Represents an immutable sequence of values. Unless otherwise specified, the sequences' values are evaluated lazily on demand. Like Cocoa collections, sequences cannot contain nil.
Most inherited RACStream methods that accept a block will execute the block at most once for each value that is evaluated in the returned sequence. Side effects are subject to the behavior described in sequenceWithHeadBlock:tailBlock:.
Implemented as a class cluster. A minimal implementation for a subclass consists simply of head and tail.
Tasks
Other Methods
-
head
property -
tail
property -
array
property -
objectEnumerator
property -
eagerSequence
property -
lazySequence
property -
– signal
-
– signalWithScheduler:
-
– foldLeftWithStart:reduce:
-
– foldRightWithStart:reduce:
-
– any:
-
– all:
-
– objectPassingTest:
-
+ sequenceWithHeadBlock:tailBlock:
Other Methods
Properties
array
Evaluates the full sequence to produce an equivalently-sized array.
@property (nonatomic, copy, readonly) NSArray *array
Declared In
RACSequence.h
eagerSequence
Converts a sequence into an eager sequence.
@property (nonatomic, copy, readonly) RACSequence *eagerSequence
Discussion
An eager sequence fully evaluates all of its values immediately. Sequences derived from an eager sequence will also be eager.
Returns a new eager sequence, or the receiver if the sequence is already eager.
Declared In
RACSequence.h
head
The first object in the sequence, or nil if the sequence is empty.
@property (nonatomic, strong, readonly) id head
Discussion
Subclasses must provide an implementation of this method.
Declared In
RACSequence.h
lazySequence
Converts a sequence into a lazy sequence.
@property (nonatomic, copy, readonly) RACSequence *lazySequence
Discussion
A lazy sequence evaluates its values on demand, as they are accessed. Sequences derived from a lazy sequence will also be lazy.
Returns a new lazy sequence, or the receiver if the sequence is already lazy.
Declared In
RACSequence.h
objectEnumerator
Returns an enumerator of all objects in the sequence.
@property (nonatomic, copy, readonly) NSEnumerator *objectEnumerator
Declared In
RACSequence.h
tail
All but the first object in the sequence, or nil if the sequence is empty.
@property (nonatomic, strong, readonly) RACSequence *tail
Discussion
Subclasses must provide an implementation of this method.
Declared In
RACSequence.h
Class Methods
return:
Lifts value
into the stream monad.
+ (instancetype)return:(id)value
Discussion
Returns a stream containing only the given value.
Declared In
RACStream.h
sequenceWithHeadBlock:tailBlock:
Creates a sequence that dynamically generates its values.
+ (RACSequence *)sequenceWithHeadBlock:(id ( ^ ) ( void ))headBlock tailBlock:(RACSequence *( ^ ) ( void ))tailBlock
Discussion
headBlock - Invoked the first time head is accessed. tailBlock - Invoked the first time tail is accessed.
The results from each block are memoized, so each block will be invoked at most once, no matter how many times the head and tail properties of the sequence are accessed.
Any side effects in headBlock
or tailBlock
should be thread-safe, since
the sequence may be evaluated at any time from any thread. Not only that, but
tail may be accessed before head, or both may be accessed simultaneously.
As noted above, side effects will only be triggered the first time head or
tail is invoked.
Returns a sequence that lazily invokes the given blocks to provide head and
tail. headBlock
must not be nil.
Declared In
RACSequence.h
Instance Methods
all:
Check if all values in the sequence pass the block.
- (BOOL)all:(BOOL ( ^ ) ( id value ))block
Discussion
block - The block predicate used to check each item. Cannot be nil.
Returns a boolean indicating if all values in the sequence passed.
Declared In
RACSequence.h
any:
Check if any value in sequence passes the block.
- (BOOL)any:(BOOL ( ^ ) ( id value ))block
Discussion
block - The block predicate used to check each item. Cannot be nil.
Returns a boolean indiciating if any value in the sequence passed.
Declared In
RACSequence.h
bind:
Lazily binds a block to the values in the receiver.
- (instancetype)bind:(RACStreamBindBlock ( ^ ) ( void ))block
Discussion
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.h
concat:
Appends the values of stream
to the values in the receiver.
- (instancetype)concat:(RACStream *)stream
Discussion
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.h
foldLeftWithStart:reduce:
Applies a left fold to the sequence.
- (id)foldLeftWithStart:(id)start reduce:(id ( ^ ) ( id accumulator , id value ))reduce
Discussion
This is the same as iterating the sequence along with a provided start value. This uses a constant amount of memory. A left fold is left-associative so in the sequence [1,2,3] the block would applied in the following order: reduce(reduce(reduce(start, 1), 2), 3)
start - The starting value for the fold. Used as accumulator
for the
first fold.
reduce - The block used to combine the accumulated value and the next value.
Cannot be nil.
Returns a reduced value.
Declared In
RACSequence.h
foldRightWithStart:reduce:
Applies a right fold to the sequence.
- (id)foldRightWithStart:(id)start reduce:(id ( ^ ) ( id first , RACSequence *rest ))reduce
Discussion
A right fold is equivalent to recursion on the list. The block is evaluated from the right to the left in list. It is right associative so it’s applied to the rightmost elements first. For example, in the sequence [1,2,3] the block is applied in the order: reduce(1, reduce(2, reduce(3, start)))
start - The starting value for the fold.
reduce - The block used to combine the accumulated value and the next head.
The block is given the accumulated value and the value of the rest
of the computation (result of the recursion). This is computed when
you retrieve its value using rest.
head. This allows you to
prevent unnecessary computation by not accessing
rest.
head if you
don’t need to.
Returns a reduced value.
Declared In
RACSequence.h
objectPassingTest:
Returns the first object that passes the block.
- (id)objectPassingTest:(BOOL ( ^ ) ( id value ))block
Discussion
block - The block predicate used to check each item. Cannot be nil.
Returns an object that passes the block or nil if no objects passed.
Declared In
RACSequence.h
signal
Invokes signalWithScheduler: with a new RACScheduler.
- (RACSignal *)signal
Declared In
RACSequence.h
signalWithScheduler:
Evaluates the full sequence on the given scheduler.
- (RACSignal *)signalWithScheduler:(RACScheduler *)scheduler
Discussion
Each item is evaluated in its own scheduled block, such that control of the scheduler is yielded between each value.
Returns a signal which sends the receiver’s values on the given scheduler as they’re evaluated.
Declared In
RACSequence.h
zipWith:
Zips the values in the receiver with those of the given stream to create RACTuples.
- (instancetype)zipWith:(RACSequence *)sequence
Discussion
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