函数可以看作是一个普通变量。可被存储在集合或结构中,作为参数传递给其他函数,或作为函数的返回值。
高阶函数:能够 接收函数作为参数或者 返回函数作为结果的函数。
- filter:过滤后集合类型不变
一个类型:T
包含T类型的集合:Collection<t> or C<t></t></t>
filter函数原型就可以写作:
(Collection<t>, (T->bool)) **->** Collection<t> </t></t>
()这个括号代表函数;
“->”符号后面是返回值类型`
T->bool这个指的是一个函数,接收一个T类型参数,返回bool值。就是lamba表达式。
(Collection, (T->bool)) 这个就是接收两个参数,一个集合T,另一个就是上面说那个函数。后面->Collection 指返回还是集合T。
- map/transform: 返回值可以是其他类型(输入是IN类型集合,输出是OUT类型的集合,所以名字叫变形呢
(Collection<in>, (In->Out)) -> Collection<out> </out></in>
累加器 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 就是遍历元素的很好方法。将返回可以全部放到新的集合中。
实现:
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, 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;
}
</class></class>
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';
</int></int></int>
注意 std::next ,郁闷死了,我说怎么取第二个值了。
Original: https://www.cnblogs.com/bigben0123/p/16221533.html
Author: Bigben
Title: c++函数式编程 笔记
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535470/
转载文章受原作者版权保护。转载请注明原作者出处!