-
Notifications
You must be signed in to change notification settings - Fork 31
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
Implement flat_map operator #374
Conversation
src/tests/rpp/test_flat_map.cpp
Outdated
{ | ||
copy_count_tracker verifier{}; | ||
auto obs = rpp::source::just<TestType>(std::move(verifier)) | ||
| rpp::ops::flat_map([](copy_count_tracker&& verifier) { return rpp::source::just<TestType>(std::move(verifier)); }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@victimsnino do you know why sources don't move objects through operators? Preventing me from taking verifier as an rvalue ref here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it would be moved IF possible, but no guarantees. If it would be only pure rvalue, then it means second repeated subscribe would see nothing :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I added some cases for copies then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you would use create with std::move inside, then it would be moved inside lambda
src/benchmarks/benchmarks.cpp
Outdated
TEST_RPP([&]() | ||
{ | ||
rpp::source::create<int>([](const auto& obs){ obs.on_next(1); }) | ||
| rpp::operators::flat_map([](int v) { return rpp::source::just(v * 2); }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use “create” there instead of just to measure there only “flat_map”, not “just” ? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Codecov Report
@@ Coverage Diff @@
## v2 #374 +/- ##
==========================================
+ Coverage 99.12% 99.13% +0.01%
==========================================
Files 37 38 +1
Lines 802 812 +10
==========================================
+ Hits 795 805 +10
Misses 7 7
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
src/rpp/rpp/operators/flat_map.hpp
Outdated
RPP_NO_UNIQUE_ADDRESS Fn m_fn; | ||
|
||
template<rpp::constraint::observable TObservable> | ||
requires std::invocable<Fn, rpp::utils::extract_observable_type_t<TObservable>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also invocation result must be observable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is already required here isn't it enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, check this comment
https://github.com/victimsnino/ReactivePlusPlus/pull/374/files#r1225415597
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/rpp/rpp/operators/flat_map.hpp
Outdated
auto operator()(TObservable&& observable) && | ||
{ | ||
return std::forward<TObservable>(observable) | ||
| rpp::ops::map(std::forward<Fn>(m_fn)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be move
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/rpp/rpp/operators/fwd.hpp
Outdated
@@ -33,6 +33,10 @@ template<typename Fn> | |||
requires (!utils::is_not_template_callable<Fn> || !std::same_as<void, std::invoke_result_t<Fn, utils::convertible_to_any>>) | |||
auto map(Fn&& callable); | |||
|
|||
template<typename Fn> | |||
requires rpp::constraint::observable<std::invoke_result_t<Fn, utils::convertible_to_any>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add check over !utils::is_not_template_callable as in map. It needed when passed function is purely template. For example [](auto v) {return rpp::source::just(v); }
wouldn't satisfy your requirement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, added a test too
src/rpp/rpp/operators/flat_map.hpp
Outdated
@@ -22,8 +22,8 @@ struct flat_map_t | |||
{ | |||
RPP_NO_UNIQUE_ADDRESS Fn m_fn; | |||
|
|||
template<rpp::constraint::observable TObservable> | |||
requires std::invocable<Fn, rpp::utils::extract_observable_type_t<TObservable>> | |||
template<rpp::constraint::observable TObservable, typename ValueType = rpp::utils::extract_observable_type_t<TObservable>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your attempt with ValueType to avoid duplicates, but it would affect final binaries due to it would be compiled-in as template function with 2 params instead of one =C it is better to create special concept instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/tests/rpp/test_flat_map.cpp
Outdated
@@ -105,22 +106,43 @@ TEMPLATE_TEST_CASE("flat_map", "", rpp::memory_model::use_stack, rpp::memory_mod | |||
CHECK(mock.get_on_error_count() == 1); | |||
} | |||
} | |||
SECTION("subscribe using flat_map with templated lambda") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think, you can just place it in a748a6f#diff-4ad61bca345fc2ef117d5617e164dca71340d801c636340f2c051a5bd4e6e9d2R37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/tests/rpp/test_flat_map.cpp
Outdated
auto obs = rpp::source::create<copy_count_tracker>([verifier = std::move(verifier)](const auto& obs) { obs.on_next(verifier); }) | ||
| rpp::ops::map([](copy_count_tracker verifier) { return std::move(verifier); }) // copy from source to map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try instead
auto obs = rpp::source::create<copy_count_tracker>([verifier = std::move(verifier)](const auto& obs) { obs.on_next(std::move(verifier)); })
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I subscribe two times here, so verifier will be in an invalid state in second subscription as it was already moved no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed to capture by ref and pass by value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I subscribe two times here, so verifier will be in an invalid state in second subscription as it was already moved no?
No, verifier is special class that is valid after move =)
src/tests/rpp/test_flat_map.cpp
Outdated
CHECK(mock.get_on_error_count() == 0); | ||
} | ||
} | ||
SECTION("subscribe using flat_map with error in middle") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add test over never in middle? to check that it is actually map + merge, not map + concat =)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/tests/rpp/test_flat_map.cpp
Outdated
} | ||
} | ||
|
||
TEST_CASE("flat_map copies/moves") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, you can remove this test_case due to you are trying to test map + test merge at the same time, it is pretty hard. It is better to have corresponding tests to map and merge. I'm going to add them later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah agree, done
template<rpp::constraint::observable TObservable> | ||
requires (std::invocable<Fn, rpp::utils::extract_observable_type_t<TObservable>> | ||
&& rpp::constraint::observable<std::invoke_result_t<Fn, rpp::utils::extract_observable_type_t<TObservable>>>) | ||
auto operator()(TObservable&& observable) const & |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add test to cover this overloading? (it is enough to apply operator from variable in any test case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Thanks for PR =) |
Should I also implement a variant taking a
ResultSelector
like in rxcpp?