Files
dholle 0998d231fe
SonarQube Scan / SonarQube Trigger (push) Failing after 2m6s
Fix package name
2026-09-23 15:34:05 +02:00

57 lines
1.8 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.Boolean;
import java.lang.String;
import java.lang.System;
import java.io.PrintStream;
public sealed interface LazyList permits Empty, Cons {
public Integer fst();
public LazyList rest();
}
public class Filter {
/*public LazyList filter(Fun1$$<Integer, Boolean> p, LazyList l) {
return switch(l) {
case Empty e -> e;
case Cons(Integer x, Supplier<LazyList> l1) ->
p.apply(x) ? new Cons(x, () -> filter(p, l1.get())) : filter(p, l1.get());
};
}*/
public LazyList filter(Fun1$$<Integer, Boolean> p, Empty()@l) {
return l;
}
public LazyList filter(Fun1$$<Integer, Boolean> p, Cons(Integer x, l1)) {
return p.apply(x) ? new Cons(x, () -> filter(p, l1.apply())) : filter(p, l1.apply());
}
}
//Der Konstruktor Cons muss lazy sein, deshalb hier Supplier<...>
record Cons(Integer x, Fun0$$<LazyList> l) implements LazyList {
public Integer fst() { return this.x; }
public LazyList rest() { return this.l.apply(); }
public String toString() {
return "Cons(" + this.x.toString() + ", " + this.l.apply().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());
}
}