import java.util.function.Supplier;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone {
public static void main (String[] args) throws java.lang.Exception {
var ll = Lazy.list( () -> print(1), () -> print(2), () -> print(3) );
ll.tail().head();
}
private static Unit print(Object o) {
System.out.println(o);
return Unit.UNIT;
}
}
final class Lazy {
public static Promise delay(Supplier expr) {
return new Promise<>(expr);
}
public static List cons(Supplier headExpr, Supplier> tailExpr) {
return new List<>(new Promise<>(headExpr), new Promise<>(tailExpr));
}
public static List nil() {
@SuppressWarnings("unchecked")
var nil = (List) List.NIL;
return nil;
}
@SafeVarargs
public static List list(Supplier... values) {
if (values == null || values.length == 0) {
return nil();
}
return list(values, 0);
}
private static List list(Supplier[] values, int headCursor) {
if (headCursor == values.length) {
return nil();
}
return new List<>(new Promise<>(values[headCursor]), new Promise<>(() -> list(values, headCursor+1)));
}
private Lazy() {
throw new UnsupportedOperationException("utility class");
}
}
final class List {
static final List