Updated Generics.

This commit is contained in:
Jacob Signorovitch
2024-11-21 11:35:18 -05:00
parent e72eb6df7c
commit 1c9ca91c24

View File

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