Skip to content

Commit

Permalink
style(reduce): change signatures and parameter name
Browse files Browse the repository at this point in the history
The second parameter of reduce isn't the accumulator, but the seed.
This commit change `acc` name to `seed`, use a shorthand for parameter properties
and fix some signature types.
  • Loading branch information
adrianomelo authored and benlesh committed Nov 3, 2015
1 parent 7d869e9 commit e96d810
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/CoreOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface CoreOperators<T> {
publish?: () => ConnectableObservable<T>;
publishBehavior?: (value: any) => ConnectableObservable<T>;
publishReplay?: (bufferSize: number, windowTime: number, scheduler?: Scheduler) => ConnectableObservable<T>;
reduce?: <R>(project: (acc: R, x: T) => R, acc?: R) => Observable<R>;
reduce?: <T, R>(project: (acc: R, x: T) => R, seed?: R) => Observable<R>;
repeat?: <T>(count: number) => Observable<T>;
retry?: <T>(count: number) => Observable<T>;
retryWhen?: (notifier: (errors: Observable<any>) => Observable<any>) => Observable<T>;
Expand Down Expand Up @@ -81,4 +81,4 @@ export interface CoreOperators<T> {
withLatestFrom?: <R>(...observables: (Observable<any> | ((...values: Array<any>) => R)) []) => Observable<R>;
zip?: <R>(...observables: (Observable<any> | ((...values: Array<any>) => R)) []) => Observable<R>;
zipAll?: <R>(project?: (...values: Array<any>) => R) => Observable<R>;
}
}
4 changes: 2 additions & 2 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default class Observable<T> implements CoreOperators<T> {
publish: () => ConnectableObservable<T>;
publishBehavior: (value: any) => ConnectableObservable<T>;
publishReplay: (bufferSize: number, windowTime: number, scheduler?: Scheduler) => ConnectableObservable<T>;
reduce: <R>(project: (acc: R, x: T) => R, acc?: R) => Observable<R>;
reduce: <T, R>(project: (acc: R, x: T) => R, seed?: R) => Observable<R>;
repeat: <T>(count: number) => Observable<T>;
retry: <T>(count: number) => Observable<T>;
retryWhen: (notifier: (errors: Observable<any>) => Observable<any>) => Observable<T>;
Expand Down Expand Up @@ -229,4 +229,4 @@ export default class Observable<T> implements CoreOperators<T> {
withLatestFrom: <R>(...observables: Array<Observable<any> | ((...values: Array<any>) => R)>) => Observable<R>;
zip: <R>(...observables: Array<Observable<any> | ((...values: Array<any>) => R)>) => Observable<R>;
zipAll: <R>(project?: (...values: Array<any>) => R) => Observable<R>;
}
}
6 changes: 3 additions & 3 deletions src/observables/ScalarObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ proto.filter = function <T>(select: (x: T, ix?: number) => boolean, thisArg?: an
}
};

proto.reduce = function <T, R>(project: (acc: R, x: T) => R, acc?: R): Observable<R> {
if (typeof acc === 'undefined') {
proto.reduce = function <T, R>(project: (acc: R, x: T) => R, seed?: R): Observable<R> {
if (typeof seed === 'undefined') {
return <any>this;
}
let result = tryCatch(project)(acc, this.value);
let result = tryCatch(project)(seed, this.value);
if (result === errorObject) {
return new ErrorObservable(errorObject.e);
} else {
Expand Down
15 changes: 5 additions & 10 deletions src/operators/reduce-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import {errorObject} from '../util/errorObject';

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

acc: R;
project: (acc: R, x: T) => R;

constructor(project: (acc: R, x: T) => R, acc?: R) {
this.acc = acc;
this.project = project;
constructor(private project: (acc: R, x: T) => R, private seed?: R) {
}

call(subscriber: Subscriber<T>): Subscriber<T> {
return new ReduceSubscriber(subscriber, this.project, this.acc);
return new ReduceSubscriber(subscriber, this.project, this.seed);
}
}

Expand All @@ -26,11 +21,11 @@ export class ReduceSubscriber<T, R> extends Subscriber<T> {
hasValue: boolean = false;
project: (acc: R, x: T) => R;

constructor(destination: Subscriber<T>, project: (acc: R, x: T) => R, acc?: R) {
constructor(destination: Subscriber<T>, project: (acc: R, x: T) => R, seed?: R) {
super(destination);
this.acc = acc;
this.acc = seed;
this.project = project;
this.hasSeed = typeof acc !== 'undefined';
this.hasSeed = typeof seed !== 'undefined';
}

_next(x) {
Expand Down
4 changes: 2 additions & 2 deletions src/operators/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Observable from '../Observable';
import { ReduceOperator } from './reduce-support';

export default function reduce<T, R>(project: (acc: R, x: T) => R, acc?: R): Observable<R> {
return this.lift(new ReduceOperator(project, acc));
export default function reduce<T, R>(project: (acc: R, x: T) => R, seed?: R): Observable<R> {
return this.lift(new ReduceOperator(project, seed));
}

0 comments on commit e96d810

Please sign in to comment.