ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
group_by.cpp
#include <rpp/rpp.hpp>
#include <iostream>
int main()
{
rpp::source::just(1, 2, 3, 4, 5, 6, 7, 8)
| rpp::operators::group_by([](int v) { return v % 2 == 0; })
| rpp::operators::subscribe([](auto grouped_observable) {
auto key = grouped_observable.get_key();
std::cout << "new grouped observable " << key << std::endl;
grouped_observable.subscribe([key](int val) {
std::cout << "key [" << key << "] Val: " << val << std::endl;
});
});
// Output: new grouped observable 0
// key [0] Val: 1
// new grouped observable 1
// key [1] Val: 2
// key [0] Val: 3
// key [1] Val: 4
// key [0] Val: 5
// key [1] Val: 6
// key [0] Val: 7
// key [1] Val: 8
//
struct person
{
std::string name;
int age;
};
rpp::source::just(person{"Kate", 18},
person{"Alex", 25},
person{"Nick", 18},
person{"Jack", 25},
person{"Tom", 30},
person{"Vanda", 18})
| rpp::operators::group_by([](const person& v) { return v.age; }, [](const person& v) { return v.name; })
| rpp::operators::subscribe([](auto grouped_observable) {
grouped_observable.subscribe([age = grouped_observable.get_key()](const std::string& name) {
std::cout << "Age [" << age << "] Name: " << name << std::endl;
});
});
// Output: Age [18] Name: Kate
// Age [25] Name: Alex
// Age [18] Name: Nick
// Age [25] Name: Jack
// Age [30] Name: Tom
// Age [18] Name: Vanda
return 0;
}
auto just(const TScheduler &scheduler, T &&item, Ts &&... items)
Creates rpp::observable that emits a particular items and completes.
Definition from.hpp:201
auto group_by(KeySelector &&key_selector, ValueSelector &&value_selector={}, KeyComparator &&comparator={})
Divide original observable into multiple observables where each new observable emits some group of va...
Definition group_by.hpp:213
auto subscribe(observer< Type, ObserverStrategy > &&observer)
Subscribes passed observer to emissions from this observable.
Definition subscribe.hpp:226