Files
dholle 2e5447c06b
SonarQube Scan / SonarQube Trigger (push) Failing after 2m17s
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 1m44s
Add Primzahlen, fixed a few problems
2026-09-10 16:22:00 +02:00

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());
}
}