Java8:Stream概念的细化,讨论延迟计算/惰性求值Lazy Evaluations。
1.惰性 Vs. 饥饿
求值策略决定函数的实参应该在何时被求值,以及实参以什么方式传递到函数体内。虽然看起来求值策略仅仅涉及到函数调用,其实表达式的操作符,条件表达式的?都可以归结为一个函数调用。函数的实参求值时机分两种:
- 饥饿求值(Strict/Eager Evaluation),应用序 (Applicative order,实用序)求值。简单地说,函数调用时先计算所有实参的值。在方法被调用前,所有的实参通通被求值,即使方法中不使用某些实参也被求值。实参的值计算之后,以什么方式传递到函数体内是应用序要考虑到第二个问题。在C/C++教学中一般会介绍按值传递(pass-by-value,或Call-by-value)和按引用传递(pass-by-reference)。
- 延迟计算/惰性求值 (delay computing /Lazy Evaluation),又被称为normal-order evaluation(正常序求值),其求值顺序按“最左最外/leftmost outermost”方式求值。如果采用这种方式,其过程“完全展开而后归约”(''fully expand and then reduce'')。这一求值策略,一个常见的应用是在Scheme特殊块或Java控制语句的短路计算。例如
(square (+ 1 2)) ;;;先展开square
=(* (+ 1 2) (+ 1 2))
= (* 3 3)
有两个布尔表达式的逻辑操作be1&&f(),可以避免了执行不必要的表达式或方法调用。
表达式求值的顺序有两种:
- 短路/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表达式。注:将一个饥饿求值的表达式,封装到一个无参函数中(这种函数有时被称为Thunk)。
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()时才会求值。