c++函数式编程 笔记

函数可以看作是一个普通变量。可被存储在集合或结构中,作为参数传递给其他函数,或作为函数的返回值。
高阶函数:能够接收函数作为参数或者返回函数作为结果的函数。

  • filter:过滤后集合类型不变
    一个类型:T
    包含T类型的集合:Collection<T> or C<T>
    filter函数原型就可以写作:

(Collection<T>, (T->bool)) **->** Collection<T>
()这个括号代表函数;
"->"符号后面是返回值类型`
T->bool这个指的是一个函数,接收一个T类型参数,返回bool值。就是lamba表达式。
(Collection, (T->bool)) 这个就是接收两个参数,一个集合T,另一个就是上面说那个函数。后面->Collection 指返回还是集合T。

  • map/transform: 返回值可以是其他类型(输入是IN类型集合,输出是OUT类型的集合,所以名字叫变形呢
    (Collection<In>, (In->Out)) -> Collection<Out>

累加器 std::accumulate/ folding/ reducing: 对递归结构(vector,list,tree...)遍历,并逐步构建自己需要的结果。
(std::reduce可实现并行效果)
折叠接收集合包含T类型条目,R类型的初始值和一个函数f:(R,T)->R

过滤函数区别

filer 是对T返回bool;
transform 对T返回另外一种类型 "T1";
accumulate 对 上次结果R和T,用f函数运算f(R,T), 返回 结果R类型的值。R可以是个集合,实现搜集。

循环与递归

纯FP不存在循环,用递归实现。思想是,对于一个集合,递归地处理它的头(第一个元素)和尾(其他所有元素),这又可以看作集合。
分别对头和尾做处理。尾就又递归调用自身处理。
其实 accumulate 就是遍历元素的很好方法。将返回可以全部放到新的集合中。

std::accumulate 实现原理 与 demo

实现:

template<class InputIt, class T>
constexpr // since C++20
T accumulate(InputIt first, InputIt last, T init)
{
for (; first != last; ++first) {
init = std::move(init) + *first; // std::move since C++20
}
return init;
}
template<class InputIt, class T, class BinaryOperation>
constexpr // since C++20
T accumulate(InputIt first, InputIt last, T init,
BinaryOperation op)
{
for (; first != last; ++first) {
init = op(std::move(init), *first); // std::move since C++20
}
return init;
}

demo:

const std::vector<int> ds = { 1, 2, 3 };
int n = std::accumulate(ds.begin(), ds.end(),
0,
[](int a, int d) {
cout << a << " " << d << endl;
return a * 10 + d;
});
std::cout << n << std::endl;
std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = std::accumulate(v.begin(), v.end(), 0);
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
auto dash_fold = [](std::string a, int b) {
return std::move(a) + '-' + std::to_string(b);
};
std::string s = std::accumulate(std::next(v.begin()), v.end(),
std::to_string(v[0]), // start with first element
dash_fold);
// Right fold using reverse iterators
std::string rs = std::accumulate(std::next(v.rbegin()), v.rend(),
std::to_string(v.back()), // start with last element
dash_fold);
std::cout << "sum: " << sum << '\n'
<< "product: " << product << '\n'
<< "dash-separated string: " << s << '\n'
<< "dash-separated string (right-folded): " << rs << '\n';

注意 std::next ,郁闷死了,我说怎么取第二个值了。

posted @   Bigben  阅读(112)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2018-05-04 每个Java程序员需要了解的8个Java开发工具
2018-05-04 五大理由分析Springboot 2.0为什么选择HikariCP
2015-05-04 Android 如何在Eclipse中查看Android API源码 及 support包源码
点击右上角即可分享
微信分享提示