ms-*_*ati 7 c++ boost functional-programming
具体功能包括:
(好奇心问道,离开C++好几年了)
是的,传统上认为其中一些功能需要垃圾收集.但是,有了现代C++特性和库,有没有人开始通过功能转换传递托管指针?
更新 要清楚,我想知道有类似于Functional Java的东西,所以以下可能是典型的语法:
// assumptions:
// * my_list is a standard library iterable of ints
// * f is a function of int that returns a std::string
// * p is a predicate of std::string returning bool
// * head_opt returns an option type
stream(my_list).map(f).filter(p).head_opt.get_or_else("None")
Run Code Online (Sandbox Code Playgroud)
这是Functional Java提供的习惯用语,相信我很容易习惯它...
Xeo*_*Xeo 12
正如@jalf所说,地图和折叠已经在标准中,隐藏在不同的名称背后:
std::transform,在标题中找到<algorithm>std::accumulate,在标题中找到<numeric>在Boost.Range中可以找到更多功能性的东西,这是一个非常棒的库.特别是范围适配器提供了真正的功能感,因为它们可以创建其他范围的视图.使用C++ 11,可以通过lambdas轻松创建可能的谓词.
Boost.Optional可能是您的"选项类型",具体取决于您的具体含义.
只需声明对象即可实现C++的不变性const.您可以使用by-reference参数传递来避免副本.说实话,这当然不等同于真正的功能不变性,因为功能语言中的不可变容器可以根据需要进行复制,通常只是共享内部表示.毕竟,如果你从不写,写拷贝很棒.
在你的托管指针上,我不知道你的意思.在C++中,通常根本不需要指针或动态分配的对象.只需在"堆栈"中创建它们:Foo obj;.
如果你的意思是共享所有权,那就是std::shared_ptr.如果将这样的指针存储在容器中,甚至还有一个很好的范围适配器:
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/algorithm/generate.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <vector>
#include <memory>
#include <algorithm>
#include <iterator>
#include <iostream>
int main(){
std::vector<std::shared_ptr<int>> v(5);
int i = 0;
boost::generate(v, [&i]{ return std::make_shared<int>(i++); });
boost::copy(v | boost::adaptors::indirected,
std::ostream_iterator<int>(std::cout));
}
Run Code Online (Sandbox Code Playgroud)
你的具体例子
my_list.map(f).filter(p).head_opt.get_or_else("not found")
可能会像这样实现(注意这std::vector是C++中的默认容器):
// Warning, C++11 only!
// Boost.Range doesn't like lambdas without this:
#define BOOST_RESULT_OF_USE_DECLTYPE
#include <vector>
#include <string>
#include <iterator>
#include <iostream>
#include <boost/optional.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/generate.hpp> // only needed for filling the vector
#include <boost/range/algorithm/copy.hpp> // only needed for printing
// we need a little helper for the optional stuff
struct head_opt_gen{} head_opt; // just a tag type
template<class Range>
auto operator|(Range const& r, head_opt_gen)
-> boost::optional<decltype(r.front())>
{
if(r.empty())
return boost::none;
return r.front();
}
int main(){
using namespace boost::adaptors;
std::vector<int> v(5);
int i = 0;
boost::generate(v, [&]()->int{ ++i; return i*i; });
// first, without the optional stuff
boost::copy(v | transformed([](int x){ return std::to_string(x); })
| filtered([](std::string const& s){ return s.size() > 1; }),
std::ostream_iterator<std::string>(std::cout, "\n"));
std::cout << "=====================\n";
// now with
std::cout << boost::get_optional_value_or(
v | transformed([](int x){ return std::to_string(x); })
| filtered([](std::string const& s){ return s.size() > 2; }) // note: > 2
| head_opt, "none");
}
Run Code Online (Sandbox Code Playgroud)
使用Clang 3.1 Trunk编译,结果如下:
16
25
=====================
none
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5830 次 |
| 最近记录: |