C++ で セミコロンを使わずに FibBuzz
前回と同じしばりですね。
今回も Boost.Phoenix を使用しました。
フィボナッチ数列は再帰で処理していたので FibBuzz は無理かなーと思ったんですが、別のアルゴリズムがあったのでやってみました。
[ソース]
#include <boost/phoenix.hpp> #include <boost/lexical_cast.hpp> #include <iostream> #include <string> int main(){ if(boost::phoenix::let( boost::phoenix::local_names::_a = boost::phoenix::lambda( boost::phoenix::local_names::_a = 1, boost::phoenix::local_names::_b = 1, boost::phoenix::local_names::_c = 1, boost::phoenix::local_names::_i = 1 )[ boost::phoenix::for_( boost::phoenix::nothing , boost::phoenix::local_names::_i < boost::phoenix::arg_names::arg1 , ++boost::phoenix::local_names::_i )[ boost::phoenix::local_names::_c = boost::phoenix::local_names::_a + boost::phoenix::local_names::_b, boost::phoenix::local_names::_a = boost::phoenix::local_names::_b, boost::phoenix::local_names::_b = boost::phoenix::local_names::_c ], boost::phoenix::local_names::_c ], boost::phoenix::local_names::_b = boost::phoenix::lambda[ boost::phoenix::if_else( boost::phoenix::arg_names::arg1 % 15 == 0, std::string("FizzBuzz"), boost::phoenix::if_else( boost::phoenix::arg_names::arg1 % 3 == 0, std::string("Fizz"), boost::phoenix::if_else( boost::phoenix::arg_names::arg1 % 5 == 0, std::string("Buzz"), boost::phoenix::bind( boost::lexical_cast<std::string, int>, boost::phoenix::arg_names::arg1 )))) ], boost::phoenix::local_names::_i = 0 )[ boost::phoenix::for_( boost::phoenix::nothing, boost::phoenix::local_names::_i < boost::phoenix::arg_names::arg1, ++boost::phoenix::local_names::_i )[ std::cout << boost::phoenix::bind(boost::phoenix::local_names::_b, boost::phoenix::bind( boost::phoenix::local_names::_a, boost::phoenix::local_names::_i ) ) << ", " ] ](30), 0){} }
[出力]
1, 1, 2, Fizz, Buzz, 8, 13, Fizz, 34, Buzz, 89, Fizz, 233, 377, Buzz, Fizz, 1597, 2584, 4181, FizzBuzz, 10946, 17711, 28657, Fizz, Buzz, 121393, 196418, Fizz, 514229, Buzz,
local_names を多用しているので見づらいですね…。