1 #include "rxcpp/rx.hpp" 2 3 #include "rxcpp/rx-test.hpp" 4 #include "catch.hpp" 5 6 SCENARIO("reduce sample"){ 7 printf("//! [reduce sample]\n"); 8 auto values = rxcpp::observable<>::range(1, 7). 9 reduce( 10 std::make_pair(0, 1.0), __anon3d234c200102(std::pair<int, double> seed, int v)11 [](std::pair<int, double> seed, int v){ 12 seed.first += 1; 13 seed.second *= v; 14 return seed; 15 }, __anon3d234c200202(std::pair<int, double> res)16 [](std::pair<int, double> res){ 17 return std::pow(res.second, 1.0 / res.first); 18 }); 19 values. 20 subscribe( __anon3d234c200302(double v)21 [](double v){printf("OnNext: %lf\n", v);}, __anon3d234c200402()22 [](){printf("OnCompleted\n");}); 23 printf("//! [reduce sample]\n"); 24 } 25 26 SCENARIO("reduce empty sample"){ 27 printf("//! [reduce empty sample]\n"); 28 auto values = rxcpp::observable<>::empty<int>(). 29 reduce( 30 1, __anon3d234c200502(int,int)31 [](int,int){return 0;}, __anon3d234c200602(int res)32 [](int res){return res;}); 33 values. 34 subscribe( __anon3d234c200702(int v)35 [](int v){printf("OnNext: %d\n", v);}, __anon3d234c200802()36 [](){printf("OnCompleted\n");}); 37 printf("//! [reduce empty sample]\n"); 38 } 39 40 SCENARIO("reduce exception from accumulator sample"){ 41 printf("//! [reduce exception from accumulator sample]\n"); 42 auto values = rxcpp::observable<>::range(1, 3). 43 reduce( 44 0, __anon3d234c200902(int seed, int v)45 [](int seed, int v){ 46 if (v == 2) 47 throw std::runtime_error("Exception from accumulator"); 48 return seed; 49 }, __anon3d234c200a02(int res)50 [](int res){return res;}); 51 values. 52 subscribe( __anon3d234c200b02(int v)53 [](int v){printf("OnNext: %d\n", v);}, __anon3d234c200c02(std::exception_ptr ep)54 [](std::exception_ptr ep){ 55 try {std::rethrow_exception(ep);} 56 catch (const std::exception& ex) { 57 printf("OnError: %s\n", ex.what()); 58 } 59 }, __anon3d234c200d02()60 [](){printf("OnCompleted\n");}); 61 printf("//! [reduce exception from accumulator sample]\n"); 62 } 63 64 SCENARIO("reduce exception from result selector sample"){ 65 printf("//! [reduce exception from result selector sample]\n"); 66 auto values = rxcpp::observable<>::range(1, 3). 67 reduce( 68 0, __anon3d234c200e02(int seed, int v)69 [](int seed, int v){return seed + v;}, __anon3d234c200f02(int res)70 [](int res){ 71 throw std::runtime_error("Exception from result selector"); 72 return res; 73 }); 74 values. 75 subscribe( __anon3d234c201002(int v)76 [](int v){printf("OnNext: %d\n", v);}, __anon3d234c201102(std::exception_ptr ep)77 [](std::exception_ptr ep){ 78 try {std::rethrow_exception(ep);} 79 catch (const std::exception& ex) { 80 printf("OnError: %s\n", ex.what()); 81 } 82 }, __anon3d234c201202()83 [](){printf("OnCompleted\n");}); 84 printf("//! [reduce exception from result selector sample]\n"); 85 } 86