0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Boost.Phoenixでのactorとlazy control structureを考える

Last updated at Posted at 2020-08-25

Boost.Phoenixには"actor"と"lazy control structures"というフレーズがある。

アクターは関数型言語でよく出てくるが、いまいちよくわからない。。

Actors are the abstractions for lazy functions which composes Phoenix function.

とあるが、actorは遅延評価関数を構成するために使うモノであるぐらいに解釈した。

 1#include <boost/phoenix/core.hpp>
 2#include <boost/phoenix/operator.hpp>
 3#include <vector>
 4#include <string>
 5#include <iostream>
 6#include <algorithm>
 7
 8int main() {
 9  using boost::phoenix::arg_names::arg1;
10  std::vector<std::string> vec{"Taro", "Ichiro",
11                               "Shiro", "Saburo"};
12  std::for_each(vec.begin(), vec.end(),
13                std::cout << arg1 << '\n');
14}

↑ Boost.Phoenixでラムダ式を使う。

遅延評価も厳密な解釈がむずかしいのだが、個人的には「リストを作って各要素に関数を適用していく」くらいの解釈にしている。

Lazy control structures.
It is possible to generate actors which lazily evaluate entire blocks of code with branching and looping constructs.

これは、「遅延評価をifやloopなどのコントロールブロック全体に適用できる」という感じだらうか。(*´Д`)

 1#include <boost/phoenix/core.hpp>
 2#include <boost/phoenix/statement/if.hpp>
 3#include <boost/phoenix/operator.hpp>
 4#include <algorithm>
 5#include <vector>
 6#include <iostream>
 7
 8int main() {
 9  namespace phx = boost::phoenix;
10  using namespace phx;
11  using phx::arg_names::arg1;
12
13  std::vector<std::string> names{"Taro", "Ichiro",
14                                 "Shiro", "Saburo"};
15  std::for_each(names.begin(), names.end(),
16            if_(arg1 == "Shiro") [
17              std::cout << arg1 << ", vocalist" << '\n'
18            ].else_[
19              std::cout << arg1 << ", player" << '\n'
20            ]
21            );
22}

実行してみる。。。


# ./a.out 
Taro, player
Ichiro, player
Shiro, vocalist
Saburo, player
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?