ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.07k stars 396 forks source link

Coverting observable<T> to observable<list<T>> #155

Closed hasyimibhar closed 9 years ago

hasyimibhar commented 9 years ago

What's the equivalent of RxJava's toList in RxCpp?

auto numbers = rxcpp::observable<>::range(1, 100);
numbers
    ./* convert rxcpp::observable<int> to rxcpp::observable<std::list<int>>> */
    .subscribe([](std::list<int> numbers)
    {
        /* do stuff */
    });
kirkshoop commented 9 years ago

that operator has not been added yet, so for now one would explicitly use reduce.

    rx::observable<>::range(1, 10).
        reduce(std::list<int>(),
            [](std::list<int>& o, int v){
                o.push_back(v);
                return std::move(o);
            },
            [](std::list<int>& o){
                return std::move(o);
            }).
        as_blocking().
        subscribe(
            [](std::list<int> ten){
                for(auto v : ten){
                    printf("%d\n", v);
                }
            });
hasyimibhar commented 9 years ago

Good enough for now. :)

kirkshoop commented 9 years ago

contributions welcome!