June 2018
Intermediate to advanced
348 pages
8h 45m
English
The scan Operator applies a function on each element of a Stream sequentially and accumulates the value into a seed value. The following program produces average of a series of numbers as and when the values are accumulated:
//----------- Scan.cpp
#include "rxcpp/rx.hpp"
#include "rxcpp/rx-test.hpp"
#include <ioStream>
int main() {
int count = 0;
auto values = rxcpp::observable<>::range(1, 20).
scan( 0,[&count](int seed, int v){
count++;
return seed + v;
});
values.subscribe(
[&](int v){printf("Average through Scan: %fn", (double)v/count);},
[](){printf("OnCompletedn");});
}
The running average will be printed on to the console. OnNext functor will be called twenty times before OnCompleted is called.
Read now
Unlock full access