Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ranges #206

Closed
cathaysia opened this issue Sep 4, 2021 · 2 comments
Closed

Ranges #206

cathaysia opened this issue Sep 4, 2021 · 2 comments

Comments

@cathaysia
Copy link
Contributor

cathaysia commented Sep 4, 2021

尽管我看到 #1 中的 Ranges 打对号了,但是似乎实际上并没有内容。

实在是抱歉,这一段时间因为需要准备秋招,所以我可能没时间弄 PR 了。今天看到这个项目想起来我 Range 做的笔记了,这里贴出来希望能对你有所帮助:

Ranges 本身内容比较少,所以确实也没什么可写的,主要特征应该是 数据不变 和管道符的使用吧。我本身的笔记是 rst 的,转成 md 后多出来了一部分换行

这里是预览:

Range

Range 是 C++ 社区对 反应式编程 () 的回应。最初由
boost
社区实现, v3 版本 分离出来成为 C++ 20
标准库的基础。与其类似的还有 RxCpp
。Range 相当于 RxCpp 的数据处理部分。

反应式编程是基于 数据流 (Data Stream)变化传递 (Propagation of Change Value)
声明式 编程方式。数据在传递过程中保持不变。原始数据通过通道符传递到一个一个 view 中,view
对数据进行处理之后又形成了新的数据,就这样,数据通过管道符在一个一个 view
中流动中发生变化,最终成品。

先看一个简单的 Demo 以对 Range 有初步的印象:

#include <iostream>
#include <ranges>
#include <vector>

using namespace std;

int main() {
   vector<int> ints { 0, 1, 3, 4, 5 };
   auto res = ints | views::filter([](int item) {
      return item % 2;
   }) | views::transform([](int item) {
      return item * item;
   });
   for(auto const& item : res){
      cout << item << ", ";
   }
   return 0;
}

如代码所述,原始的数据 ints 在成品(形成
res)的过程中分别经过过滤(views::filter)、转换(views::transform)形成,最后由
for 将成品打印出来。ints 在这过程中始终保持不变。

概述

Range 库由头文件 <ranges> 提供,并带来一个命名空间 std::views 。 std::views 名为
视图适配器 ,负责对数据的处理,常用的视图适配器如下:

view::filter 谓词 根据谓词过滤数据
view::transform 函数 将数据进行变形
view::take size_t 取走前几个数据
view::drop size_t 丢弃前几个数据
view::join 数据流 将上一个数据流与指定数据流合并
view::split str 将上一个数据流根据 str 分割为多个数据
view::keys 数据流 从 map 数据流中提取 key
view::values 数据流 从 map 数据流中提取 value
view::reverse none 反转数据流中数据的顺序
view::take_while 谓词 过滤满足谓词条件的数据
view::iota <i,i> 产生范围数据
view::single data 产生单个数据

这里是源码:
Range.md

@changkun changkun reopened this Sep 14, 2021
@changkun
Copy link
Owner

Let's keep it open before it arrives into the book

@cathaysia
Copy link
Contributor Author

I close this due to it took too much time.

@cathaysia cathaysia closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants