@@ -5429,6 +5429,41 @@ public final Observable<T> reduce(Func2<T, T, T> accumulator) {
5429
5429
public final <R > Observable <R > reduce (R initialValue , Func2 <R , ? super T , R > accumulator ) {
5430
5430
return scan (initialValue , accumulator ).takeLast (1 );
5431
5431
}
5432
+
5433
+ /**
5434
+ * Returns an Observable that applies a function of your choosing to the first item emitted by a source
5435
+ * Observable and a specified seed value, then feeds the result of that function along with the second item
5436
+ * emitted by an Observable into the same function, and so on until all items have been emitted by the
5437
+ * source Observable, emitting the final result from the final call to your function as its sole item.
5438
+ * <p>
5439
+ * <img width="640" height="325" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/reduceSeed.png" alt="">
5440
+ * <p>
5441
+ * This technique, which is called "reduce" here, is sometimec called "aggregate," "fold," "accumulate,"
5442
+ * "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method
5443
+ * that does a similar operation on lists.
5444
+ * <dl>
5445
+ * <dt><b>Backpressure Support:</b></dt>
5446
+ * <dd>This operator does not support backpressure because by intent it will receive all values and reduce
5447
+ * them to a single {@code onNext}.</dd>
5448
+ * <dt><b>Scheduler:</b></dt>
5449
+ * <dd>{@code reduce} does not operate by default on a particular {@link Scheduler}.</dd>
5450
+ * </dl>
5451
+ *
5452
+ * @param initialValueFactory
5453
+ * factory to produce the initial (seed) accumulator item each time the Observable is subscribed to
5454
+ * @param accumulator
5455
+ * an accumulator function to be invoked on each item emitted by the source Observable, the
5456
+ * result of which will be used in the next accumulator call
5457
+ * @return an Observable that emits a single item that is the result of accumulating the output from the
5458
+ * items emitted by the source Observable
5459
+ * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#reduce">RxJava wiki: reduce</a>
5460
+ * @see <a href="http://msdn.microsoft.com/en-us/library/hh229154.aspx">MSDN: Observable.Aggregate</a>
5461
+ * @see <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Wikipedia: Fold (higher-order function)</a>
5462
+ */
5463
+ public final <R > Observable <R > reduce (Func0 <R > initialValueFactory , Func2 <R , ? super T , R > accumulator ) {
5464
+ return scan (initialValueFactory , accumulator ).takeLast (1 );
5465
+ }
5466
+
5432
5467
5433
5468
/**
5434
5469
* Returns an Observable that repeats the sequence of items emitted by the source Observable indefinitely.
@@ -6491,6 +6526,38 @@ public final Observable<T> scan(Func2<T, T, T> accumulator) {
6491
6526
public final <R > Observable <R > scan (R initialValue , Func2 <R , ? super T , R > accumulator ) {
6492
6527
return lift (new OperatorScan <R , T >(initialValue , accumulator ));
6493
6528
}
6529
+
6530
+ /**
6531
+ * Returns an Observable that applies a function of your choosing to the first item emitted by a source
6532
+ * Observable and a seed value, then feeds the result of that function along with the second item emitted by
6533
+ * the source Observable into the same function, and so on until all items have been emitted by the source
6534
+ * Observable, emitting the result of each of these iterations.
6535
+ * <p>
6536
+ * <img width="640" height="320" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/scanSeed.png" alt="">
6537
+ * <p>
6538
+ * This sort of function is sometimes called an accumulator.
6539
+ * <p>
6540
+ * Note that the Observable that results from this method will emit {@code initialValue} as its first
6541
+ * emitted item.
6542
+ * <dl>
6543
+ * <dt><b>Scheduler:</b></dt>
6544
+ * <dd>{@code scan} does not operate by default on a particular {@link Scheduler}.</dd>
6545
+ * </dl>
6546
+ *
6547
+ * @param initialValueFactory
6548
+ * factory to produce the initial (seed) accumulator item each time the Observable is subscribed to
6549
+ * @param accumulator
6550
+ * an accumulator function to be invoked on each item emitted by the source Observable, whose
6551
+ * result will be emitted to {@link Observer}s via {@link Observer#onNext onNext} and used in the
6552
+ * next accumulator call
6553
+ * @return an Observable that emits {@code initialValue} followed by the results of each call to the
6554
+ * accumulator function
6555
+ * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Transforming-Observables#scan">RxJava wiki: scan</a>
6556
+ * @see <a href="http://msdn.microsoft.com/en-us/library/hh211665.aspx">MSDN: Observable.Scan</a>
6557
+ */
6558
+ public final <R > Observable <R > scan (Func0 <R > initialValueFactory , Func2 <R , ? super T , R > accumulator ) {
6559
+ return lift (new OperatorScan <R , T >(initialValueFactory , accumulator ));
6560
+ }
6494
6561
6495
6562
/**
6496
6563
* Forces an Observable's emissions and notifications to be serialized and for it to obey the Rx contract
0 commit comments