函数式编程是一种编程范式。在其中,函数是一等公民,函数能够付给其余变量,也能够做为另外一个函数的参数或返回值。函数式编程要求,只使用表达式,不使用语句。c++
函数式编程是声明式的编程,它与命令式编程不一样。命令意味着要命令计算机作某事,明确它须要执行的每一步,以计算结果。声明意味着你说明该作什么,而后编程语言的任务是找出如何作,一般它定义是什么,而不是一步一步告诉计算机去执行。算法
想象一下,你编写一个函数,它接收文件列表并计算每一个文件中的行数。
命令式的作法可能会是这样:编程
下面是代码:编程语言
auto count_lines_in_file(const std::vector<std::string>& files) -> std::vector<int> { std::vector<int> result; int c = 0; for (const auto& file : files) { int count_lines = 0; std::ifstream in(file); while (c = in.get()) { if (c == '\n') count_lines++; } result.push_back(count_lines); } return result; }
但上面的代码易出错,好比未初始化的变量、if、while的条件等等。
下面作法用到了std::count算法和输入流迭代器简化了代码:函数式编程
int count_lines(const std::string& file) { std::ifstream in(file); // 像对容器同样,也能够对流迭代器使用stl算法 return std::count(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>(), '\n'); } auto count_lines_in_file(std::vector<std::string>& files) -> std::vector<int> { std::vector<int> result; for (auto& file : files) { result.push_back(count_lines(file)); } }
在上面这个解决方案中,你不用关心count_lines是如何实现的,count_lines只是指出了计算文件的行数。这就是函数式编程的意图。函数
让咱们再次简化代码,接近函数式编程,此次使用了std::transform:code
auto coutn_lines_in_file3(std::vector<std::string>& files) { std::vector<int> result; std::transform(files.begin(), files.end(), result.begin(), count_lines); return result; }
到了c++20,能够使用范围库再次简化:orm
auto count_lines_in_file4(std::vector<std::string>& files) { // 使用管道操做符(c++20) return files | std::transform(count_lines); }