Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { test } from 'ava';
- import * as Rx from 'rxjs';
- import { List } from 'immutable';
- class State {
- private constructor(private lastN: List<number>, private num: number) {}
- public static make(num: number): State { return new State(List(), num); }
- public push(a: number): State {
- const newLast = List(this.lastN.push(a).slice(-this.num));
- return new State(newLast, this.num);
- }
- public avg(): number {
- const sum = this.lastN.reduce((a, b) => a! + b!, 0);
- return sum / this.num;
- }
- }
- test('feedback', async (t) => {
- const state = new Rx.Subject<State>();
- const avgs = [] as number[];
- const c = Rx.Observable.interval(100)
- .withLatestFrom(state)
- .map(([n, s]) => [n, s.push(n)] as [number, State])
- .do(([_, s]) => state.next(s))
- .take(6)
- .forEach(([_, s]) => avgs.push(s.avg()));
- // Provide first value to withLatestFrom.
- state.next(State.make(8));
- await c;
- t.deepEqual(avgs, [0, 0.25, 0.75, 1.5, 2.5, 3.5, 4.5, 5.5]);
- });
Add Comment
Please, Sign In to add comment