Skip to content

Commit 42a355a

Browse files
devisnikakarnokd
authored andcommitted
Fix javadoc for Observable.reduce() and Observable.reduceWith() (#5406)
* fix javadoc sample code for Observable.reduce() to compile * update javadoc for Observable.reduceWith() * fix javadoc sample code for Flowable.reduce() to compile * update javadoc for Flowable.reduceWith() * remove one more word to be similar to Observable.reduce() * fix Publisher.defer() to Flowable.defer() * add example using reduceWith() to Observable.reduce() and Flowable.reduce() * add @see reduceWith() to reduce()
1 parent b17bd1a commit 42a355a

File tree

2 files changed

+26
-42
lines changed

2 files changed

+26
-42
lines changed

src/main/java/io/reactivex/Flowable.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10723,18 +10723,22 @@ public final Maybe<T> reduce(BiFunction<T, T, T> reducer) {
1072310723
* "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method
1072410724
* that does a similar operation on lists.
1072510725
* <p>
10726-
* Note that the {@code initialValue} is shared among all subscribers to the resulting Publisher
10726+
* Note that the {@code seed} is shared among all subscribers to the resulting Publisher
1072710727
* and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer
1072810728
* the application of this operator via {@link #defer(Callable)}:
1072910729
* <pre><code>
1073010730
* Publisher&lt;T> source = ...
10731-
* Publisher.defer(() -> source.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)));
10731+
* Single.defer(() -> source.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)));
1073210732
*
1073310733
* // alternatively, by using compose to stay fluent
1073410734
*
1073510735
* source.compose(o ->
10736-
* Publisher.defer(() -> o.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)))
10737-
* );
10736+
* Flowable.defer(() -> o.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)).toFlowable())
10737+
* ).firstOrError();
10738+
*
10739+
* // or, by using reduceWith instead of reduce
10740+
*
10741+
* source.reduceWith(() -> new ArrayList&lt;>(), (list, item) -> list.add(item)));
1073810742
* </code></pre>
1073910743
* <dl>
1074010744
* <dt><b>Backpressure:</b></dt>
@@ -10754,6 +10758,7 @@ public final Maybe<T> reduce(BiFunction<T, T, T> reducer) {
1075410758
* items emitted by the source Publisher
1075510759
* @see <a href="http://reactivex.io/documentation/operators/reduce.html">ReactiveX operators documentation: Reduce</a>
1075610760
* @see <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Wikipedia: Fold (higher-order function)</a>
10761+
* @see #reduceWith(Callable, BiFunction)
1075710762
*/
1075810763
@CheckReturnValue
1075910764
@BackpressureSupport(BackpressureKind.UNBOUNDED_IN)
@@ -10766,29 +10771,16 @@ public final <R> Single<R> reduce(R seed, BiFunction<R, ? super T, R> reducer) {
1076610771

1076710772
/**
1076810773
* Returns a Flowable that applies a specified accumulator function to the first item emitted by a source
10769-
* Publisher and a specified seed value, then feeds the result of that function along with the second item
10770-
* emitted by a Publisher into the same function, and so on until all items have been emitted by the
10771-
* source Publisher, emitting the final result from the final call to your function as its sole item.
10774+
* Publisher and a seed value derived from calling a specified seedSupplier, then feeds the result
10775+
* of that function along with the second item emitted by a Publisher into the same function, and so on until
10776+
* all items have been emitted by the source Publisher, emitting the final result from the final call to your
10777+
* function as its sole item.
1077210778
* <p>
1077310779
* <img width="640" height="325" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/reduceSeed.png" alt="">
1077410780
* <p>
1077510781
* This technique, which is called "reduce" here, is sometimes called "aggregate," "fold," "accumulate,"
1077610782
* "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method
1077710783
* that does a similar operation on lists.
10778-
* <p>
10779-
* Note that the {@code initialValue} is shared among all subscribers to the resulting Publisher
10780-
* and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer
10781-
* the application of this operator via {@link #defer(Callable)}:
10782-
* <pre><code>
10783-
* Publisher&lt;T> source = ...
10784-
* Publisher.defer(() -> source.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)));
10785-
*
10786-
* // alternatively, by using compose to stay fluent
10787-
*
10788-
* source.compose(o ->
10789-
* Publisher.defer(() -> o.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)))
10790-
* );
10791-
* </code></pre>
1079210784
* <dl>
1079310785
* <dt><b>Backpressure:</b></dt>
1079410786
* <dd>The operator honors backpressure of its downstream consumer and consumes the

src/main/java/io/reactivex/Observable.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8922,18 +8922,22 @@ public final Maybe<T> reduce(BiFunction<T, T, T> reducer) {
89228922
* "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method
89238923
* that does a similar operation on lists.
89248924
* <p>
8925-
* Note that the {@code initialValue} is shared among all subscribers to the resulting ObservableSource
8925+
* Note that the {@code seed} is shared among all subscribers to the resulting ObservableSource
89268926
* and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer
89278927
* the application of this operator via {@link #defer(Callable)}:
89288928
* <pre><code>
89298929
* ObservableSource&lt;T> source = ...
8930-
* Observable.defer(() -> source.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)));
8930+
* Single.defer(() -> source.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)));
89318931
*
89328932
* // alternatively, by using compose to stay fluent
89338933
*
89348934
* source.compose(o ->
8935-
* Observable.defer(() -> o.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)))
8936-
* );
8935+
* Observable.defer(() -> o.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)).toObservable())
8936+
* ).firstOrError();
8937+
*
8938+
* // or, by using reduceWith instead of reduce
8939+
*
8940+
* source.reduceWith(() -> new ArrayList&lt;>(), (list, item) -> list.add(item)));
89378941
* </code></pre>
89388942
* <dl>
89398943
* <dt><b>Scheduler:</b></dt>
@@ -8950,6 +8954,7 @@ public final Maybe<T> reduce(BiFunction<T, T, T> reducer) {
89508954
* items emitted by the source ObservableSource
89518955
* @see <a href="http://reactivex.io/documentation/operators/reduce.html">ReactiveX operators documentation: Reduce</a>
89528956
* @see <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">Wikipedia: Fold (higher-order function)</a>
8957+
* @see #reduceWith(Callable, BiFunction)
89538958
*/
89548959
@CheckReturnValue
89558960
@SchedulerSupport(SchedulerSupport.NONE)
@@ -8961,29 +8966,16 @@ public final <R> Single<R> reduce(R seed, BiFunction<R, ? super T, R> reducer) {
89618966

89628967
/**
89638968
* Returns a Single that applies a specified accumulator function to the first item emitted by a source
8964-
* ObservableSource and a specified seed value, then feeds the result of that function along with the second item
8965-
* emitted by an ObservableSource into the same function, and so on until all items have been emitted by the
8966-
* source ObservableSource, emitting the final result from the final call to your function as its sole item.
8969+
* ObservableSource and a seed value derived from calling a specified seedSupplier, then feeds the result
8970+
* of that function along with the second item emitted by an ObservableSource into the same function,
8971+
* and so on until all items have been emitted by the source ObservableSource, emitting the final result
8972+
* from the final call to your function as its sole item.
89678973
* <p>
89688974
* <img width="640" height="325" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/reduceSeed.2.png" alt="">
89698975
* <p>
89708976
* This technique, which is called "reduce" here, is sometimes called "aggregate," "fold," "accumulate,"
89718977
* "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method
89728978
* that does a similar operation on lists.
8973-
* <p>
8974-
* Note that the {@code initialValue} is shared among all subscribers to the resulting ObservableSource
8975-
* and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer
8976-
* the application of this operator via {@link #defer(Callable)}:
8977-
* <pre><code>
8978-
* ObservableSource&lt;T> source = ...
8979-
* Observable.defer(() -> source.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)));
8980-
*
8981-
* // alternatively, by using compose to stay fluent
8982-
*
8983-
* source.compose(o ->
8984-
* Observable.defer(() -> o.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)))
8985-
* );
8986-
* </code></pre>
89878979
* <dl>
89888980
* <dt><b>Scheduler:</b></dt>
89898981
* <dd>{@code reduceWith} does not operate by default on a particular {@link Scheduler}.</dd>

0 commit comments

Comments
 (0)