-
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f58a84
Implement flat_map operator
a748a6f
Address comments
bd37f38
Address comments
47e25dd
Remove whitespace
9e99db8
Added comments
aba97fa
add marble
victimsnino 627a88e
Address comments
45e0ec6
Increase code coverage
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// ReactivePlusPlus library | ||
// | ||
// Copyright Aleksey Loginov 2023 - present. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
// Project home: https://github.com/victimsnino/ReactivePlusPlus | ||
// | ||
|
||
#pragma once | ||
|
||
#include <rpp/operators/map.hpp> | ||
#include <rpp/operators/merge.hpp> | ||
#include <rpp/defs.hpp> | ||
|
||
namespace rpp::operators::details | ||
{ | ||
|
||
template<rpp::constraint::decayed_type Fn> | ||
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>> | ||
&& rpp::constraint::observable<std::invoke_result_t<Fn, rpp::utils::extract_observable_type_t<TObservable>>>) | ||
auto operator()(TObservable&& observable) const & | ||
{ | ||
return std::forward<TObservable>(observable) | ||
| rpp::ops::map(m_fn) | ||
| rpp::ops::merge(); | ||
} | ||
|
||
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) && | ||
{ | ||
return std::forward<TObservable>(observable) | ||
| rpp::ops::map(std::move(m_fn)) | ||
| rpp::ops::merge(); | ||
} | ||
}; | ||
|
||
} // namespace rpp::operators::details | ||
|
||
namespace rpp::operators | ||
{ | ||
|
||
/** | ||
* @brief Transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable | ||
* | ||
* @marble flat_map | ||
{ | ||
source observable : +--1--2--3--| | ||
operator "flat_map: x=>just(x,x+1)" : +--12-23-34-| | ||
} | ||
* | ||
* @details Actually it makes `map(callable)` and then `merge`. | ||
* @details Note that flat_map merges the emissions of these Observables, so that they may interleave. | ||
* | ||
* @param a function that returns an observable for each item emitted by the source observable. | ||
* @warning #include <rpp/operators/flat_map.hpp> | ||
* | ||
* @ingroup transforming_operators | ||
* @see https://reactivex.io/documentation/operators/flatmap.html | ||
*/ | ||
template<typename Fn> | ||
requires (!utils::is_not_template_callable<Fn> || rpp::constraint::observable<std::invoke_result_t<Fn, utils::convertible_to_any>>) | ||
auto flat_map(Fn&& callable) | ||
{ | ||
return details::flat_map_t<std::decay_t<Fn>>{std::forward<Fn>(callable)}; | ||
} | ||
|
||
} // namespace rpp::operators |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// ReactivePlusPlus library | ||
// | ||
// Copyright Aleksey Loginov 2023 - present. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
// Project home: https://github.com/victimsnino/ReactivePlusPlus | ||
// | ||
|
||
#include <snitch/snitch.hpp> | ||
#include <snitch/snitch_macros_check.hpp> | ||
|
||
#include <rpp/operators/flat_map.hpp> | ||
#include <rpp/sources/create.hpp> | ||
#include <rpp/sources/just.hpp> | ||
#include <rpp/sources/empty.hpp> | ||
#include <rpp/sources/error.hpp> | ||
#include <rpp/sources/never.hpp> | ||
#include <rpp/schedulers/immediate.hpp> | ||
|
||
#include "copy_count_tracker.hpp" | ||
#include "mock_observer.hpp" | ||
|
||
#include <stdexcept> | ||
#include <string> | ||
|
||
TEMPLATE_TEST_CASE("flat_map", "", rpp::memory_model::use_stack, rpp::memory_model::use_shared) | ||
{ | ||
auto mock = mock_observer_strategy<int>(); | ||
SECTION("observable of items") | ||
{ | ||
auto obs = rpp::source::just<TestType>(rpp::schedulers::immediate{}, 1, 2, 3); | ||
|
||
SECTION("subscribe using flat_map with templated lambda") | ||
{ | ||
obs | rpp::operators::flat_map([](auto v) { return rpp::source::just(v * 2); }) | ||
| rpp::ops::subscribe(mock.get_observer()); | ||
SECTION("observer obtains values from underlying observables") | ||
{ | ||
CHECK(mock.get_received_values() == std::vector{ 2, 4, 6 }); | ||
CHECK(mock.get_on_completed_count() == 1); | ||
CHECK(mock.get_on_error_count() == 0); | ||
} | ||
} | ||
SECTION("subscribe using flat_map with error") | ||
{ | ||
obs | rpp::operators::flat_map([](int) { return rpp::source::error<int>(std::make_exception_ptr(std::runtime_error{""})); }) | ||
| rpp::ops::subscribe(mock.get_observer()); | ||
SECTION("observer obtains values from underlying observables") | ||
{ | ||
CHECK(mock.get_total_on_next_count() == 0); | ||
CHECK(mock.get_on_completed_count() == 0); | ||
CHECK(mock.get_on_error_count() == 1); | ||
} | ||
} | ||
SECTION("subscribe using flat_map with empty") | ||
{ | ||
obs | rpp::operators::flat_map([](int) { return rpp::source::empty<int>(); }) | ||
| rpp::ops::subscribe(mock.get_observer()); | ||
SECTION("observer obtains values from underlying observables") | ||
{ | ||
CHECK(mock.get_total_on_next_count() == 0); | ||
CHECK(mock.get_on_completed_count() == 1); | ||
CHECK(mock.get_on_error_count() == 0); | ||
} | ||
} | ||
SECTION("subscribe using flat_map with never") | ||
{ | ||
obs | rpp::operators::flat_map([](int) { return rpp::source::never<int>(); }) | ||
| rpp::ops::subscribe(mock.get_observer()); | ||
SECTION("observer obtains values from underlying observables") | ||
{ | ||
CHECK(mock.get_total_on_next_count() == 0); | ||
CHECK(mock.get_on_completed_count() == 0); | ||
CHECK(mock.get_on_error_count() == 0); | ||
} | ||
} | ||
SECTION("subscribe using flat_map with empty in middle") | ||
{ | ||
obs | rpp::operators::flat_map([](int v) { | ||
if (v == 2) | ||
return rpp::source::empty<int>().as_dynamic(); | ||
return rpp::source::just(v).as_dynamic(); | ||
}) | ||
| rpp::ops::subscribe(mock.get_observer()); | ||
SECTION("observer obtains values from underlying observables") | ||
{ | ||
CHECK(mock.get_received_values() == std::vector{ 1, 3 }); | ||
CHECK(mock.get_on_completed_count() == 1); | ||
CHECK(mock.get_on_error_count() == 0); | ||
} | ||
} | ||
SECTION("subscribe using flat_map with never in middle") | ||
{ | ||
obs | rpp::operators::flat_map([](int v) { | ||
if (v == 2) | ||
return rpp::source::never<int>().as_dynamic(); | ||
return rpp::source::just(v).as_dynamic(); | ||
}) | ||
| rpp::ops::subscribe(mock.get_observer()); | ||
SECTION("observer obtains values from underlying observables") | ||
{ | ||
CHECK(mock.get_received_values() == std::vector{ 1, 3 }); | ||
CHECK(mock.get_on_completed_count() == 0); | ||
CHECK(mock.get_on_error_count() == 0); | ||
} | ||
} | ||
SECTION("subscribe using flat_map with error in middle") | ||
{ | ||
obs | rpp::operators::flat_map([](int v) { | ||
if (v == 2) | ||
return rpp::source::error<int>(std::make_exception_ptr(std::runtime_error{""})).as_dynamic(); | ||
return rpp::source::just(v).as_dynamic(); | ||
}) | ||
| rpp::ops::subscribe(mock.get_observer()); | ||
SECTION("observer obtains values from underlying observables") | ||
{ | ||
CHECK(mock.get_total_on_next_count() == 0); | ||
CHECK(mock.get_on_completed_count() == 0); | ||
CHECK(mock.get_on_error_count() == 1); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
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