Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reduce): forward index to accumulator #2291

Merged
merged 1 commit into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions spec/operators/reduce-spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions, type};

Expand Down Expand Up @@ -65,6 +66,34 @@ describe('Observable.prototype.reduce', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should reduce with index without seed', (done: MochaDone) => {
const idx = [1, 2, 3, 4, 5];

Observable.range(0, 6).reduce((acc, value, index) => {
console.log(index);
console.log(value);
expect(idx.shift()).to.equal(index);
return value;
}).subscribe(null, null, () => {
expect(idx).to.be.empty;
done();
});
});

it('should reduce with index with seed', (done: MochaDone) => {
const idx = [0, 1, 2, 3, 4, 5];

Observable.range(0, 6).reduce((acc, value, index) => {
console.log(index);
console.log(value);
expect(idx.shift()).to.equal(index);
return value;
}, -1).subscribe(null, null, () => {
expect(idx).to.be.empty;
done();
});
});

it('should reduce with seed if source is empty', () => {
const e1 = hot('--a--^-------|');
const e1subs = '^ !';
Expand Down
19 changes: 12 additions & 7 deletions src/operator/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T
* @see {@link mergeScan}
* @see {@link scan}
*
* @param {function(acc: R, value: T): R} accumulator The accumulator function
* @param {function(acc: R, value: T, index: number): R} accumulator The accumulator function
* called on each source value.
* @param {R} [seed] The initial accumulation value.
* @return {Observable<R>} An observable of the accumulated values.
Expand All @@ -53,7 +53,7 @@ export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T
* @method reduce
* @owner Observable
*/
export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T) => R, seed?: R): Observable<R> {
export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T, index?: number) => R, seed?: R): Observable<R> {
let hasSeed = false;
// providing a seed of `undefined` *should* be valid and trigger
// hasSeed! so don't use `seed !== undefined` checks!
Expand All @@ -68,7 +68,7 @@ export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T
}

export class ReduceOperator<T, R> implements Operator<T, R> {
constructor(private accumulator: (acc: R, value: T) => R, private seed?: R, private hasSeed: boolean = false) {}
constructor(private accumulator: (acc: R, value: T, index?: number) => R, private seed?: R, private hasSeed: boolean = false) {}

call(subscriber: Subscriber<R>, source: any): any {
return source.subscribe(new ReduceSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
Expand All @@ -81,15 +81,20 @@ export class ReduceOperator<T, R> implements Operator<T, R> {
* @extends {Ignored}
*/
export class ReduceSubscriber<T, R> extends Subscriber<T> {
acc: T | R;
hasValue: boolean = false;
private index: number = 0;
private acc: T | R;
private hasValue: boolean = false;

constructor(destination: Subscriber<R>,
private accumulator: (acc: R, value: T) => R,
private accumulator: (acc: R, value: T, index?: number) => R,
seed: R,
private hasSeed: boolean) {
super(destination);
this.acc = seed;

if (!this.hasSeed) {
this.index++;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does index start at 1 if there is no seed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

currentIndex The index of the current element being processed in the array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.

I tried to follow same behavior to native reduce, where it says start from 0 if seed exists, 1 otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I've read spec incorrectly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kwonoj Nope, you read it correctly. TIL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth documenting this behaviour in the RxJs documentation?

}

protected _next(value: T) {
Expand All @@ -104,7 +109,7 @@ export class ReduceSubscriber<T, R> extends Subscriber<T> {
private _tryReduce(value: T) {
let result: any;
try {
result = this.accumulator(<R>this.acc, value);
result = this.accumulator(<R>this.acc, value, this.index++);
} catch (err) {
this.destination.error(err);
return;
Expand Down