ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
flat_map.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2023 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/victimsnino/ReactivePlusPlus
9//
10
11#pragma once
12
13#include <rpp/defs.hpp>
14#include <rpp/operators/map.hpp>
15#include <rpp/operators/merge.hpp>
16
17namespace rpp::operators::details
18{
19 template<rpp::constraint::decayed_type Fn>
21 {
22 RPP_NO_UNIQUE_ADDRESS Fn m_fn;
23
24 template<rpp::constraint::observable TObservable>
25 auto operator()(TObservable&& observable) const &
26 {
27 static_assert(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>>>, "fn should return observable");
28 return std::forward<TObservable>(observable)
29 | rpp::ops::map(m_fn)
31 }
32
33 template<rpp::constraint::observable TObservable>
34 auto operator()(TObservable&& observable) &&
35 {
36 static_assert(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>>>, "fn should return observable");
37 return std::forward<TObservable>(observable)
38 | rpp::ops::map(std::move(m_fn))
40 }
41 };
42
43} // namespace rpp::operators::details
44
45namespace rpp::operators
46{
47
64 * @see https://reactivex.io/documentation/operators/flatmap.html
65 */
66 template<typename Fn>
68 auto flat_map(Fn&& callable)
69 {
70 return details::flat_map_t<std::decay_t<Fn>>{std::forward<Fn>(callable)};
71 }
72
73} // namespace rpp::operators
Base class for any observable used in RPP. It handles core callbacks of observable.
Definition observable.hpp:38
Definition fwd.hpp:80
Definition function_traits.hpp:45
auto merge()
Converts observable of observables of items into observable of items via merging emissions.
Definition merge.hpp:221
auto map(Fn &&callable)
Transforms the items emitted by an Observable via applying a function to each item and emitting resul...
Definition map.hpp:94
auto flat_map(Fn &&callable)
Transform the items emitted by an Observable into Observables, then flatten the emissions from those ...
Definition flat_map.hpp:64
Definition flat_map.hpp:21