import cs2030s.fp.*; import java.util.Scanner; class Lab6 { public static void main(String[] args) { // Create a scanner to read from standard input. Scanner sc = new Scanner(System.in); // Read a single integer from the test file // and then run the appropriate test case switch (sc.nextInt()) { case 1: test1(); break; case 2: test2(); break; case 3: test3(); break; case 4: test4(); break; case 5: test5(); break; } } public static void test1() { Lazy mod1 = Lazy.from(2030); System.out.println(mod1.toString()); Lazy mod2 = Lazy.from(() -> "CS2030S"); System.out.println(mod2.toString()); Lazy hello = Lazy.from(() -> { System.out.println("world!"); return "hello"; }); System.out.println(hello.get().toString()); System.out.println(hello.get().toString()); } public static void test2() { Memo mod1 = Memo.from(2030); System.out.println(mod1.toString()); Memo mod2 = Memo.from(() -> "CS2030S"); System.out.println(mod2.toString()); System.out.println(mod2.get()); System.out.println(mod2.toString()); Memo hello = Memo.from(() -> { System.out.println("world!"); return "hello"; }); System.out.println(hello.get().toString()); System.out.println(hello.get().toString()); } public static void test3() { Constant password = () -> "123456"; Lazy lazy = Lazy.from(password); System.out.println(lazy.toString()); System.out.println(lazy.transform(str -> str.substring(0, 1)).toString()); Memo memo = Memo.from(password); System.out.println(memo.toString()); System.out.println(memo.transform(str -> str.substring(0, 1)).toString()); System.out.println(memo.transform(str -> str.substring(0, 1)).get().toString()); System.out.println(memo.toString()); Immutator len = str -> { System.out.println("length"); return str.length(); }; Lazy lazyLen = lazy.transform(len); System.out.println(lazyLen.toString()); System.out.println(lazyLen.get()); System.out.println(lazyLen.get()); Memo step1 = Memo.from(1010); Memo step2 = step1.transform(i -> i * 2); Memo step3 = step2.next(i -> Memo.from(i + 10)); System.out.println(step1.toString()); System.out.println(step2.toString()); System.out.println(step3.toString()); System.out.println(step3.get()); System.out.println(step2.toString()); System.out.println(step1.toString()); Memo noErr = Memo.from(0); Memo err = noErr.transform(x -> 1/x); try { System.out.println(err.get()); } catch(ArithmeticException ae) { System.out.println(ae.getMessage()); } } public static void test4() { Combiner concat = (x, y) -> { System.out.println("combine"); return x.toString() + y.toString(); }; Memo twenty, thirty, modInt; twenty = Memo.from(() -> 20); thirty = Memo.from(() -> 30); modInt = twenty.combine(thirty, (x, y) -> x * 100 + y); Memo modStr = twenty.combine(thirty, concat); System.out.println(twenty.toString()); System.out.println(thirty.toString()); System.out.println(modInt.toString()); System.out.println(modStr.toString()); System.out.println(modStr.get().toString()); System.out.println(twenty.toString()); System.out.println(thirty.toString()); System.out.println(modInt.toString()); Combiner comb = (x, y) -> x.toString() + " + " + y.toString(); Memo s = modInt.combine(Memo.from(0.1), comb); System.out.println(s.toString()); System.out.println(s.get()); System.out.println(modInt.toString()); Memo x1 = Memo.from(1); for (int i = 0; i < 10; i ++) { final Memo y1 = x1; // final just to ensure it is unchanged final int j = i; x1 = Memo.from(() -> { System.out.println(j); return y1.get() + y1.get(); }); } System.out.println(x1.get()); } public static void test5() { Constant t = new Constant<>() { public Boolean init() { return true; } }; Constant f = new Constant<>() { public Boolean init() { String res = ""; for (int i=0; i<100000; i++) { res += i; } return false; } }; Cond cond = new And(new Or(new Bool(t), new Bool(f)), new Not(new Not(new Bool(t)))); System.out.println(cond.toString()); System.out.println(cond.neg().toString()); System.out.println(cond.neg().neg().toString()); System.out.println(cond.eval()); System.out.println(cond.neg().toString()); System.out.println(cond.neg().neg().toString()); } }