From 2db67486ca967cd89328d1beb1d4b86cd7544ebe Mon Sep 17 00:00:00 2001 From: Volker Leck Date: Sun, 11 Jun 2017 01:00:45 +0100 Subject: [PATCH 1/8] fix javadoc sample code for Observable.reduce() to compile --- src/main/java/io/reactivex/Observable.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/reactivex/Observable.java b/src/main/java/io/reactivex/Observable.java index a0e945af7f..ec980b51a8 100644 --- a/src/main/java/io/reactivex/Observable.java +++ b/src/main/java/io/reactivex/Observable.java @@ -8922,18 +8922,18 @@ public final Maybe reduce(BiFunction reducer) { * "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method * that does a similar operation on lists. *

- * Note that the {@code initialValue} is shared among all subscribers to the resulting ObservableSource + * Note that the {@code seed} is shared among all subscribers to the resulting ObservableSource * and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer * the application of this operator via {@link #defer(Callable)}: *


      * ObservableSource<T> source = ...
-     * Observable.defer(() -> source.reduce(new ArrayList<>(), (list, item) -> list.add(item)));
+     * Single.defer(() -> source.reduce(new ArrayList<>(), (list, item) -> list.add(item)));
      *
      * // alternatively, by using compose to stay fluent
      *
      * source.compose(o ->
-     *     Observable.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)))
-     * );
+     *     Observable.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)).toObservable())
+     * ).firstOrError();
      * 
*
*
Scheduler:
From 7af41ac0b45f46d3fa2c5ec12aa0786274297ece Mon Sep 17 00:00:00 2001 From: Volker Leck Date: Sun, 11 Jun 2017 01:01:26 +0100 Subject: [PATCH 2/8] update javadoc for Observable.reduceWith() --- src/main/java/io/reactivex/Observable.java | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/reactivex/Observable.java b/src/main/java/io/reactivex/Observable.java index ec980b51a8..b8ddc602cd 100644 --- a/src/main/java/io/reactivex/Observable.java +++ b/src/main/java/io/reactivex/Observable.java @@ -8961,29 +8961,16 @@ public final Single reduce(R seed, BiFunction reducer) { /** * Returns a Single that applies a specified accumulator function to the first item emitted by a source - * ObservableSource and a specified seed value, then feeds the result of that function along with the second item - * emitted by an ObservableSource into the same function, and so on until all items have been emitted by the - * source ObservableSource, emitting the final result from the final call to your function as its sole item. + * ObservableSource and a seed value derived from calling a specified seedSupplier, then feeds the result + * of that function along with the second item emitted by an ObservableSource into the same function, + * and so on until all items have been emitted by the source ObservableSource, emitting the final result + * from the final call to your function as its sole item. *

* *

* This technique, which is called "reduce" here, is sometimes called "aggregate," "fold," "accumulate," * "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method * that does a similar operation on lists. - *

- * Note that the {@code initialValue} is shared among all subscribers to the resulting ObservableSource - * and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer - * the application of this operator via {@link #defer(Callable)}: - *


-     * ObservableSource<T> source = ...
-     * Observable.defer(() -> source.reduce(new ArrayList<>(), (list, item) -> list.add(item)));
-     *
-     * // alternatively, by using compose to stay fluent
-     *
-     * source.compose(o ->
-     *     Observable.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)))
-     * );
-     * 
*
*
Scheduler:
*
{@code reduceWith} does not operate by default on a particular {@link Scheduler}.
From 18cb093eb3e83554d97aae02e1dd52a27fdbece0 Mon Sep 17 00:00:00 2001 From: Volker Leck Date: Sun, 11 Jun 2017 08:36:09 +0100 Subject: [PATCH 3/8] fix javadoc sample code for Flowable.reduce() to compile --- src/main/java/io/reactivex/Flowable.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/reactivex/Flowable.java b/src/main/java/io/reactivex/Flowable.java index 992653169c..7600716cdc 100644 --- a/src/main/java/io/reactivex/Flowable.java +++ b/src/main/java/io/reactivex/Flowable.java @@ -10723,18 +10723,18 @@ public final Maybe reduce(BiFunction reducer) { * "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method * that does a similar operation on lists. *

- * Note that the {@code initialValue} is shared among all subscribers to the resulting Publisher + * Note that the {@code seed} is shared among all subscribers to the resulting Publisher * and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer * the application of this operator via {@link #defer(Callable)}: *


      * Publisher<T> source = ...
-     * Publisher.defer(() -> source.reduce(new ArrayList<>(), (list, item) -> list.add(item)));
+     * Single.defer(() -> source.reduce(new ArrayList<>(), (list, item) -> list.add(item)));
      *
      * // alternatively, by using compose to stay fluent
      *
      * source.compose(o ->
-     *     Publisher.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)))
-     * );
+     *     Publisher.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)).toFlowable())
+     * ).firstOrError();
      * 
*
*
Backpressure:
From 3b07ff601064413a5a8bb8688ecfd85fa1cd716e Mon Sep 17 00:00:00 2001 From: Volker Leck Date: Sun, 11 Jun 2017 08:41:19 +0100 Subject: [PATCH 4/8] update javadoc for Flowable.reduceWith() --- src/main/java/io/reactivex/Flowable.java | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/reactivex/Flowable.java b/src/main/java/io/reactivex/Flowable.java index 7600716cdc..ab8cfea28c 100644 --- a/src/main/java/io/reactivex/Flowable.java +++ b/src/main/java/io/reactivex/Flowable.java @@ -10766,29 +10766,16 @@ public final Single reduce(R seed, BiFunction reducer) { /** * Returns a Flowable that applies a specified accumulator function to the first item emitted by a source - * Publisher and a specified seed value, then feeds the result of that function along with the second item - * emitted by a Publisher into the same function, and so on until all items have been emitted by the - * source Publisher, emitting the final result from the final call to your function as its sole item. + * Publisher and a specified seed value derived from calling a specified seedSupplier, then feeds the result + * of that function along with the second item emitted by a Publisher into the same function, and so on until + * all items have been emitted by the source Publisher, emitting the final result from the final call to your + * function as its sole item. *

* *

* This technique, which is called "reduce" here, is sometimes called "aggregate," "fold," "accumulate," * "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method * that does a similar operation on lists. - *

- * Note that the {@code initialValue} is shared among all subscribers to the resulting Publisher - * and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer - * the application of this operator via {@link #defer(Callable)}: - *


-     * Publisher<T> source = ...
-     * Publisher.defer(() -> source.reduce(new ArrayList<>(), (list, item) -> list.add(item)));
-     *
-     * // alternatively, by using compose to stay fluent
-     *
-     * source.compose(o ->
-     *     Publisher.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)))
-     * );
-     * 
*
*
Backpressure:
*
The operator honors backpressure of its downstream consumer and consumes the From 13ac31e1093cf9cbd1361b451b07a9f8228dcd8b Mon Sep 17 00:00:00 2001 From: Volker Leck Date: Sun, 11 Jun 2017 08:55:45 +0100 Subject: [PATCH 5/8] remove one more word to be similar to Observable.reduce() --- src/main/java/io/reactivex/Flowable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/reactivex/Flowable.java b/src/main/java/io/reactivex/Flowable.java index ab8cfea28c..975c0ea4c4 100644 --- a/src/main/java/io/reactivex/Flowable.java +++ b/src/main/java/io/reactivex/Flowable.java @@ -10766,7 +10766,7 @@ public final Single reduce(R seed, BiFunction reducer) { /** * Returns a Flowable that applies a specified accumulator function to the first item emitted by a source - * Publisher and a specified seed value derived from calling a specified seedSupplier, then feeds the result + * Publisher and a seed value derived from calling a specified seedSupplier, then feeds the result * of that function along with the second item emitted by a Publisher into the same function, and so on until * all items have been emitted by the source Publisher, emitting the final result from the final call to your * function as its sole item. From 7c24e34cbe5a02e490172e5d06c35df121c866ef Mon Sep 17 00:00:00 2001 From: Volker Leck Date: Sun, 11 Jun 2017 08:57:41 +0100 Subject: [PATCH 6/8] fix Publisher.defer() to Flowable.defer() --- src/main/java/io/reactivex/Flowable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/reactivex/Flowable.java b/src/main/java/io/reactivex/Flowable.java index 975c0ea4c4..113d12d0aa 100644 --- a/src/main/java/io/reactivex/Flowable.java +++ b/src/main/java/io/reactivex/Flowable.java @@ -10733,7 +10733,7 @@ public final Maybe reduce(BiFunction reducer) { * // alternatively, by using compose to stay fluent * * source.compose(o -> - * Publisher.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)).toFlowable()) + * Flowable.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)).toFlowable()) * ).firstOrError(); * *
From 2d445bc6dddfa5938e84a1dc65aa847b37f120c9 Mon Sep 17 00:00:00 2001 From: Volker Leck Date: Sun, 11 Jun 2017 09:11:33 +0100 Subject: [PATCH 7/8] add example using reduceWith() to Observable.reduce() and Flowable.reduce() --- src/main/java/io/reactivex/Flowable.java | 4 ++++ src/main/java/io/reactivex/Observable.java | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/io/reactivex/Flowable.java b/src/main/java/io/reactivex/Flowable.java index 113d12d0aa..2307e81fb2 100644 --- a/src/main/java/io/reactivex/Flowable.java +++ b/src/main/java/io/reactivex/Flowable.java @@ -10735,6 +10735,10 @@ public final Maybe reduce(BiFunction reducer) { * source.compose(o -> * Flowable.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)).toFlowable()) * ).firstOrError(); + * + * // or, by using reduceWith instead of reduce + * + * source.reduceWith(() -> new ArrayList<>(), (list, item) -> list.add(item))); * *
*
Backpressure:
diff --git a/src/main/java/io/reactivex/Observable.java b/src/main/java/io/reactivex/Observable.java index b8ddc602cd..ad67c19818 100644 --- a/src/main/java/io/reactivex/Observable.java +++ b/src/main/java/io/reactivex/Observable.java @@ -8934,6 +8934,10 @@ public final Maybe reduce(BiFunction reducer) { * source.compose(o -> * Observable.defer(() -> o.reduce(new ArrayList<>(), (list, item) -> list.add(item)).toObservable()) * ).firstOrError(); + * + * // or, by using reduceWith instead of reduce + * + * source.reduceWith(() -> new ArrayList<>(), (list, item) -> list.add(item))); * *
*
Scheduler:
From 67575488d65f46e5c7a8380abd69809a11e3cae6 Mon Sep 17 00:00:00 2001 From: Volker Leck Date: Sun, 11 Jun 2017 09:17:39 +0100 Subject: [PATCH 8/8] add @see reduceWith() to reduce() --- src/main/java/io/reactivex/Flowable.java | 1 + src/main/java/io/reactivex/Observable.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/io/reactivex/Flowable.java b/src/main/java/io/reactivex/Flowable.java index 2307e81fb2..c49689484c 100644 --- a/src/main/java/io/reactivex/Flowable.java +++ b/src/main/java/io/reactivex/Flowable.java @@ -10758,6 +10758,7 @@ public final Maybe reduce(BiFunction reducer) { * items emitted by the source Publisher * @see ReactiveX operators documentation: Reduce * @see Wikipedia: Fold (higher-order function) + * @see #reduceWith(Callable, BiFunction) */ @CheckReturnValue @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) diff --git a/src/main/java/io/reactivex/Observable.java b/src/main/java/io/reactivex/Observable.java index ad67c19818..c28c846d26 100644 --- a/src/main/java/io/reactivex/Observable.java +++ b/src/main/java/io/reactivex/Observable.java @@ -8954,6 +8954,7 @@ public final Maybe reduce(BiFunction reducer) { * items emitted by the source ObservableSource * @see ReactiveX operators documentation: Reduce * @see Wikipedia: Fold (higher-order function) + * @see #reduceWith(Callable, BiFunction) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE)