是否存在与Functional Java类似/等效的C++库?

ms-*_*ati 7 c++ boost functional-programming

是否有与优秀功能Java库类似或等效的开源C++

具体功能包括:

  • 在迭代物等上映射,折叠/缩小,过滤等
  • 选项类型
  • 不可变数据结构实现

(好奇心问道,离开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所说,地图和折叠已经在标准中,隐藏在不同的名称背后:

  • map - > std::transform,在标题中找到<algorithm>
  • fold - > 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)

  • @ ms-tg:对不起,我们尽最大努力回答问题.如果你对我们的答案不满意,那么合乎逻辑的结论就是*你的问题应该得到改善*,而不是**"每个花时间尝试*并回答我的问题的人应该学习4种新语言,*然后*给我写一个答案 (2认同)
  • 最后,除此之外,我**知道一些函数式语言.正如你所说,我知道map/reduce是什么,是的,在我的评论中,我暗示如何以命令式的方式做同样的事情,因为当我读到这个问题时,这是我能想到的最好的建议.底线是**C++不是一种功能语言.它可以近似,并且@Xeo已经显示*一个这样的可能近似值.如果你想要一个*不同的*近似值,那么写一个更好的问题,它确定*确切*答案应该提供什么."像其他语言/图书馆这样的东西"非常模糊. (2认同)