MCPcopy Create free account
hub / github.com/AlexInLog/ReactivePlusPlus / main

Function main

src/examples/rpp/linesfrombytes/linesfrombytes.cpp:10–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8#include <vector>
9
10int main() // NOLINT(bugprone-exception-escape)
11{
12 std::random_device rd; // non-deterministic generator
13 std::mt19937 gen(rd());
14 std::uniform_int_distribution<> dist(4, 18);
15
16 // for testing purposes, produce byte stream that from lines of text
17 auto bytes = rpp::source::just(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
18 | rpp::operators::flat_map([&](int i) {
19 auto body = rpp::source::just((uint8_t)('A' + i)) | rpp::operators::repeat(dist(gen));
20 auto delim = rpp::source::just((uint8_t)'\r');
21 return rpp::source::concat(body, delim);
22 })
23 | rpp::operators::window(17)
24 | rpp::operators::flat_map([](auto&& w) {
25 return std::forward<decltype(w)>(w) | rpp::operators::reduce(std::vector<uint8_t>(), [](std::vector<uint8_t> v, uint8_t b) {
26 v.push_back(b);
27 return v;
28 });
29 })
30 | rpp::operators::tap([](const std::vector<uint8_t>& v) {
31 // print input packet of bytes
32 std::copy(v.begin(), v.end(), std::ostream_iterator<long>(std::cout, " "));
33 std::cout << std::endl;
34 });
35
36 //
37 // recover lines of text from byte stream
38 //
39
40 auto removespaces = [](std::string s) {
41 s.erase(std::remove_if(s.begin(), s.end(), &isspace), s.end());
42 return s;
43 };
44
45 // create strings split on \r
46 auto strings = bytes | rpp::operators::map([](std::vector<uint8_t> v) {
47 std::string s(v.begin(), v.end());
48 std::regex delim(R"/(\r)/");
49 std::cregex_token_iterator cursor(&s[0], &s[0] + s.size(), delim, {-1, 0});
50 std::cregex_token_iterator end;
51 std::vector<std::string> splits(cursor, end);
52 return rpp::source::from_iterable(std::move(splits));
53 })
54 | rpp::operators::concat()
55 | rpp::operators::filter([](const std::string& s) {
56 return !s.empty();
57 })
58 | rpp::operators::publish() | rpp::operators::ref_count();
59
60 // filter to last string in each line
61 auto closes = strings | rpp::operators::filter([](const std::string& s) {
62 return s.back() == '\r';
63 })
64 | rpp::operators::map([](const std::string&) { return 0; });
65
66 // group strings by line
67 auto linewindows = strings | rpp::operators::window_toggle(closes | rpp::operators::start_with(0), [=](int) { return closes; });

Callers

nothing calls this directly

Calls 15

justFunction · 0.85
flat_mapFunction · 0.85
repeatFunction · 0.85
windowFunction · 0.85
reduceFunction · 0.85
tapFunction · 0.85
mapFunction · 0.85
from_iterableFunction · 0.85
filterFunction · 0.85
publishFunction · 0.85
ref_countFunction · 0.85
window_toggleFunction · 0.85

Tested by

no test coverage detected