40 lines
1.3 KiB
Java
40 lines
1.3 KiB
Java
//Grundsaetzlich wird Lazy-Evaluation so realisiert, dass immer beim
|
|
//Methoden/Konstruktor-Aufruf das Argument in einen Lambda-Ausdruck (Supplier)
|
|
//eingepackt wird (siehe Aufruf von Cons und Empty in Count und main) und
|
|
//ein Lazy-Argument mit get ausgerollt wird (siehe Methode rest)
|
|
|
|
import java.lang.Integer;
|
|
import java.lang.String;
|
|
import java.lang.System;
|
|
import java.io.PrintStream;
|
|
import java.util.function.Supplier;
|
|
|
|
public sealed interface LazyList permits Empty, Cons {
|
|
public Integer fst();
|
|
public LazyList rest();
|
|
}
|
|
|
|
//Der Konstruktor Cons muss lazy sein, deshalb hier Supplier<...>
|
|
record Cons(Integer x, Supplier<LazyList> l) implements LazyList {
|
|
public Integer fst() { return this.x; }
|
|
public LazyList rest() { return this.l.get(); }
|
|
public String toString() {
|
|
return "Cons(" + this.x.toString() + ", " + this.l.get().toString() + ")";
|
|
}
|
|
}
|
|
|
|
record Empty() implements LazyList {
|
|
public Integer fst() { return -1;}
|
|
public LazyList rest() { return null; }
|
|
}
|
|
|
|
|
|
class Main {
|
|
static LazyList Count(int i) { return new Cons(i, () -> Count(i+1)); }
|
|
|
|
public static void main(args) {
|
|
System.out.println(new Cons(1, () -> new Cons(2, () -> new Empty())).fst());
|
|
System.out.println(Count(1).rest().fst());
|
|
}
|
|
}
|