@@ -3459,7 +3459,7 @@ public final <R> Observable<R> cast(final Class<R> klass) {
34593459 * <dd>{@code collect} does not operate by default on a particular {@link Scheduler}.</dd>
34603460 * </dl>
34613461 *
3462- * @param state
3462+ * @param stateFactory
34633463 * the mutable data structure that will collect the items
34643464 * @param collector
34653465 * a function that accepts the {@code state} and an emitted item, and modifies {@code state}
@@ -3468,7 +3468,7 @@ public final <R> Observable<R> cast(final Class<R> klass) {
34683468 * into a single mutable data structure
34693469 * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#collect">RxJava wiki: collect</a>
34703470 */
3471- public final <R > Observable <R > collect (R state , final Action2 <R , ? super T > collector ) {
3471+ public final <R > Observable <R > collect (Func0 < R > stateFactory , final Action2 <R , ? super T > collector ) {
34723472 Func2 <R , T , R > accumulator = new Func2 <R , T , R >() {
34733473
34743474 @ Override
@@ -3478,7 +3478,14 @@ public final R call(R state, T value) {
34783478 }
34793479
34803480 };
3481- return reduce (state , accumulator );
3481+
3482+ /*
3483+ * Discussion and confirmation of implementation at
3484+ * https://github.com/ReactiveX/RxJava/issues/423#issuecomment-27642532
3485+ *
3486+ * It should use last() not takeLast(1) since it needs to emit an error if the sequence is empty.
3487+ */
3488+ return lift (new OperatorScan <R , T >(stateFactory , accumulator )).last ();
34823489 }
34833490
34843491 /**
@@ -5293,40 +5300,6 @@ public final <R> Observable<R> reduce(R initialValue, Func2<R, ? super T, R> acc
52935300 return scan (initialValue , accumulator ).takeLast (1 );
52945301 }
52955302
5296- /**
5297- * Returns an Observable that applies a specified accumulator function to the first item emitted by a source
5298- * Observable and a specified seed value, then feeds the result of that function along with the second item
5299- * emitted by an Observable into the same function, and so on until all items have been emitted by the
5300- * source Observable, emitting the final result from the final call to your function as its sole item.
5301- * <p>
5302- * <img width="640" height="325" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/reduceSeed.png" alt="">
5303- * <p>
5304- * This technique, which is called "reduce" here, is sometimec called "aggregate," "fold," "accumulate,"
5305- * "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method
5306- * that does a similar operation on lists.
5307- * <dl>
5308- * <dt><b>Backpressure Support:</b></dt>
5309- * <dd>This operator does not support backpressure because by intent it will receive all values and reduce
5310- * them to a single {@code onNext}.</dd>
5311- * <dt><b>Scheduler:</b></dt>
5312- * <dd>{@code reduce} does not operate by default on a particular {@link Scheduler}.</dd>
5313- * </dl>
5314- *
5315- * @param initialValueFactory
5316- * factory to produce the initial (seed) accumulator item each time the Observable is subscribed to
5317- * @param accumulator
5318- * an accumulator function to be invoked on each item emitted by the source Observable, the
5319- * result of which will be used in the next accumulator call
5320- * @return an Observable that emits a single item that is the result of accumulating the output from the
5321- * items emitted by the source Observable
5322- * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#reduce">RxJava wiki: reduce</a>
5323- * @see <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Wikipedia: Fold (higher-order function)</a>
5324- */
5325- public final <R > Observable <R > reduce (Func0 <R > initialValueFactory , Func2 <R , ? super T , R > accumulator ) {
5326- return scan (initialValueFactory , accumulator ).takeLast (1 );
5327- }
5328-
5329-
53305303 /**
53315304 * Returns an Observable that repeats the sequence of items emitted by the source Observable indefinitely.
53325305 * <p>
@@ -6359,37 +6332,6 @@ public final Observable<T> scan(Func2<T, T, T> accumulator) {
63596332 public final <R > Observable <R > scan (R initialValue , Func2 <R , ? super T , R > accumulator ) {
63606333 return lift (new OperatorScan <R , T >(initialValue , accumulator ));
63616334 }
6362-
6363- /**
6364- * Returns an Observable that applies a specified accumulator function to the first item emitted by a source
6365- * Observable and a seed value, then feeds the result of that function along with the second item emitted by
6366- * the source Observable into the same function, and so on until all items have been emitted by the source
6367- * Observable, emitting the result of each of these iterations.
6368- * <p>
6369- * <img width="640" height="320" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/scanSeed.png" alt="">
6370- * <p>
6371- * This sort of function is sometimes called an accumulator.
6372- * <p>
6373- * Note that the Observable that results from this method will emit the item returned from
6374- * {@code initialValueFactory} as its first emitted item.
6375- * <dl>
6376- * <dt><b>Scheduler:</b></dt>
6377- * <dd>{@code scan} does not operate by default on a particular {@link Scheduler}.</dd>
6378- * </dl>
6379- *
6380- * @param initialValueFactory
6381- * factory to produce the initial (seed) accumulator item each time the Observable is subscribed to
6382- * @param accumulator
6383- * an accumulator function to be invoked on each item emitted by the source Observable, whose
6384- * result will be emitted to {@link Observer}s via {@link Observer#onNext onNext} and used in the
6385- * next accumulator call
6386- * @return an Observable that emits the item returned from {@code initialValueFactory} followed by the
6387- * results of each call to the accumulator function
6388- * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Transforming-Observables#scan">RxJava wiki: scan</a>
6389- */
6390- public final <R > Observable <R > scan (Func0 <R > initialValueFactory , Func2 <R , ? super T , R > accumulator ) {
6391- return lift (new OperatorScan <R , T >(initialValueFactory , accumulator ));
6392- }
63936335
63946336 /**
63956337 * Forces an Observable's emissions and notifications to be serialized and for it to obey the Rx contract
0 commit comments