Passing Functions in Scala
- def g(x: () => Double) : Double =
- val x1 = x()
- val x2 = x()
- x1 - x2
- end g
- println ("g= " + g (() => Math.random()))
Emulate in OOP (Java)
- interface java.util.function.Supplier<T> { T get(); }
- class GetRnd implements Supplier<Double> {
- public Double get() { return Math.random(); }
- }
- class Client {
- public static double g(Supplier<Double> t) {
- double x1 = t.get();
- double x2 = t.get();
- return x1-x2;
- }
- }
- System.out.println ("g= " + g (new GetRnd()));
- System.out.println ("g= " + g (() -> Math.random()));