Finished Generics.
This commit is contained in:
@@ -13,6 +13,10 @@ class Examples {
|
||||
ILo<String> ajcmsdj = new Cons<String>("ajcmsdj", mstr);
|
||||
ILo<String> helloWorld = new Cons<String>("Hello, ", new Cons<String>("world.", mstr));
|
||||
ILo<String> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
|
||||
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
|
||||
ILo<String> hh = new Cons<String>("hh", new Cons<String>("hhhh", new Cons<String>("h", mstr)));
|
||||
ILo<String> hhh = new Cons<String>("a", new Cons<String>("b", new Cons<String>("c", mstr)));
|
||||
ILo<String> hhhh = new Cons<String>("b", new Cons<String>("a", new Cons<String>("c", mstr)));
|
||||
|
||||
boolean testILoAppend(Tester t) {
|
||||
return t.checkExpect(hello.append(world), helloWorld) &&
|
||||
@@ -27,6 +31,26 @@ class Examples {
|
||||
&& t.checkExpect(lint.argmax(new IntSize()), 92);
|
||||
}
|
||||
|
||||
boolean testILoInsertSorted(Tester t) {
|
||||
return t.checkExpect(mstr.insertSorted(new CompStrLen(), "h"), new Cons<String>("h", mstr))
|
||||
&& t.checkExpect(hello.insertSorted(new CompStrLen(), "s"), new Cons<String>("s", hello))
|
||||
&& t.checkExpect(h.insertSorted(new CompStrLen(), "hhh"), new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhh", new Cons<String>("hhhh", mstr)))))
|
||||
&& t.checkExpect(hello.insertSorted(new CompStrLen(), "hello"), new Cons<String>("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<String>("world.", new Cons<String>("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<String>(new StrLen())), h)
|
||||
&& t.checkExpect(lint.sort(new Comp<Integer>(new IntSize())), new Cons<Integer>(-12, new Cons<Integer>(1, new Cons<Integer>(92, mint))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,14 +70,52 @@ class StrLen implements IEvaluator<String> { public Integer apply(String input)
|
||||
// Calculate the size of an integer.
|
||||
class IntSize implements IEvaluator<Integer> { public Integer apply(Integer input) {
|
||||
return input;
|
||||
}}
|
||||
|
||||
// Compares two values.
|
||||
interface IComparator<A> {
|
||||
// Returns a negative number if a1 is "smaller" than a2,
|
||||
// 0 if they are considered equal,
|
||||
// and a positive number of a1 is "bigger" than a2.
|
||||
int compare(A a1, A a2);
|
||||
}
|
||||
|
||||
// Compare string lengths.
|
||||
class CompStrLen implements IComparator<String> { public int compare(String s1, String s2) {
|
||||
return s1.length() - s2.length();
|
||||
}}
|
||||
|
||||
// Compare strings alphabetically.
|
||||
class CompStrAlp implements IComparator<String> { public int compare(String s1, String s2) {
|
||||
return s1.compareTo(s2);
|
||||
}}
|
||||
|
||||
// Compare the sizes of two things with the given Evaluator.
|
||||
class Comp<A> implements IComparator<A> {
|
||||
IEvaluator<A> eval;
|
||||
Comp(IEvaluator<A> eval) {
|
||||
this.eval = eval;
|
||||
}
|
||||
|
||||
public int compare(A a1, A a2) {
|
||||
return eval.apply(a1) - eval.apply(a2);
|
||||
}
|
||||
}
|
||||
|
||||
// A list of A.
|
||||
interface ILo<A> {
|
||||
// Append a list.
|
||||
ILo<A> append(ILo<A> a);
|
||||
// Find the largest from the evaluator.
|
||||
A argmax(IEvaluator<A> eval);
|
||||
A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal);
|
||||
// Skip this element of the list.
|
||||
ILo<A> skip();
|
||||
// Sort the list with the comparator.
|
||||
ILo<A> sort(IComparator<A> comp);
|
||||
ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted);
|
||||
// Add an element to the correct place in a sorted list.
|
||||
ILo<A> insertSorted(IComparator<A> comp, A a);
|
||||
}
|
||||
|
||||
class Cons<A> implements ILo<A> {
|
||||
@@ -83,6 +145,18 @@ class Cons<A> implements ILo<A> {
|
||||
public ILo<A> skip() {
|
||||
return this.rest;
|
||||
}
|
||||
|
||||
public ILo<A> sort(IComparator<A> comp) {
|
||||
return this.rest.sortHelper(comp, new Cons<A>(this.first, new Mt<A>()));
|
||||
}
|
||||
|
||||
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
|
||||
return this.rest.sortHelper(comp, sorted.insertSorted(comp, this.first));
|
||||
}
|
||||
|
||||
public ILo<A> insertSorted(IComparator<A> comp, A a) {
|
||||
return comp.compare(a, this.first) <= 0 ? new Cons<A>(a ,this) : new Cons<A>(this.first, this.rest.insertSorted(comp, a));
|
||||
}
|
||||
}
|
||||
|
||||
class Mt<A> implements ILo<A> {
|
||||
@@ -96,4 +170,12 @@ class Mt<A> implements ILo<A> {
|
||||
}
|
||||
|
||||
public ILo<A> skip() { return this; }
|
||||
}
|
||||
|
||||
public ILo<A> sort(IComparator<A> comp) { return this; }
|
||||
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
|
||||
return sorted;
|
||||
}
|
||||
public ILo<A> insertSorted(IComparator<A> comp, A a) {
|
||||
return new Cons<A>(a, this);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user