... while on the subject of "power of expression" ...
Luc Duponcheel
luc.duponcheel at gmail.com
Fri Mar 15 10:36:55 PDT 2013
Here is an (agreed, somewhat contrived) example
of what *is* possible with the current lambda: a pure functional while (see
below)
btw:
the definition is tail recursive
does that also mean that it is replaced by iteration?
[ I had a look at the byte code via "javap -c" but I am not a specialist ]
... just wanted to share the example (sorry for those who are not
interested) ...
ps: the formatting (I used netbeans) is not optimal
=== begin example ===
package whatever;
import java.util.function.Function;
import java.util.function.Predicate;
public class While_App {
private static <T> Function<Predicate<T>, Function<Function<T, T>,
Function<T, T>>> while_() {
return predicate
-> function
-> t -> {
if (predicate.test(t)) {
return While_App.<T>while_().apply(predicate).apply( // tail recursive!
function).apply(function.apply(t));
} else {
return t;
}
};
}
private static Function<Predicate<Integer>, Function<Function<Integer,
Integer>, Function<Integer, Integer>>> integerWhile = While_App.while_();
public static void main(String[] args) {
int j =
integerWhile.apply(i -> i > 0).apply( // while(i > 0) {
i -> {
System.out.println("i = " + i); // System.out.println("i =
" + i);
return i - 1; // i = i - 1
}).apply(10); // int i = 10;
System.out.println("j = " + j);
}
;
}
=== end example ===
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
More information about the lambda-dev
mailing list