d求值两次map

原文

void main()
{
    import std.algorithm, std.stdio;

    [1,2,3,4,5].
        map!((x){
            writeln("mapping ", x);
            return x;
        }).
        filter!(x=>x>2).
        front.
        writeln();
}

map每次调用front时调用lambda.如果想要缓存版本,请使用cache:

void main()
{
    import std.algorithm, std.stdio;

    [1,2,3,4,5].
        map!((x){
            writeln("mapping ", x);
            return x;
        }).
        cache. //加上.
        filter!(x=>x>2).
        front.
        writeln();
}
posted @ 2022-06-13 09:23  zjh6  阅读(20)  评论(0)    收藏  举报  来源