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
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/benchmarks/benchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ int main(int argc, char* argv[]) // NOLINT
| rxcpp::operators::subscribe<int>([](int v){ ankerl::nanobench::doNotOptimizeAway(v); });
});
}
SECTION("create+flat_map(just(v*2))+subscribe")
{
TEST_RPP([&]()
{
rpp::source::create<int>([](const auto& obs){ obs.on_next(1); })
| rpp::operators::flat_map([](int v) { return rpp::source::create<int>([v](const auto& obs){ obs.on_next(v * 2); }); })
| rpp::operators::subscribe([](int v){ ankerl::nanobench::doNotOptimizeAway(v); });
});

TEST_RXCPP([&]()
{
rxcpp::observable<>::create<int>([](const auto& obs){obs.on_next(1);})
| rxcpp::operators::flat_map([](int v) { return rxcpp::observable<>::create<int>([v](const auto& obs){ obs.on_next(v * 2); }); })
| rxcpp::operators::subscribe<int>([](int v){ ankerl::nanobench::doNotOptimizeAway(v); });
});
}
};

BENCHMARK("Filtering Operators")
Expand Down
1 change: 1 addition & 0 deletions src/rpp/rpp/operators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @ingroup operators
*/

#include <rpp/operators/flat_map.hpp>
#include <rpp/operators/map.hpp>
#include <rpp/operators/scan.hpp>
#include <rpp/operators/subscribe.hpp>
Expand Down
76 changes: 76 additions & 0 deletions src/rpp/rpp/operators/flat_map.hpp
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 &
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

{
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
4 changes: 4 additions & 0 deletions src/rpp/rpp/operators/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (!utils::is_not_template_callable<Fn> || rpp::constraint::observable<std::invoke_result_t<Fn, utils::convertible_to_any>>)
auto flat_map(Fn&& callable);

template<rpp::constraint::observable TObservable, rpp::constraint::observable... TObservables>
requires constraint::observables_of_same_type<std::decay_t<TObservable>, std::decay_t<TObservables>...>
auto merge_with(TObservable&& observable, TObservables&& ...observables);
Expand Down
125 changes: 125 additions & 0 deletions src/tests/rpp/test_flat_map.cpp
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);
}
}
}
}