凤凰涅磐 --- Phoenix 2 发布预览

来源:互联网 发布:morris.js api中文 编辑:程序博客网 时间:2024/04/29 20:41
首先,对于不熟悉 Phoenix 的读者,先介绍一下 Phoenix 是什么。Phoenix 的作者,Joel de Guzman,也就是 Boost.Spirit 的作者,在看到许多往 C++ 中引入 Functional Programming 的努力以后,决定把 Spirit 优雅的设计思想用于建立一个 C++ 的 Functional Programming 库,这就是 Phoenix 了。Joel 自己是这样说的:

Phoenix is a blend of FC++ and BLL using the implementation techniques used in the Spirit  inline parser. Phoenix version 2, this version, will probably be the last release of the library. Phoenix v2 will be the basis of the Phoenix and BLL  merger.

如雷贯耳吧,Spirit,FC++ 和 BLL (Boost Lambda Library) 的结合会是什么样子?Phoenix 2 就是一份答卷。

先来个初步印象,用 Phoenix 2 ,我们可以办到 BLL 能办到的事情:

#include
#include
#include
#include

using namespace boost::phoenix;

int main()
{
    std::vector vec(10);
    std::partial_sum(vec.begin(), vec.end(), vec.begin(), _2 = _1 + 1);
   
    std::for_each(vec.begin(), vec.end(), std::cout << _1 << " " );
}

输出:

0 1 2 3 4 5 6 7 8 9

没错,这些在我以前介绍 Boost.Lamda 的时候都用过,程序可以原封不动的搬过来使用 Phoenix 2 。

Phoenix 2 几乎采用了 Boost.Lamda 的所用东西,对某些作了语法上的改动,使之更加一致和清晰。例如我们想要输出上面 vec 当中所有大于5的元素,便可以用到 Phoenix 2 中的 statement :

#include
#include
#include
#include
#include
#include
#include

using namespace boost::phoenix;

int main()
{
    std::vector vec(10);
    std::partial_sum(vec.begin(), vec.end(), vec.begin(), _2 = _1 + 1);
   
    std::for_each(vec.begin(), vec.end(),
        if_ ( _1 > 5 )
        [
            std::cout << _1 << ", "
        ]
    );
}

输出:

6, 7, 8, 9,

这是什么东西?注意,我们没有真的把 if 放到 for_each 里面,我们放进去的是 if_ ,这是一个 Phoenix 定义的 statement,它的作用和 C++ "原生"的 if 是一样的。可能你还会注意到 if_ 后面是 [ ] 而不是我们熟悉的 { },这是 Phoenix 2 对 Boost.Lambda 的改进(在 Lambda 里面,我们有的时候用 ( ),有的时候用 [ ] ,缺乏一致性)。“真无聊“,我听到有人在说,“我自己写个循环不是一样么”。当然,如果是这样,我们甚至可以不用 for_each,for 循环是万能的;我们还可以不用 vector,数组也完全可以办到这些……这些 statement 存在的意义,在于我们可以 "inline" 的编写相当复杂的 lambda 表达式,例如我如果不希望输出最后一个逗号,可以把输出的语句换成这样:

            std::cout << _1 << if_else(_1 < 9, ", ", "")

这里的 if_else 是专门用来替代 C++ 的 : , 操作符的,我们知道 C++ 不允许重载这个操作符,所以 Phoenix 2 采用了这个办法,不过依我看来,它反而比 : , 更加清晰。

当然,if_ 是可以接 else_ 的,看看下面,注意语法:

    std::for_each(vec.begin(), vec.end(),
        if_ ( _1 > 5 )
        [
            std::cout << _1 << if_else(_1 < 9, ", ", "")
        ]
        .else_
        [
            std::cout << _1 << "<=5, "
        ]
    );

输出:

0<=5, 1<=5, 2<=5, 3<=5, 4<=5, 5<=5, 6, 7, 8, 9

注意 else_ 前面有一个 . 这有这样,它们才能被编译器看作同一个表达式而接受。

#include
#include
#include
#include
#include
#include
#include

using namespace boost::phoenix;

int main()
{
    std::vector vec(10);
    std::partial_sum(vec.begin(), vec.end(), vec.begin(), _2 = _1 + 1);
   
    std::for_each(vec.begin(), vec.end(),
        (
            while_ ( --_1 >= 0 )
            [
                std::cout << _1 << if_else(_1 > 0, ", ", "")
            ],
            std::cout << val('/n')
        )
    );
}

输出:

0
1, 0
2, 1, 0
3, 2, 1, 0
4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
6, 5, 4, 3, 2, 1, 0
7, 6, 5, 4, 3, 2, 1, 0
8, 7, 6, 5, 4, 3, 2, 1, 0

在这个程序中,我们除了看到 while_ ,还看到如何把几个语句并列起来:放在括号里,用逗号隔开,当然这是 C++ 的常识了,只不过在这种场合显得特别有用。这个语句有一个副作用:由于参数是 by ref 传递的,所以在执行过程中,vec 中的元素的值已经被更改了,如果你在语句之后输出 vec 所有的元素,你会得到:

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

我们甚至可以在 for_each 中使用 for 循环,这就要用到 Phoenix 2 的 for_ statement:

#include
#include
#include
#include
#include
#include
#include

using namespace boost::phoenix;

int main()
{
    std::vector vec(10);
    std::partial_sum(vec.begin(), vec.end(), vec.begin(), _2 = _1 + 1);
   
    int i;
    std::for_each(vec.begin(), vec.end(),
        (
            for_ ( var(i) = _1, var(i) >= 0, --var(i) )
            [
                std::cout << var(i) << if_else(var(i) > 0, ", ", "")
            ],
            std::cout << val('/n')
        )
    );
   
    std::cout << '/n';
    for_each(vec.begin(), vec.end(), std::cout << _1 << " ");
}

输出:

0
1, 0
2, 1, 0
3, 2, 1, 0
4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
6, 5, 4, 3, 2, 1, 0
7, 6, 5, 4, 3, 2, 1, 0
8, 7, 6, 5, 4, 3, 2, 1, 0
9, 8, 7, 6, 5, 4, 3, 2, 1, 0

0 1 2 3 4 5 6 7 8 9

注意,for_ 的3个部分不是用分号,而是用逗号分隔的。为什么我们要用 var(i) 而不直接用 i ?这是为了使用 lazy evaluation ,如果直接用 i ,那么 i 会在语句执行之前被求值,而不是在语句执行的过程中被求值。Lazy evaluation 在 Functional Programming 中的地位至关重要。

在上面的例子中,我们必须在外面定义一个变量 i ,然后用 var 把它变成 Phoenix 2 能接受的表达式,难道我们必须这样?非也,Phoenix 2 甚至提供了方法来定义局部变量:

#include
#include
#include
#include
#include
#include
#include
#include

using namespace boost::phoenix;
using namespace boost::phoenix::local_names;

int main()
{
    std::vector vec(10);
    std::partial_sum(vec.begin(), vec.end(), vec.begin(), _2 = _1 + 1);
   
    std::for_each(vec.begin(), vec.end(),
        let ( _a = 0 )
        [
            for_ ( _a = _1, _a >= 0, --_a )
            [
                std::cout << _a << if_else(_a > 0, ", ", "")
            ],
            std::cout << val('/n')
        ]
    );
  
    std::cout << '/n';
    for_each(vec.begin(), vec.end(), std::cout << _1 << " ");
}

输出和前面的程序一样:

0
1, 0
2, 1, 0
3, 2, 1, 0
4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
6, 5, 4, 3, 2, 1, 0
7, 6, 5, 4, 3, 2, 1, 0
8, 7, 6, 5, 4, 3, 2, 1, 0
9, 8, 7, 6, 5, 4, 3, 2, 1, 0

0 1 2 3 4 5 6 7 8 9

其中 let 就是在 Phoenix 2 中使用局部变量的方法,Phoenix 2 预定义了26个局部变量 _a ~ _z,它们放在 boost::phoenix::local_names 命名空间内。




原创粉丝点击