BACK
ПрактикаInterview: mergeScan() async accumulator
ПРАКТИКА · 52 · 練

Interview: mergeScan() async accumulator

Накопите баланс банковских операций через mergeScan.

  1. Решите задачу в редакторе без длинной теории.
  2. Запустите проверку и сравните вывод с ожидаемым.
  3. Если застряли, откройте решение и перенесите подход в свой код.
Решение spoiler · click to reveal
const { from, of, mergeScan } = Rx;

const operations = [100, -20, 50];

const result$ = from(operations).pipe(
  mergeScan((balance, change) => of(balance + change), 0)
);

result$.subscribe(value => console.log(value));
script.ts // TypeScript
CONSOLE · Console Output
Нажмите на запуск, чтобы увидеть результат...