Skip to content

Commit 9dd61b6

Browse files
authored
1.x: scan & reduce give javadoc about unsharing the initialValue (ReactiveX#4063)
1 parent a077f9b commit 9dd61b6

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/main/java/rx/Observable.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7201,8 +7201,8 @@ public final Observable<T> rebatchRequests(int n) {
72017201
* that does a similar operation on lists.
72027202
* <dl>
72037203
* <dt><b>Backpressure Support:</b></dt>
7204-
* <dd>This operator does not support backpressure because by intent it will receive all values and reduce
7205-
* them to a single {@code onNext}.</dd>
7204+
* <dd>The operator honors backpressure of its downstream consumer and consumes the
7205+
* upstream source in unbounded mode.</dd>
72067206
* <dt><b>Scheduler:</b></dt>
72077207
* <dd>{@code reduce} does not operate by default on a particular {@link Scheduler}.</dd>
72087208
* </dl>
@@ -7238,10 +7238,24 @@ public final Observable<T> reduce(Func2<T, T, T> accumulator) {
72387238
* This technique, which is called "reduce" here, is sometimes called "aggregate," "fold," "accumulate,"
72397239
* "compress," or "inject" in other programming contexts. Groovy, for instance, has an {@code inject} method
72407240
* that does a similar operation on lists.
7241+
* <p>
7242+
* Note that the {@code initialValue} is shared among all subscribers to the resulting Observable
7243+
* and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer
7244+
* the application of this operator via {@link #defer(Func0)}:
7245+
* <pre><code>
7246+
* Observable&lt;T> source = ...
7247+
* Observable.defer(() -> source.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)));
7248+
*
7249+
* // alternatively, by using compose to stay fluent
7250+
*
7251+
* source.compose(o ->
7252+
* Observable.defer(() -> o.reduce(new ArrayList&lt;>(), (list, item) -> list.add(item)))
7253+
* );
7254+
* </code></pre>
72417255
* <dl>
72427256
* <dt><b>Backpressure Support:</b></dt>
7243-
* <dd>This operator does not support backpressure because by intent it will receive all values and reduce
7244-
* them to a single {@code onNext}.</dd>
7257+
* <dd>The operator honors backpressure of its downstream consumer and consumes the
7258+
* upstream source in unbounded mode.</dd>
72457259
* <dt><b>Scheduler:</b></dt>
72467260
* <dd>{@code reduce} does not operate by default on a particular {@link Scheduler}.</dd>
72477261
* </dl>
@@ -8167,7 +8181,23 @@ public final Observable<T> scan(Func2<T, T, T> accumulator) {
81678181
* <p>
81688182
* Note that the Observable that results from this method will emit {@code initialValue} as its first
81698183
* emitted item.
8184+
* <p>
8185+
* Note that the {@code initialValue} is shared among all subscribers to the resulting Observable
8186+
* and may cause problems if it is mutable. To make sure each subscriber gets its own value, defer
8187+
* the application of this operator via {@link #defer(Func0)}:
8188+
* <pre><code>
8189+
* Observable&lt;T> source = ...
8190+
* Observable.defer(() -> source.scan(new ArrayList&lt;>(), (list, item) -> list.add(item)));
8191+
*
8192+
* // alternatively, by using compose to stay fluent
8193+
*
8194+
* source.compose(o ->
8195+
* Observable.defer(() -> o.scan(new ArrayList&lt;>(), (list, item) -> list.add(item)))
8196+
* );
8197+
* </code></pre>
81708198
* <dl>
8199+
* <dt><b>Backpressure:</b></dt>
8200+
* <dd>The operator honors backpressure.</dd>
81718201
* <dt><b>Scheduler:</b></dt>
81728202
* <dd>{@code scan} does not operate by default on a particular {@link Scheduler}.</dd>
81738203
* </dl>

0 commit comments

Comments
 (0)