Skip to content

Commit 993a2c3

Browse files
committed
docs(operators): write comprehensive JSDoc for reduce
Fixes issue #1685.
1 parent 97bcc15 commit 993a2c3

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

src/operator/reduce.ts

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,65 @@ import {Operator} from '../Operator';
33
import {Subscriber} from '../Subscriber';
44

55
/**
6-
* Returns an Observable that applies a specified accumulator function to the first item emitted by a source Observable,
7-
* then feeds the result of that function along with the second item emitted by the source Observable into the same
8-
* function, and so on until all items have been emitted by the source Observable, and emits the final result from
9-
* the final call to your function as its sole item.
10-
* This technique, which is called "reduce" here, is sometimes called "aggregate," "fold," "accumulate," "compress," or
11-
* "inject" in other programming contexts.
6+
* Applies an accumulator function over the source Observable, and returns the
7+
* accumulated result when the source completes, given an optional seed value.
8+
*
9+
* <span class="informal">Combines together all values emitted on the source,
10+
* using an accumulator function that knows how to join a new source value into
11+
* the accumulation from the past.</span>
1212
*
1313
* <img src="./img/reduce.png" width="100%">
1414
*
15-
* @param {initialValue} the initial (seed) accumulator value
16-
* @param {accumulator} an accumulator function to be invoked on each item emitted by the source Observable, the
17-
* result of which will be used in the next accumulator call.
18-
* @return {Observable} an Observable that emits a single item that is the result of accumulating the output from the
19-
* items emitted by the source Observable.
15+
* Like
16+
* [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce),
17+
* `reduce` applies an `accumulator` function against an accumulation and each
18+
* value of the source Observable (from the past) to reduce it to a single
19+
* value, emitted on the output Observable. Note that `reduce` will only emit
20+
* one value, only when the source Observable completes. It is equivalent to
21+
* applying operator {@link scan} followed by operator {@link last}.
22+
*
23+
* Returns an Observable that applies a specified `accumulator` function to each
24+
* item emitted by the source Observable. If a `seed` value is specified, then
25+
* that value will be used as the initial value for the accumulator. If no seed
26+
* value is specified, the first item of the source is used as the seed.
27+
*
28+
* @example <caption>Count the number of click events that happened in 5 seconds</caption>
29+
* var clicksInFiveSeconds = Rx.Observable.fromEvent(document, 'click')
30+
* .takeUntil(Rx.Observable.interval(5000));
31+
* var ones = clicksInFiveSeconds.mapTo(1);
32+
* var seed = 0;
33+
* var count = ones.reduce((acc, one) => acc + one, seed);
34+
* count.subscribe(x => console.log(x));
35+
*
36+
* @see {@link count}
37+
* @see {@link expand}
38+
* @see {@link mergeScan}
39+
* @see {@link scan}
40+
*
41+
* @param {function(acc: R, value: T): R} accumulator The accumulator function
42+
* called on each source value.
43+
* @param {R} [seed] The initial accumulation value.
44+
* @return {Observable<R>} An observable of the accumulated values.
45+
* @return {Observable<R>} An Observable that emits a single value that is the
46+
* result of accumulating the values emitted by the source Observable.
2047
* @method reduce
2148
* @owner Observable
2249
*/
23-
export function reduce<T, R>(project: (acc: R, value: T) => R, seed?: R): Observable<R> {
24-
return this.lift(new ReduceOperator(project, seed));
50+
export function reduce<T, R>(accumulator: (acc: R, value: T) => R, seed?: R): Observable<R> {
51+
return this.lift(new ReduceOperator(accumulator, seed));
2552
}
2653

2754
export interface ReduceSignature<T> {
28-
<R>(project: (acc: R, value: T) => R, seed?: R): Observable<R>;
55+
<R>(accumulator: (acc: R, value: T) => R, seed?: R): Observable<R>;
2956
}
3057

3158
export class ReduceOperator<T, R> implements Operator<T, R> {
3259

33-
constructor(private project: (acc: R, value: T) => R, private seed?: R) {
60+
constructor(private accumulator: (acc: R, value: T) => R, private seed?: R) {
3461
}
3562

3663
call(subscriber: Subscriber<R>, source: any): any {
37-
return source._subscribe(new ReduceSubscriber(subscriber, this.project, this.seed));
64+
return source._subscribe(new ReduceSubscriber(subscriber, this.accumulator, this.seed));
3865
}
3966
}
4067

@@ -48,12 +75,13 @@ export class ReduceSubscriber<T, R> extends Subscriber<T> {
4875
acc: T | R;
4976
hasSeed: boolean;
5077
hasValue: boolean = false;
51-
project: (acc: R, value: T) => R;
5278

53-
constructor(destination: Subscriber<R>, project: (acc: R, value: T) => R, seed?: R) {
79+
constructor(destination: Subscriber<R>,
80+
private accumulator: (acc: R, value: T) => R,
81+
seed?: R) {
5482
super(destination);
5583
this.acc = seed;
56-
this.project = project;
84+
this.accumulator = accumulator;
5785
this.hasSeed = typeof seed !== 'undefined';
5886
}
5987

@@ -69,7 +97,7 @@ export class ReduceSubscriber<T, R> extends Subscriber<T> {
6997
private _tryReduce(value: T) {
7098
let result: any;
7199
try {
72-
result = this.project(<R>this.acc, value);
100+
result = this.accumulator(<R>this.acc, value);
73101
} catch (err) {
74102
this.destination.error(err);
75103
return;

src/operator/scan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Observable} from '../Observable';
33
import {Subscriber} from '../Subscriber';
44

55
/**
6-
* Applies an accumulation function over the source Observable, and returns each
6+
* Applies an accumulator function over the source Observable, and returns each
77
* intermediate result, with an optional seed value.
88
*
99
* <span class="informal">It's like {@link reduce}, but emits the current

0 commit comments

Comments
 (0)