読者です 読者をやめる 読者になる 読者になる

Boost.Fusionのアルゴリズムでラムダ式

C++

@naoki_yoshiokaさんのつぶやきまとめ。

http://twitter.com/naoki_yoshioka/status/14813792168:twitter

http://twitter.com/naoki_yoshioka/status/14814757074:twitter

http://twitter.com/naoki_yoshioka/status/14814924227:twitter

#include <iostream>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/home/phoenix/core.hpp>
#include <boost/spirit/home/phoenix/operator.hpp>
#include <pstade/oven/regular.hpp>

namespace fusion = boost::fusion;
using namespace boost::phoenix::arg_names;

int main()
{
    const fusion::vector<int, double> v(1, 0.5);

    {
        const fusion::vector<int, double> result = fusion::transform(v, _1 + _1);
        std::cout << result << std::endl;
    }
    {
        const double sum = fusion::accumulate(v, 0.0, pstade::oven::regular(_1 + _2));
        std::cout << sum << std::endl;
    }
}
(2 1)
1.5

Phoenixのラムダは戻り値の型がとれるんですね。


追記:
Boost.Lambdaではregularいらないですね。

#include <iostream>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/lambda/lambda.hpp>
#include <pstade/egg/bll/result_of.hpp>

namespace fusion = boost::fusion;
using namespace boost::lambda;

int main()
{
    const fusion::vector<int, double> v(1, 0.5);

    {
        const fusion::vector<int, double> result = fusion::transform(v, _1 + _1);
        std::cout << result << std::endl;
    }
    {
        const double sum = fusion::accumulate(v, 0.0, _1 + _2);
        std::cout << sum << std::endl;
    }
}