-
Notifications
You must be signed in to change notification settings - Fork 397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] buffer with predicate which determines when buffer must be flushed #369
Comments
The // filter to last string in each line
auto closes = strings |
filter(
[](const string& s){
return s.back() == '\r';
}) |
Rx::map([](const string&){return 0;});
// group strings by line
auto linewindows = strings |
window_toggle(closes | start_with(0), [=](int){return closes;}); Partially applied to this case: auto source = rxcpp::observable<>::iterate(
std::initializer_list<char>{ '1', '2', '3', '$', '5', '$', '7', '8', '$' }
) |
// share
publish() |
ref_count();
// filter to flushes
auto flushes = source |
filter(
[](char c){
return c == '$';
}) |
Rx::map([](const string&){return 0;});
// group
auto windows = source |
window_toggle(flushes | start_with(0), [=](int){return flushes;});
auto vectors = windows |
map([](observable<char> w){
return w |
filter(. . .) | // filter out '$'
reduce(. . .); // push_back each char into a vector.
}); |
Thank you so much, Kirk! You are kind wizard. But I cannot make it works with infinity character sequense:
I got the following:
Any hints? |
I think that the fix is to share the source auto source = rxcpp::observable<>::interval(std::chrono::milliseconds(50), rxcpp::observe_on_new_thread())
.map([](long i) {
if (!(i % 10))
return (char)'$';
return (char)('A' + i);
}) |
// share
publish() |
ref_count(); this should let you remove the mutex m as well. at the moment every subscribe to the source is starting a new thread and emitting all the chars on each thread. this leads to some coordination issues in the window_toggle operator, since it was not given a thread-safe scheduler to use to coordinate the values from different threads. It also means that the '$' from one thread is being used to close the sequence from a different thread. |
Thank you, Kirk! As the result window_toggle is not suitable to solve this task. The following does not work:
I got only one vector and my program is hanged:
As for me the best solution in that case will be buffer with predicate. |
I finally have a fix for an async lifetime bug in A few improvements to the code as well.
auto source = rxcpp::observable<>::interval(std::chrono::milliseconds(50))
.map([](long i) {
if (!(i % 10))
return (char)'$';
return (char)('A' + i);
})
.publish()
.ref_count();
// filter to flushes
auto flushes = source |
rxcpp::operators::filter(
[](char ch) {
return ch == '$';
}) |
rxcpp::operators::map([](char ) {
return 0;
});
auto windows = source |
rxcpp::operators::window_toggle(flushes | rxcpp::operators::start_with(0), [=](int) {
return flushes;
});
auto vectors = windows |
rxcpp::operators::map([](rxcpp::observable<char> w) {
return w |
// filter out '$'
rxcpp::operators::filter([](char ch) {
return ch != '$';
}) |
// reduce to vector
rxcpp::operators::reduce(
std::vector<char>(),
[](std::vector<char> v, char ch) {
v.push_back(ch);
return v;
}) |
rxcpp::operators::as_dynamic();
}) |
merge();
vectors
.take(3)
.subscribe([](const std::vector<char> &v) {
std::cout << "vector: ";
std::copy(v.begin(), v.end(), std::ostream_iterator<char>(std::cout, " "));
std::cout << std::endl;
}); with the bug fix this results in $ ./delimited
vector: B C D E F G H I J
vector: L M N O P Q R S T
vector: V W X Y Z [ \ ] ^
$
Adding |
Thank you so much, Kirk! This is very good news. |
Thank you for pointing out the issues in |
With all respect to the 'creative solution', readable and understandable code looks different, so something like a |
Hello!
I have simple example with character stream:
I want to get std::vector for each sub sequence terminated by '$'. I know that there is buffer operator ( http://reactivex.io/documentation/operators/buffer.html ) with count and time overloads. But what about buffer with predicate which determines when buffer must be flushed?
Now I have the following workaround:
I am newcomer, so may be you point me more correct and elegant RX way to solve my task.
Thanks in advance,
Anatoly Shirokov
The text was updated successfully, but these errors were encountered: