• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "rxcpp/rx.hpp"
2 
3 #include "rxcpp/rx-test.hpp"
4 #include "catch.hpp"
5 
6 #include <sstream>
7 
8 SCENARIO("group_by sample"){
9     printf("//! [group_by sample]\n");
10     auto values = rxcpp::observable<>::range(0, 8).
11         group_by(
__anon3d9165160102(int v)12             [](int v){return v % 3;},
__anon3d9165160202(int v)13             [](int v){return 10 * v;});
14     values.
15         subscribe(
__anon3d9165160302(rxcpp::grouped_observable<int, int> g)16             [](rxcpp::grouped_observable<int, int> g){
17                 auto key = g.get_key();
18                 printf("OnNext: key = %d\n", key);
19                 g.subscribe(
20                     [key](int v){printf("[key %d] OnNext: %d\n", key, v);},
21                     [key](){printf("[key %d] OnCompleted\n", key);});
22             },
__anon3d9165160602()23             [](){printf("OnCompleted\n");});
24     printf("//! [group_by sample]\n");
25 }
26 
27 //! [group_by full intro]
less(int v1,int v2)28 static bool less(int v1, int v2){
29     return v1 < v2;
30 }
31 //! [group_by full intro]
32 
33 SCENARIO("group_by full sample"){
34     printf("//! [group_by full sample]\n");
35     auto data = rxcpp::observable<>::range(0, 8).
__anon3d9165160702(int v)36         map([](int v){
37             std::stringstream s;
38             s << "Value " << v;
39             return std::make_pair(v % 3, s.str());
40         });
41     auto values = data.group_by(
__anon3d9165160802(std::pair<int, std::string> v)42             [](std::pair<int, std::string> v){return v.first;},
__anon3d9165160902(std::pair<int, std::string> v)43             [](std::pair<int, std::string> v){return v.second;},
44             less);
45     values.
46         subscribe(
__anon3d9165160a02(rxcpp::grouped_observable<int, std::string> g)47             [](rxcpp::grouped_observable<int, std::string> g){
48                 auto key = g.get_key();
49                 printf("OnNext: key = %d\n", key);
50                 g.subscribe(
51                     [key](const std::string& v){printf("[key %d] OnNext: %s\n", key, v.c_str());},
52                     [key](){printf("[key %d] OnCompleted\n", key);});
53             },
__anon3d9165160d02()54             [](){printf("OnCompleted\n");});
55     printf("//! [group_by full sample]\n");
56 }
57