首页 > 其他 > 详细

ReactiveX 学习笔记(23)RxCpp

时间:2018-09-05 00:06:59      阅读:570      评论:0      收藏:0      [点我收藏+]

RxCpp

RxCpp 是 ReactiveX 的 C++ 语言实现。

#include "rxcpp/rx.hpp"
namespace Rx {
    using namespace rxcpp;
    using namespace rxcpp::sources;
    using namespace rxcpp::operators;
    using namespace rxcpp::util;
}
using namespace Rx;

Creating

auto ints = observable<>::create<int>(
    [](subscriber<int> s){
        s.on_next(1);
        s.on_next(2);
        s.on_completed();
    });
ints.subscribe(
    [](int v){printf("OnNext: %d\n", v);},
    [](){printf("OnCompleted\n");});
/*
OnNext: 1
OnNext: 2
OnCompleted
*/

Converting

std::array< int, 3 > a={{1, 2, 3}};
auto values1 = observable<>::iterate(a);
values1.subscribe(
    [](int v){printf("OnNext: %d\n", v);},
    [](){printf("OnCompleted\n");});
/*
OnNext: 1
OnNext: 2
OnNext: 3
OnCompleted
*/

Combining

auto values = observable<>::range(1); // infinite (until overflow) stream of integers
auto s1 = values.
take(3).
map([](int prime) { return std::make_tuple("1:", prime);});
auto s2 = values.
take(3).
map([](int prime) { return std::make_tuple("2:", prime);});
s1.
concat(s2).
subscribe(apply_to(
    [](const char* s, int p) {
        printf("%s %d\n", s, p);
    }));
/*
1: 1
1: 2
1: 3
2: 1
2: 2
2: 3
*/
auto values = rxcpp::observable<>::range(1); // infinite (until overflow) stream of integers
auto s1 = values.
map([](int prime) { return std::make_tuple("1:", prime);});
auto s2 = values.
map([](int prime) { return std::make_tuple("2:", prime);});
s1.
merge(s2).
take(6).
as_blocking().
subscribe(apply_to(
    [](const char* s, int p) {
        printf("%s %d\n", s, p);
    }));
/*
1: 1
2: 1
1: 2
2: 2
1: 3
2: 3
*/

ReactiveX 学习笔记(23)RxCpp

原文:https://www.cnblogs.com/zwvista/p/9589298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!