网站模板怎么使用,查建设公司年度保证金网站,网络营销价格,joomla功能型网站建设C 越来越像函数式编程了
大家好#xff0c;欢迎来到今天的博客话题。今天我们要聊的是 C 这门老牌的强类型语言是如何一步一步向函数式编程靠拢的。从最早的函数指针#xff0c;到函数对象#xff08;Functor#xff09;#xff0c;再到 std::function 和 std::bind…C 越来越像函数式编程了
大家好欢迎来到今天的博客话题。今天我们要聊的是 C 这门老牌的强类型语言是如何一步一步向函数式编程靠拢的。从最早的函数指针到函数对象Functor再到 std::function 和 std::bind还有 lambda 表达式最后我们重点讲讲 C20 的 Ranges。这一路走来C 变得越来越强大越来越像函数式编程了。 什么是函数式编程
在深入探讨 C 的演变之前我们先简单介绍一下什么是函数式编程Functional Programming。函数式编程是一种编程范式它把计算视为数学函数的求值强调引用透明性、纯函数、高阶函数和惰性求值等概念。
引用透明性相同输入总是得到相同的输出没有副作用。纯函数函数内部不修改任何外部状态也不依赖外部状态。高阶函数可以接受函数作为参数或者返回函数。惰性求值表达式只在需要时才计算。
一些更接近纯函数式编程范式的编程语言有
HaskellLisp (及其变种如 Scheme 和 Clojure)ErlangF#
这些语言天生具有函数式编程的特性但我们的 C 也在一步步地引入这些概念让我们看看 C 是如何演变到今天的吧。
函数指针
首先当然是函数指针了这是 C 中最原始的一种“函数式”手段。函数指针可以指向一个函数然后通过这个指针调用这个函数。
#include iostreamvoid hello() {std::cout Hello, world! std::endl;
}int main() {// 定义一个函数指针void (*funcPtr)() hello;// 通过指针调用函数funcPtr();return 0;
}这种方法虽然简单但是它的局限性也很明显比如只能指向某一种特定签名的函数灵活性不足。
函数对象Functor
随后 C 中引入了函数对象Functor。通过重载 operator()我们可以创建一个像函数一样调用的对象。
#include iostreamclass HelloFunctor {
public:void operator()() const {std::cout Hello, world! std::endl;}
};int main() {HelloFunctor hello;hello(); // 调用函数对象return 0;
}函数对象比起函数指针更灵活因为它可以保存状态和行为。不过写起代码来多少有些繁琐。
std::function 和 std::bind
到了 C11std::function 和 std::bind 出现了。std::function 是一个通用的函数包装器几乎可以保存任意的可调用对象。而 std::bind 则可以将函数和参数绑定起来生成新的函数。
#include functional
#include iostream// 普通函数
int add(int a, int b) {return a b;
}int main() {// 使用 std::function 包装函数std::functionint(int, int) func add;std::cout func(3, 4) std::endl; // 输出 7// 使用 std::bind 绑定参数auto add_with_2 std::bind(add, 2, std::placeholders::_1);std::cout add_with_2(5) std::endl; // 输出 7return 0;
}这一步让 C 的函数处理能力更上了一个台阶。可以部分绑定参数再把它们传递或者存储起来方便多了。
Lambda 表达式
C11 还引入了 lambda 表达式让我们可以在代码的任何地方定义匿名函数极大地提高了代码的简洁性和灵活性。
#include iostreamint main() {auto hello []() {std::cout Hello, world! std::endl;};hello();int x 42;auto printX [x]() {std::cout x std::endl;};printX();return 0;
}lambda 表达式不但让代码更加简洁还可以捕获上下文中的变量真是灵活至极。
C20 的 Ranges
重点来了C20 引入了 Ranges 库这真是一大进步让 C 更接近现代的函数式编程风格。用 Ranges我们可以像处理流一样处理序列而且不需要手动写那些繁琐的循环。
基本用法
先来看一个简单的例子吧。
#include iostream
#include vector
#include rangesint main() {std::vectorint numbers {1, 2, 3, 4, 5, 6};auto result numbers| std::views::filter([](int n) { return n % 2 0; })| std::views::transform([](int n) { return n * n; });for (auto n : result) {std::cout n ;}std::cout std::endl;return 0;
}这个例子里我们用 std::views::filter 过滤掉了奇数然后用 std::views::transform 把每个偶数平方。这种写法简洁优雅而且更符合人类思维。
惰性求值
Ranges 还有一个厉害的地方就是它是惰性求值的。意思是说它不会在定义的时候马上计算而是在真正需要结果的时候才计算这样就避免了不必要的开销。
#include iostream
#include rangesint main() {auto numbers std::views::iota(1, 1000000)| std::views::filter([](int n) { return n % 2 0; })| std::views::transform([](int n) { return n * n; })| std::views::take(5); // 仅获取前5个结果for (auto n : numbers) {std::cout n ;}std::cout std::endl;return 0;
}这个例子中我们生成了从 1 到 1000000 的范围但最后只取了前 5 个结果。因为是惰性求值的整个过程非常高效。
4 16 36 64 100 ...Program finished with exit code 0
Press ENTER to exit console.组合视图
Ranges 里的视图可以像管道一样组合起来用 | 操作符一看就知道数据是按什么顺序处理的。
#include iostream
#include vector
#include rangesint main() {std::vectorint numbers {1, 2, 3, 4, 5, 6};auto result numbers| std::views::filter([](int n) { return n % 2 0; })| std::views::transform([](int n) { return n * n; })| std::views::reverse;for (auto n : result) {std::cout n ;}std::cout std::endl;return 0;
}这个例子里我们把前面的结果反转了一下同样用的视图代码依然非常清晰。
36 16 4 ...Program finished with exit code 0
Press ENTER to exit console.生成和合并
还有一些其他很有用的视图比如 std::views::iota 可以生成递增的序列std::views::join 可以扁平化嵌套的范围。
#include iostream
#include ranges
#include vectorint main() {auto numbers std::views::iota(1) | std::views::take(10); // 1到10for (auto n : numbers) {std::cout n ;}std::cout std::endl;std::vectorstd::vectorint nested { {1, 2}, {3, 4}, {5, 6} };auto flat_view nested | std::views::join;for (auto n : flat_view) {std::cout n ;}std::cout std::endl;return 0;
}第一个例子是生成从 1 开始的自然数序列取前 10 个。第二个例子是把嵌套的 vector 扁平化这种操作在实际中非常常见而且有用。
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 ...Program finished with exit code 0
Press ENTER to exit console.结语 通过这一路的演化我们看到 C 引入的这些特性——从函数指针、函数对象、std::function、std::bind、lambda 表达式再到 C20 的 Ranges让我们可以越来越方便地写函数式风格的代码。
这些新特性不仅让我们的代码更简洁、更易读更高效还让我们更容易掌握函数式编程的理念。希望大家通过这篇文章对这些特性有更深的理解并把它们用到实际开发中让你的代码更加优雅