Java 8:延迟计算

来源:互联网 发布:建筑节能分析软件 编辑:程序博客网 时间:2024/06/05 05:12


Java8:Stream概念的细化,讨论延迟计算/惰性求值Lazy Evaluations

1.惰性 Vs. 饥饿

表达式求值的顺序有两种:

  • 短路/short-circuiting计算,即惰性求值或者说正常顺序/normal order(SICP中译为正则序);例如有两个布尔表达式的逻辑操作be1&&f(),可以避免了执行不必要的表达式或方法调用。JDK中,Stream的中间函数如 filter(Predicate<? super T>)是惰性求值的,filter并非对流中所有元素调用传递给它的Predicate,Java8:Stream概念中我们见过,调换map()和filter()函数的调用顺序,可以减少函数执行的次数。
  • 通常的计算,即饥饿或者说应用序/applicative order,这里重点强调的是Java的方法实参的求值是饥饿计算,在方法被调用前,所有的实参通通被求值,即使方法中不使用某些实参也被求值。

    public static boolean isPositive(int x,int y) {        return x/y >0 ;    }    public static void eagerTest(boolean be1, boolean be2) {//方法实参的求值是饥饿的        pln(be1 && be2); //实参        pln(isPositive(-1,1) && isPositive(1,0)); //&&是短路的    }
eagerTest(isPositive(-1,1), isPositive(1,0));// /by zero

2.(实参的)lambda表达式的求值是惰性的


实参中有lambda表达式时,却让我们觉得lambda表达式的求值是惰性求值。其原因是,Java的lambda表达式是某个函数接口的匿名类的实现对象

这样就容易理解了,如通常的面向对象代码:dosth(A a),不会因为A有方法foo()而在dosth(new A())时执行A的方法foo();类比可知,dosth(() -> isPositive(1,0))也不会立即执行实参的lambda表达式。

    public static  Predicate<Integer> lazyTest(Predicate<Integer> p1, Predicate<Integer> p2) {       return p1.and(p2);    }

lazyTest(P  p1, P p2) 当传来两个lambda表达式,事实上对两个lambda表达式求值是给两个P类型的引用变量p1和p2赋值,并不计算lambda表达式的函数体。因此,lambda表达式的函数体的计算是惰性的,简称lambda表达式的求值是惰性。

        Predicate<Integer> p = lazyTest( x->x/1>0, x->x/0>0 );        
        pln(p.test(-5)); // false

    public static  boolean lazyTest2(Predicate<Integer> p1, Predicate<Integer> p2,int x) {        return p1.test(x) && p2.test(x);    }
       pln(lazyTest2( x->x/1>0, x->x/0>0 ,-5)); // false



例如有一个方法执行很长时间的运算(sleep模拟)后返回i>0。

    public static boolean isPositive(int i) {        pln("test ..." + i);        sleep(1000);//        return i > 0;    }

因为方法实参的求值是饥饿的,对于下面的代码

    public static void eagerTest(boolean be1, boolean be2) {//方法实参的求值是饥饿的        pln(be1 && be2); //实参        isPositive(-1) && isPositive(2); //&&是短路的            }
执行eagerTest(isPositive(-1), isPositive(2));的输出为:

test ...-1
test ...2
false
test ...-1
false

实参的lambda表达式在被调用时才求值

    public static void lazyTest(Supplier<Boolean> supplier1, Supplier<Boolean> supplier2) {        pln((supplier1.get() && supplier2.get()));    }    static class imp implements Supplier<Boolean>{        @Override public  Boolean get(){            return isPositive(-2);        }    }    public static void test() {        //eagerTest(isPositive(-1), isPositive(2));        pln("lazyTest-------------1");        lazyTest(() -> isPositive(-1), () -> isPositive(2));        pln("lazyTest-------------2");        lazyTest( new imp(), () -> isPositive(2)  );        pln("lazyTest-------------3");        lazyTest( new Supplier<Boolean>(){                @Override public  Boolean get(){                    return isPositive(-3);                }            }, () -> isPositive(3)  );    }
执行test()的输出为:
lazyTest-------------1
test ...-1
false
lazyTest-------------2
test ...-2
false
lazyTest-------------3
test ...-3
false

在进入lazyTest方法体之前,作为实参的两个lambda表达式并没有求值,而在调用supplier1.get()时才会求值。




0 0
原创粉丝点击