首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用rxcpp对聚合函数进行分组?

使用rxcpp对聚合函数进行分组?
EN

Stack Overflow用户
提问于 2016-01-05 05:05:33
回答 1查看 605关注 0票数 2

我正试图获得RxCpp的要点,这是微软的反应式扩展的原生cpp实现,看看我是否可以在项目中使用它,但我很难理解这些概念。

如果我有一个可观察到的模板,其结构如下:

代码语言:javascript
复制
struct Person
{
    std::string name;
    std::string sex;
    int age;
}

我如何创造另一个可观察的,包括按性别分组的人的数量,最小年龄,最大年龄和所有事件的平均年龄?

我看过例子,我看不出如何一次得到多个聚合。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-05 18:25:59

使用group_by按性别划分,然后将最小/最大/平均减速器组合起来,生成每个性别所需的输出。

更新了计数、输出和其他注释

这对我来说很管用:

代码语言:javascript
复制
#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;
}

产生的输出

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34604511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档