diff --git a/generics/src/generics/Main.java b/generics/src/generics/Main.java index e06534f..3300ad6 100644 --- a/generics/src/generics/Main.java +++ b/generics/src/generics/Main.java @@ -3,55 +3,106 @@ package generics; import tester.Tester; class Examples { + ILo mint = new Mt(); - ILo lint = new Cons(1, new Cons(92, new Cons(-12, mint))); + ILo lint = new Cons( + 1, + new Cons(92, new Cons(-12, mint)) + ); ILo lint2 = new Cons(1, new Cons(1, mint)); - + ILo mstr = new Mt(); ILo hello = new Cons("Hello, ", mstr); ILo world = new Cons("world.", mstr); ILo ajcmsdj = new Cons("ajcmsdj", mstr); - ILo helloWorld = new Cons("Hello, ", new Cons("world.", mstr)); + ILo helloWorld = new Cons( + "Hello, ", + new Cons("world.", mstr) + ); ILo helloAjcmsdj = new Cons("Hello, ", ajcmsdj); - ILo h = new Cons("h", new Cons("hh", new Cons("hhhh", mstr))); - ILo hh = new Cons("hh", new Cons("hhhh", new Cons("h", mstr))); - ILo hhh = new Cons("a", new Cons("b", new Cons("c", mstr))); - ILo hhhh = new Cons("b", new Cons("a", new Cons("c", mstr))); - + ILo h = new Cons( + "h", + new Cons("hh", new Cons("hhhh", mstr)) + ); + ILo hh = new Cons( + "hh", + new Cons("hhhh", new Cons("h", mstr)) + ); + ILo hhh = new Cons( + "a", + new Cons("b", new Cons("c", mstr)) + ); + ILo hhhh = new Cons( + "b", + new Cons("a", new Cons("c", mstr)) + ); + boolean testILoAppend(Tester t) { - return t.checkExpect(hello.append(world), helloWorld) && - t.checkExpect(lint.append(mint), lint) && - t.checkExpect(mint.append(lint), lint); + return ( + t.checkExpect(hello.append(world), helloWorld) && + t.checkExpect(lint.append(mint), lint) && + t.checkExpect(mint.append(lint), lint) + ); } - + boolean testILoArmax(Tester t) { - return t.checkExpect(helloWorld.argmax(new StrLen()), "Hello, ") - && t.checkExpect(helloAjcmsdj.argmax(new StrLen()), "Hello, ") - && t.checkExpect(lint2.argmax(new IntSize()), 1) - && t.checkExpect(lint.argmax(new IntSize()), 92); + return ( + t.checkExpect(helloWorld.argmax(new StrLen()), "Hello, ") && + t.checkExpect(helloAjcmsdj.argmax(new StrLen()), "Hello, ") && + t.checkExpect(lint2.argmax(new IntSize()), 1) && + t.checkExpect(lint.argmax(new IntSize()), 92) + ); } - + boolean testILoInsertSorted(Tester t) { - return t.checkExpect(mstr.insertSorted(new CompStrLen(), "h"), new Cons("h", mstr)) - && t.checkExpect(hello.insertSorted(new CompStrLen(), "s"), new Cons("s", hello)) - && t.checkExpect(h.insertSorted(new CompStrLen(), "hhh"), new Cons("h", new Cons("hh", new Cons("hhh", new Cons("hhhh", mstr))))) - && t.checkExpect(hello.insertSorted(new CompStrLen(), "hello"), new Cons("hello", hello)); + return ( + t.checkExpect( + mstr.insertSorted(new CompStrLen(), "h"), + new Cons("h", mstr) + ) && + t.checkExpect( + hello.insertSorted(new CompStrLen(), "s"), + new Cons("s", hello) + ) && + t.checkExpect( + h.insertSorted(new CompStrLen(), "hhh"), + new Cons( + "h", + new Cons( + "hh", + new Cons("hhh", new Cons("hhhh", mstr)) + ) + ) + ) && + t.checkExpect( + hello.insertSorted(new CompStrLen(), "hello"), + new Cons("hello", hello) + ) + ); } - + boolean testILoSort(Tester t) { - return t.checkExpect(h.sort(new CompStrLen()), h) - && t.checkExpect(mstr.sort(new CompStrLen()), mstr) - && t.checkExpect(helloWorld.sort(new CompStrLen()), new Cons("world.", new Cons("Hello, ", mstr))) - && t.checkExpect(hh.sort(new CompStrLen()), h) - - && t.checkExpect(mstr.sort(new CompStrAlp()), mstr) - && t.checkExpect(hhh.sort(new CompStrAlp()), hhh) - && t.checkExpect(hhhh.sort(new CompStrAlp()), hhh) - - && t.checkExpect(hh.sort(new Comp(new StrLen())), h) - && t.checkExpect(lint.sort(new Comp(new IntSize())), new Cons(-12, new Cons(1, new Cons(92, mint)))); + return ( + t.checkExpect(h.sort(new CompStrLen()), h) && + t.checkExpect(mstr.sort(new CompStrLen()), mstr) && + t.checkExpect( + helloWorld.sort(new CompStrLen()), + new Cons("world.", new Cons("Hello, ", mstr)) + ) && + t.checkExpect(hh.sort(new CompStrLen()), h) && + t.checkExpect(mstr.sort(new CompStrAlp()), mstr) && + t.checkExpect(hhh.sort(new CompStrAlp()), hhh) && + t.checkExpect(hhhh.sort(new CompStrAlp()), hhh) && + t.checkExpect(hh.sort(new Comp(new StrLen())), h) && + t.checkExpect( + lint.sort(new Comp(new IntSize())), + new Cons( + -12, + new Cons(1, new Cons(92, mint)) + ) + ) + ); } - } // Generic function. @@ -63,14 +114,20 @@ interface IFunc { interface IEvaluator extends IFunc {} // Calculate the length of a string. -class StrLen implements IEvaluator { public Integer apply(String input) { - return input.length(); -}} +class StrLen implements IEvaluator { + + public Integer apply(String input) { + return input.length(); + } +} // Calculate the size of an integer. -class IntSize implements IEvaluator { public Integer apply(Integer input) { - return input; -}} +class IntSize implements IEvaluator { + + public Integer apply(Integer input) { + return input; + } +} // Compares two values. interface IComparator { @@ -81,29 +138,37 @@ interface IComparator { } // Compare string lengths. -class CompStrLen implements IComparator { public int compare(String s1, String s2) { - return s1.length() - s2.length(); -}} +class CompStrLen implements IComparator { + + public int compare(String s1, String s2) { + return s1.length() - s2.length(); + } +} // Compare strings alphabetically. -class CompStrAlp implements IComparator { public int compare(String s1, String s2) { - return s1.compareTo(s2); -}} +class CompStrAlp implements IComparator { + + public int compare(String s1, String s2) { + return s1.compareTo(s2); + } +} // Compare the sizes of two things with the given Evaluator. class Comp implements IComparator { + IEvaluator eval; + Comp(IEvaluator eval) { this.eval = eval; } - + public int compare(A a1, A a2) { return eval.apply(a1) - eval.apply(a2); } } // A list of A. -interface ILo { +interface ILo { // Append a list. ILo append(ILo a); // Find the largest from the evaluator. @@ -119,62 +184,80 @@ interface ILo { } class Cons implements ILo { + A first; ILo rest; - + Cons(A first, ILo rest) { this.first = first; this.rest = rest; } - - public ILo append(ILo a) { return new Cons(this.first, this.rest.append(a)); } - - public A argmax(IEvaluator eval) { + + public ILo append(ILo a) { + return new Cons(this.first, this.rest.append(a)); + } + + public A argmax(IEvaluator eval) { return argmaxHelper(eval, this.first, eval.apply(this.first)); } - + public A argmaxHelper(IEvaluator eval, A prev, Integer prevVal) { A now = this.first; Integer nowVal = eval.apply(now); - - return prevVal >= nowVal ? - this.rest.argmaxHelper(eval, prev, prevVal) : - this.rest.skip().argmaxHelper(eval, now, nowVal); - }; - + + return prevVal >= nowVal + ? this.rest.argmaxHelper(eval, prev, prevVal) + : this.rest.skip().argmaxHelper(eval, now, nowVal); + } + public ILo skip() { return this.rest; } - + public ILo sort(IComparator comp) { return this.rest.sortHelper(comp, new Cons(this.first, new Mt())); } - + public ILo sortHelper(IComparator comp, ILo sorted) { - return this.rest.sortHelper(comp, sorted.insertSorted(comp, this.first)); + return this.rest.sortHelper( + comp, + sorted.insertSorted(comp, this.first) + ); } - + public ILo insertSorted(IComparator comp, A a) { - return comp.compare(a, this.first) <= 0 ? new Cons(a ,this) : new Cons(this.first, this.rest.insertSorted(comp, a)); + return comp.compare(a, this.first) <= 0 + ? new Cons(a, this) + : new Cons(this.first, this.rest.insertSorted(comp, a)); } } class Mt implements ILo { - public ILo append(ILo a) { return a; } + + public ILo append(ILo a) { + return a; + } + public A argmax(IEvaluator eval) { throw new UnsupportedOperationException("No max of empty list."); } - + public A argmaxHelper(IEvaluator eval, A prev, Integer prevVal) { return prev; } - - public ILo skip() { return this; } - - public ILo sort(IComparator comp) { return this; } + + public ILo skip() { + return this; + } + + public ILo sort(IComparator comp) { + return this; + } + public ILo sortHelper(IComparator comp, ILo sorted) { return sorted; } + public ILo insertSorted(IComparator comp, A a) { return new Cons(a, this); }