【问题标题】:Groups aggregate functions using rxcpp?使用 rxcpp 对函数进行分组?
【发布时间】:2016-01-05 05:05:33
【问题描述】:

我正在尝试了解 RxCpp(来自 Microsoft 的反应式扩展的本机 cpp 实现)的要点,以查看是否可以在项目中使用它,但我无法理解这些概念。

如果我有一个具有以下结构的可观察模板:

struct Person
{
    std::string name;
    std::string sex;
    int age;
}

如何创建另一个包含按性别分组的可观察对象,其中包含所有事件的人数、最小年龄、最大年龄和平均年龄?

我查看了示例,但不知道如何一次获取多个聚合。

【问题讨论】:

    标签: c++ reactive-programming rxcpp


    【解决方案1】:

    使用 group_by 按性别进行分区,然后结合 min/max/average reducer 以产生每个性别所需的输出。

    更新了计数、输出和其他 cmets

    这对我有用:

    #include "rxcpp/rx.hpp"
    using namespace rxcpp;
    using namespace rxcpp::sources;
    using namespace rxcpp::subjects;
    using namespace rxcpp::util;
    
    using namespace std;
    
    struct Person
    {
        string name;
        string gender;
        int age;
    };
    
    int main()
    {
        subject<Person> person$;
    
        // group ages by gender
        auto agebygender$ = person$.
            get_observable().
            group_by(
                [](Person& p) { return p.gender;},
                [](Person& p) { return p.age;});
    
        // combine min max and average reductions.
        auto result$ = agebygender$.
            map([](grouped_observable<string, int> gp$){
                // the function passed to combine_latest 
                // will be called once all the source streams
                // (count, min, max, average) have produced a 
                // value. in this case, all the streams are reducers
                // that produce only one value when gp$ completes.
                // thus the function is only called once per gender 
                // with the final value of each stat.
                return gp$.
                    count().
                    combine_latest(
                        [=](int count, int min, int max, double average){
                            return make_tuple(gp$.get_key(), count, min, max, average);
                        },
                        gp$.min(),
                        gp$.max(),
                        gp$.map([](int age) -> double { return age;}).average());
            }).
            // this map() returns observable<observable<tuple<string, int, int, int, double>>>
            // the merge() returns observable<tuple<string, int, int, int, double>>
            // a grouped observable is 'hot' if it is not subscribed to immiediatly (in this case by merge)
            // then the values sent to it are lost.
            merge();
    
        // display results
        result$.
            subscribe(apply_to([](string gender, int count, int min, int max, double avg){
                cout << gender << ": count = " << count << ", range = [" << min << "-" << max << "], avg = " << avg << endl;
            }));
    
        //provide input data
        observable<>::from(
            Person{"Tom", "Male", 32},
            Person{"Tim", "Male", 12},
            Person{"Stel", "Other", 42},
            Person{"Flor", "Female", 24},
            Person{"Fran", "Female", 97}).
            subscribe(person$.get_subscriber());
    
        return 0;
    }
    

    结果输出

    Female: count = 2, range = [24-97], avg = 60.5
    Male: count = 2, range = [12-32], avg = 22
    Other: count = 1, range = [42-42], avg = 42
    

    【讨论】:

    • 谢谢,看起来很棒。几个问题:为什么必须在 group_by 的第一个参数中使用 [=] 和可变的?你能在结合了最小值最大值和平均值的部分添加一些额外的 cmets 吗?我不太明白那里发生了什么。最后,很明显你对 rxxcpp 很了解,因为你在这个库上工作过,有什么学习如何有效使用它的技巧吗?
    • [=] 捕获和可变是前一个示例的产物,我已将它们删除并添加了更多 cmets。使用 rxcpp 所需的大多数概念对于其他 Rx 实现是通用的。这很好,因为其他资源可以重复使用。这个页面是我如何将自己暴露于这些共享概念:reactivex.io/learnrx。我很高兴讨论 rxcpp 的具体问题。其他资源是:文档 - reactive-extensions.github.io/RxCpp 和聊天 - gitter.im/Reactive-Extensions/RxCpp
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2018-12-16
    • 2019-05-24
    • 2017-11-01
    • 1970-01-01
    相关资源
    最近更新 更多