Updated Generics.
This commit is contained in:
@@ -3,55 +3,106 @@ package generics;
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
|
||||
ILo<Integer> mint = new Mt<Integer>();
|
||||
ILo<Integer> lint = new Cons<Integer>(1, new Cons<Integer>(92, new Cons<Integer>(-12, mint)));
|
||||
ILo<Integer> lint = new Cons<Integer>(
|
||||
1,
|
||||
new Cons<Integer>(92, new Cons<Integer>(-12, mint))
|
||||
);
|
||||
ILo<Integer> lint2 = new Cons<Integer>(1, new Cons<Integer>(1, mint));
|
||||
|
||||
ILo<String> mstr = new Mt<String>();
|
||||
ILo<String> hello = new Cons<String>("Hello, ", mstr);
|
||||
ILo<String> world = new Cons<String>("world.", mstr);
|
||||
ILo<String> ajcmsdj = new Cons<String>("ajcmsdj", mstr);
|
||||
ILo<String> helloWorld = new Cons<String>("Hello, ", new Cons<String>("world.", 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)));
|
||||
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) &&
|
||||
return (
|
||||
t.checkExpect(hello.append(world), helloWorld) &&
|
||||
t.checkExpect(lint.append(mint), lint) &&
|
||||
t.checkExpect(mint.append(lint), 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<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));
|
||||
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))));
|
||||
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))
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Generic function.
|
||||
@@ -63,14 +114,20 @@ interface IFunc<X, Y> {
|
||||
interface IEvaluator<X> extends IFunc<X, Integer> {}
|
||||
|
||||
// Calculate the length of a string.
|
||||
class StrLen implements IEvaluator<String> { public Integer apply(String input) {
|
||||
class StrLen implements IEvaluator<String> {
|
||||
|
||||
public Integer apply(String input) {
|
||||
return input.length();
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the size of an integer.
|
||||
class IntSize implements IEvaluator<Integer> { public Integer apply(Integer input) {
|
||||
class IntSize implements IEvaluator<Integer> {
|
||||
|
||||
public Integer apply(Integer input) {
|
||||
return input;
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
// Compares two values.
|
||||
interface IComparator<A> {
|
||||
@@ -81,18 +138,26 @@ interface IComparator<A> {
|
||||
}
|
||||
|
||||
// Compare string lengths.
|
||||
class CompStrLen implements IComparator<String> { public int compare(String s1, String s2) {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
@@ -119,6 +184,7 @@ interface ILo<A> {
|
||||
}
|
||||
|
||||
class Cons<A> implements ILo<A> {
|
||||
|
||||
A first;
|
||||
ILo<A> rest;
|
||||
|
||||
@@ -127,7 +193,9 @@ class Cons<A> implements ILo<A> {
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
public ILo<A> append(ILo<A> a) { return new Cons<A>(this.first, this.rest.append(a)); }
|
||||
public ILo<A> append(ILo<A> a) {
|
||||
return new Cons<A>(this.first, this.rest.append(a));
|
||||
}
|
||||
|
||||
public A argmax(IEvaluator<A> eval) {
|
||||
return argmaxHelper(eval, this.first, eval.apply(this.first));
|
||||
@@ -137,10 +205,10 @@ class Cons<A> implements ILo<A> {
|
||||
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<A> skip() {
|
||||
return this.rest;
|
||||
@@ -151,16 +219,25 @@ class Cons<A> implements ILo<A> {
|
||||
}
|
||||
|
||||
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
|
||||
return this.rest.sortHelper(comp, sorted.insertSorted(comp, this.first));
|
||||
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));
|
||||
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> {
|
||||
public ILo<A> append(ILo<A> a) { return a; }
|
||||
|
||||
public ILo<A> append(ILo<A> a) {
|
||||
return a;
|
||||
}
|
||||
|
||||
public A argmax(IEvaluator<A> eval) {
|
||||
throw new UnsupportedOperationException("No max of empty list.");
|
||||
}
|
||||
@@ -169,12 +246,18 @@ class Mt<A> implements ILo<A> {
|
||||
return prev;
|
||||
}
|
||||
|
||||
public ILo<A> skip() { return this; }
|
||||
public ILo<A> skip() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILo<A> sort(IComparator<A> comp) {
|
||||
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