Skip to content
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

Merged
merged 8 commits into from
Jun 11, 2023
Merged

Conversation

CorentinBT
Copy link
Contributor

@CorentinBT CorentinBT commented Jun 10, 2023

Should I also implement a variant taking a ResultSelector like in rxcpp?

{
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)); });
Copy link
Contributor Author

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

Copy link
Owner

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 :)

Copy link
Contributor Author

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

Copy link
Owner

@victimsnino victimsnino Jun 10, 2023

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

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); })
Copy link
Owner

@victimsnino victimsnino Jun 10, 2023

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” ? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@codecov
Copy link

codecov bot commented Jun 10, 2023

Codecov Report

Merging #374 (45e0ec6) into v2 (05a4d05) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            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              
Impacted Files Coverage Δ
src/rpp/rpp/operators/flat_map.hpp 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

RPP_NO_UNIQUE_ADDRESS Fn m_fn;

template<rpp::constraint::observable TObservable>
requires std::invocable<Fn, rpp::utils::extract_observable_type_t<TObservable>>
Copy link
Owner

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

Copy link
Contributor Author

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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

auto operator()(TObservable&& observable) &&
{
return std::forward<TObservable>(observable)
| rpp::ops::map(std::forward<Fn>(m_fn))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be move

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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>>
Copy link
Owner

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

Copy link
Contributor Author

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

@@ -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>>
Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 126 to 127
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
Copy link
Owner

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)); })

Copy link
Contributor Author

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?

Copy link
Contributor Author

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

Copy link
Owner

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 =)

CHECK(mock.get_on_error_count() == 0);
}
}
SECTION("subscribe using flat_map with error in middle")
Copy link
Owner

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 =)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}

TEST_CASE("flat_map copies/moves")
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah agree, done

victimsnino
victimsnino previously approved these changes Jun 11, 2023
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 &
Copy link
Owner

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@victimsnino victimsnino merged commit 2e54fc4 into victimsnino:v2 Jun 11, 2023
@victimsnino
Copy link
Owner

Thanks for PR =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants