This commit is contained in:
2024-11-25 21:10:17 -05:00
parent 4e474e0598
commit e612c6263d
168 changed files with 90 additions and 11350 deletions

1
.gitignore vendored
View File

@@ -13,3 +13,4 @@ replay_pid*
**/bin/
.old/
.metadata/
**/.classpath

View File

@@ -1,5 +0,0 @@
package abstraction;
public class Main {
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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)));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,162 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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);
}
boole
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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);
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 new Cons<A>(a, this);
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,146 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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)))));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,7 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21

View File

@@ -1,180 +0,0 @@
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> 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> 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) &&
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);
}
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(new StrLen())), h);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,151 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) >= 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,151 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) >= 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,163 +0,0 @@
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> 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> 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) &&
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);
}
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);
}
boole
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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);
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,5 +0,0 @@
package abstraction;
class Examples {
}

View File

@@ -1,149 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) >= 0) {
return new Cons<A>(a, new Cons<A>(this.first, this.rest));
}
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,155 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,131 +0,0 @@
package fibonacci;
import tester.Tester;
class Examples {
ISequence<Integer> f;
ISequence<String> a;
ISeqGen<String> aGen = new AGen();
void init() {
this.f = new Fibonacci();
this.a = new GenSeq<String>("", aGen);
}
void testFibonacci(Tester t) {
init();
t.checkExpect(f.get(), 0);
t.checkExpect(f.get(), 1);
t.checkExpect(f.get(), 1);
t.checkExpect(f.get(), 2);
t.checkExpect(f.get(), 3);
t.checkExpect(f.get(), 5);
t.checkExpect(f.get(), 8);
}
void testASeq(Tester t) {
init();
t.checkExpect(a.get(), "");
t.checkExpect(a.get(), "a");
t.checkExpect(a.get(), "aa");
t.checkExpect(a.get(), "aaa");
t.checkExpect(a.get(), "aaaa");
t.checkExpect(a.get(), "aaaaa");
}
}
// A generic list.
interface ILo<A> {}
class Cons<A> implements ILo<A> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
}
class Mt<A> implements ILo<A> {}
interface ISequence<X> {
// Returns the current element in the sequence.
// EFFECT: Updates the state to the next element in the sequence.
X get();
}
// Sequence generator method.
interface ISeqGen<X> {
X gen(X state); // Generates the next value in the sequence given the current state.
}
// The Fibonacci sequence.
class Fibonacci implements ISequence<Integer> {
int idx; // Number of times `get()` has been called.
int pre; // The previous (last) number.
int pen; // The penultimate (second to last) number.
Fibonacci() {
this.pen = 0;
this.pre = 1;
}
// Get the next in the sequence.
public Integer get() {
int ret; // What to return.
/*
idx pen pre ret
0 0 1 0
1 0 1 1
2 1 1 2
3 1 2 3
4 2 3 5
5 3 5 8
*/
// F(0) = 0; F(1) = 1
if (this.idx <= 1) ret = this.idx;
else { // F(n) = F(n-1) + F(n-2)
ret = this.pre + this.pen;
this.pen = this.pre;
this.pre = ret;
}
this.idx = this.idx + 1;
return ret;
}
}
// A generic sequence.
class GenSeq<X> implements ISequence<X> {
X state; // The current state of the sequence.
ISeqGen<X> seqGen; // The method by which new values are generated.
// Create new generic sequence given an initial state.
GenSeq(X init, ISeqGen<X> seqGen) {
this.state = init;
this.seqGen = seqGen;
}
public X get() {
X ret = this.state;
this.state = this.seqGen.gen(this.state);
return ret;
}
}
// A string of n "a"s.
class AGen implements ISeqGen<String> {
public String gen(String state) {
return state.concat("a");
}
}

View File

@@ -1,153 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,149 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) == 0) {
}
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,155 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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 hh.CompStr()), h);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,153 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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)));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,94 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean after(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByShortness()).after(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByAlpha()).after(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
return new StringsAfter(new OrderByAlpha()).after(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,151 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) >= 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,94 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean after(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByShortness()).after(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
return new StringsAfter(new OrderByAlpha()).after(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,154 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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)));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,155 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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 null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,176 +0,0 @@
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> 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> 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) &&
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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 eval;
Comp(IEvaluator eval) {
this.eval = eval;
}
public int compare()
}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,153 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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)));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean apply(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByShortness().before(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,155 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,151 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) >= 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,94 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean apply(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByShortness()).apply(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
return new StringsAfter(new OrderByAlpha()).apply(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,7 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21

View File

@@ -1,155 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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 null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,89 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByShortness().before(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,151 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) > 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,156 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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) {
ILo<A> newsort = sorted.insertSorted(comp, this.first);
return this.rest.sortHelper(comp, newsort);
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean apply(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByAlpha()).apply(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,151 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) >= 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,153 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates an ordering.
class Disorder {
IStringsBefore ordering;
Disorder(IStringsBefore ordering) { this.ordering = ordering; }
boolean after(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByShortness()).after(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByAlpha()).after(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByAlpha()).after(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates an ordering.
class Disorder {
IStringsBefore ordering;
Disorder(IStringsBefore ordering) { this.ordering = ordering; }
boolean after(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByShortness()).after(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByAlpha()).after(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByOrderkt hi ()).after(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean apply(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByShortness().before(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,151 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) <= 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,180 +0,0 @@
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> 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> 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) &&
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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)));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,153 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) <= 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,84 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByShortness().before(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}
Main.java

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) == 0)
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,178 +0,0 @@
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> 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> 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) &&
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,832 +0,0 @@
package mastermind;
import java.awt.Color;
import java.awt.Image;
import java.util.Random;
import javalib.funworld.*;
import javalib.worldcanvas.*;
import javalib.worldimages.*;
import tester.Tester;
class Examples {
ILoInt noInt = new MtInt();
ILoInt intsOne = new ConsInt(1, new ConsInt(2, new ConsInt(3, noInt)));
Dot redDot = new Dot(Color.red), greenDot = new Dot(Color.green), blueDot =
new Dot(Color.blue), reenDot = new Dot(new Color(255, 255, 0));
ILoDot noDot = new MtDot(), dotsOne = new ConsDot(
redDot,
new ConsDot(greenDot, new ConsDot(blueDot, noDot)),
new Random(0)
), dotsTwo = new ConsDot(
redDot,
new ConsDot(blueDot, new ConsDot(blueDot, noDot))
), dotsThree = new ConsDot(
reenDot,
new ConsDot(reenDot, new ConsDot(reenDot, noDot))
), exampleDotsOne = new ConsDot(
redDot,
new ConsDot(
greenDot,
new ConsDot(
blueDot,
new ConsDot(
greenDot,
new ConsDot(redDot, new ConsDot(greenDot, noDot))
)
)
)
), exampleDotsTwo = new ConsDot(
redDot,
new ConsDot(
blueDot,
new ConsDot(
greenDot,
new ConsDot(
greenDot,
new ConsDot(blueDot, new ConsDot(blueDot, noDot))
)
)
)
), exampleDotsOneNoExact = new ConsDot(
greenDot,
new ConsDot(blueDot, new ConsDot(redDot, new ConsDot(greenDot, noDot)))
), exampleDotsTwoNoExact = new ConsDot(
blueDot,
new ConsDot(greenDot, new ConsDot(blueDot, new ConsDot(blueDot, noDot)))
);
Feedback exampleFeedback = new Feedback(2, 2);
Game exampleGame = new Game();
boolean testILoDotLen(Tester t) {
return t.checkExpect(noDot.len(), 0) && t.checkExpect(dotsOne.len(), 3);
}
boolean testILoDotGetnth(Tester t) {
return (
t.checkException(
new IllegalArgumentException("Index out of bounds."),
noDot,
"get",
1
) &&
t.checkException(
new IllegalArgumentException("Index out of bounds."),
dotsOne,
"get",
3
) &&
t.checkExpect(dotsOne.get(0), redDot) &&
t.checkExpect(dotsOne.get(2), blueDot)
);
}
boolean testILoDotGen(Tester t) {
return (
t.checkExpect(noDot.gen(219), noDot) &&
t.checkExpect(dotsOne.gen(1), new ConsDot(redDot, noDot)) &&
t.checkExpect(
dotsOne.gen(3),
new ConsDot(
greenDot,
new ConsDot(greenDot, new ConsDot(blueDot, noDot))
)
)
);
}
boolean testILoDotIn(Tester t) {
return (
t.checkExpect(dotsOne.in(blueDot), true) &&
t.checkExpect(noDot.in(redDot), false) &&
t.checkExpect(dotsThree.in(blueDot), false)
);
}
boolean testILoIntRemove(Tester t) {
return (
t.checkExpect(
intsOne.remove(0),
new ConsInt(2, new ConsInt(3, noInt))
) &&
t.checkExpect(
intsOne.remove(2),
new ConsInt(1, new ConsInt(2, noInt))
) &&
t.checkException(
new IllegalArgumentException(
"Index out of bounds: cannot remove something from nothing."
),
intsOne,
"remove",
3
) &&
t.checkException(
new IllegalArgumentException(
"Index out of bounds: cannot remove something from nothing."
),
noInt,
"remove",
0
) &&
t.checkException(
new IllegalArgumentException("Indices must be positive."),
intsOne,
"remove",
-2
)
);
}
boolean testILoIntLen(Tester t) {
return t.checkExpect(intsOne.len(), 3) && t.checkExpect(noInt.len(), 0);
}
boolean testILoIntSubOne(Tester t) {
return t.checkExpect(
intsOne.subOne(),
new ConsInt(0, new ConsInt(1, new ConsInt(2, noInt)))
);
}
boolean testILoDotExactIndices(Tester t) {
return (
t.checkException(
new IllegalArgumentException(
"Empty list doesn't exactly match anything."
),
noDot,
"exactIndices",
noDot
) &&
t.checkExpect(
dotsOne.exactIndices(dotsOne),
new ConsInt(0, new ConsInt(1, new ConsInt(2, noInt)))
) &&
t.checkExpect(
dotsOne.exactIndices(dotsTwo),
new ConsInt(0, new ConsInt(2, new MtInt()))
)
);
}
boolean testILoDotRemove(Tester t) {
return (
t.checkException(
new IllegalArgumentException("Index out of bounds."),
noDot,
"remove",
0
) &&
t.checkExpect(
dotsOne.remove(0),
new ConsDot(greenDot, new ConsDot(blueDot, noDot))
) &&
t.checkExpect(
dotsTwo.remove(1),
new ConsDot(redDot, new ConsDot(blueDot, noDot))
)
);
}
boolean testILoDotRemoveAll(Tester t) {
return (
t.checkExpect(dotsOne.removeAll(noInt), dotsOne) &&
t.checkExpect(
dotsOne.removeAll(
new ConsInt(0, new ConsInt(1, new ConsInt(2, noInt)))
),
noDot
) &&
t.checkExpect(noDot.removeAll(intsOne), noDot) &&
t.checkExpect(
dotsTwo.removeAll(new ConsInt(1, new ConsInt(2, noInt))),
new ConsDot(redDot, noDot)
)
);
}
boolean testILoDotCountInexact(Tester t) {
return (
t.checkExpect(noDot.countInexact(noDot), 0) &&
t.checkExpect(dotsOne.countInexact(dotsOne), 3) &&
t.checkExpect(
exampleDotsOneNoExact.countInexact(exampleDotsTwoNoExact),
2
)
);
}
boolean testILoDotCompare(Tester t) {
return (
t.checkExpect(
exampleDotsOne.compare(exampleDotsTwo),
exampleFeedback
) &&
t.checkExpect(dotsOne.compare(dotsOne), new Feedback(3, 0))
);
}
boolean testDrawMethods(Tester t) {
// WorldImage incomplete = new IncompleteGuess(exampleDotsOne).draw();
//WorldImage guesses = new ConsGuess(new Guess(dotsOne, new Feedback(1, 2)), new ConsGuess(new Guess(dotsTwo, new Feedback(2, 5)), new MtGuess())).draw();
Game game = new Game().addGuess(new Guess(dotsOne, exampleFeedback));
WorldImage gameImg = game.draw();
int w = (int) gameImg.getWidth();
int h = (int) gameImg.getHeight();
WorldCanvas c = new WorldCanvas(w, h);
WorldScene s = new WorldScene(w, h);
return game.launch();
//c.drawScene(s.placeImageXY(gameImg, w/2, h/2))
//c.drawScene(s.placeImageXY(incomplete, (int) (incomplete.getWidth()/2), 100))
//c.drawScene(s.placeImageXY(exampleFeedback.draw(), 100, 100))
//c.drawScene(s.placeImageXY(redDot.draw(), 100, 100))
//c.drawScene(s.placeImageXY(dotsOne.draw(), 250, 250))
//&& c.show();
//true;
}
}
class Util {
static int scale = 2;
static int fontSz = 24 * scale;
static int gapW = 4 * scale; // The gap between objects.
// Drawing methods.
static WorldImage gap = new RectangleImage(
gapW,
gapW,
"outline",
new Color(0, 0, 0, 0)
);
static WorldImage pairGap(WorldImage img1, WorldImage img2) {
return new BesideAlignImage(AlignModeY.MIDDLE, img1, gap, img2);
}
static WorldImage pairGapAbove(WorldImage img1, WorldImage img2) {
return new AboveAlignImage(AlignModeX.CENTER, img1, gap, img2);
}
static WorldImage strPair(String str1, String str2) {
return pairGap(
new TextImage(str1, fontSz, Color.black),
new TextImage(str2, fontSz, Color.black)
);
}
static int pairgapW = 2 * 16 * scale + gapW;
}
// A game state.
class Game extends World {
static ILoDot DEFAULTDOTS = new ConsDot(
new Dot(Color.RED),
new ConsDot(
new Dot(Color.GREEN),
new ConsDot(new Dot(Color.BLUE), new MtDot())
)
);
static GameConf DEFAULTCONF = new GameConf(true, 5, 5, DEFAULTDOTS);
GameConf conf; // The game's configuration.
ILoDot solution; // The solution to the game.
int guessesLeft; // The number of guesses the player has left.
ILoGuess guesses; // The guesses the player has taken.
boolean done; // Is the game over?
boolean won; // Did they player win?
Game(
GameConf conf,
ILoDot solution,
int guessesLeft,
ILoGuess guesses,
boolean won,
boolean done
) {
if (this.won && !this.done) throw new IllegalArgumentException(
"Can't win before you've finished playing."
);
this.conf = conf;
this.solution = solution;
this.won = won;
this.done = done;
this.guessesLeft = guessesLeft;
this.guesses = guesses;
}
// Convenience constructor using default config and starting values.
Game() {
this(
DEFAULTCONF,
DEFAULTCONF.options.gen(DEFAULTCONF.len),
DEFAULTCONF.nguesses,
new MtGuess(),
false,
false
);
}
public WorldScene makeScene() {
WorldScene bg = this.getEmptyScene();
WorldImage dot = new Dot(Color.RED).draw();
return bg.placeImageXY(
dot,
(int) dot.getWidth(),
(int) dot.getHeight()
);
}
boolean launch() {
return this.bigBang(100, 100);
}
// Draw the current game state.
WorldImage draw() {
return new AboveAlignImage(
AlignModeX.LEFT,
this.draw_sol(),
this.draw_guesses(),
this.draw_options()
);
}
// Draw the solution.
WorldImage draw_sol() {
if (this.done) return this.draw_sol_rev();
else return this.draw_sol_hid();
}
// Draw the revealed answer.
WorldImage draw_sol_rev() {
return this.solution.draw();
}
// Draw the hidden answer.
WorldImage draw_sol_hid() {
return new RectangleImage(
this.solution.getW(),
2 * Dot.r,
OutlineMode.SOLID,
Color.BLACK
);
}
WorldImage draw_guesses() {
return this.guesses.draw();
}
WorldImage draw_options() {
return this.conf.options.draw();
}
// Convenience methods for testing -- not part of program.
Game win() {
return new Game(
this.conf,
this.solution,
this.guessesLeft,
this.guesses,
true,
true
);
}
Game addGuess(Guess guess) {
return new Game(
this.conf,
this.solution,
this.guessesLeft,
new ConsGuess(guess, this.guesses),
this.won,
this.done
);
}
}
// A game configuration.
class GameConf {
boolean dups; // Whether duplicates are allowed.
int len; // The length of the sequence to be guessed.
int nguesses; // Number of guesses the player is allowed.
ILoDot options; // The dots of which the solution is comprised.
GameConf(boolean dups, int len, int nguesses, ILoDot options) {
if (len <= 0) throw new IllegalArgumentException(
"Length of the solution must be greater than 0."
);
if (nguesses <= 0) throw new IllegalArgumentException(
"Must provide the player some guesses."
);
int oplen = options.len();
if (oplen <= 0) throw new IllegalArgumentException(
"Must have dot options to guess with."
);
if (!dups && len > oplen) throw new IllegalArgumentException(
"Cant create solution of that length without duplicates."
);
this.dups = dups;
this.len = len;
this.nguesses = nguesses;
this.options = options;
}
}
// A list of guesses.
interface ILoGuess {
WorldImage draw();
}
class ConsGuess implements ILoGuess {
Guess guess;
ILoGuess nxt;
ConsGuess(Guess guess, ILoGuess nxt) {
this.guess = guess;
this.nxt = nxt;
}
public WorldImage draw() {
return Util.pairGapAbove(guess.draw(), this.nxt.draw());
}
}
class MtGuess implements ILoGuess {
public WorldImage draw() {
return new EmptyImage();
}
}
// A guess.
class Guess {
ILoDot guess; // The dots the user entered.
Feedback feedback; // The feedback returned.
Guess(ILoDot guess, Feedback feedback) {
this.guess = guess;
this.feedback = feedback;
}
WorldImage draw() {
return Util.pairGap(this.guess.draw(), this.feedback.draw());
}
}
// A guess in the midst of being entered.
class IncompleteGuess {
ILoDot guessSoFar;
IncompleteGuess(ILoDot guessSoFar) {
this.guessSoFar = guessSoFar;
}
WorldImage draw() {
return guessSoFar.draw();
}
}
// Feedback for a guess.
class Feedback {
int exact, inexact;
Feedback(int exact, int inexact) {
this.exact = exact;
this.inexact = inexact;
}
Feedback add(Feedback other) {
int otherExact = other.exact, otherInexact = other.inexact;
return new Feedback(
this.exact + otherExact,
this.inexact + otherInexact
);
}
WorldImage draw() {
return Util.strPair(
String.valueOf(this.exact),
String.valueOf(this.exact)
);
}
}
// A list of dots.
interface ILoDot {
int len(); // Get length.
Dot get(int n); // Get nth element.
ILoDot remove(int n); // Remove nth element.
ILoDot removeAll(ILoInt indices); // Remove element at each index.
ILoDot gen(int n); // Generate randomized list.
boolean match(Color col); // Do the colors match?
boolean in(Dot dot); // Is the dot in here?
Feedback compare(ILoDot other); // Compare two lists & give feedback.
ILoInt exactIndices(ILoDot other); // Get indices of exact matches.
ILoInt exactIndicesHelper(ILoDot other, int i);
int countInexact(ILoDot other); // Get the number of inexact matches. Must be fed exact match-free lists to be accurate.
int countInexactHelper(ILoDot other, ILoDot seen);
WorldImage draw(); // Draw the dots.
int getW(); // Get the total width, in pixels, of the list.
}
class ConsDot implements ILoDot {
Random rand;
Dot dot;
ILoDot nxt;
ConsDot(Dot dot, ILoDot nxt, Random rand) {
this.rand = rand;
this.dot = dot;
this.nxt = nxt;
}
ConsDot(Dot dot, ILoDot nxt) {
this(dot, nxt, new Random());
}
public ILoDot gen(int n) {
return n <= 0
? new MtDot()
: new ConsDot(
this.get(this.rand.nextInt(this.len())),
this.gen(n - 1)
);
}
public Dot get(int n) {
if (n == 0) return this.dot;
else return this.nxt.get(n - 1);
}
public boolean in(Dot dot) {
return dot.equals(this.dot) || this.nxt.in(dot);
}
public int len() {
return 1 + this.nxt.len();
}
public boolean match(Color col) {
return col.equals(this.dot.c);
}
public WorldImage draw() {
return new BesideAlignImage(
AlignModeY.PINHOLE,
this.dot.draw(),
Util.gap,
this.nxt.draw()
);
}
public int getW() {
return this.len() * Dot.r * 2;
}
public Feedback compare(ILoDot other) {
if (this.len() != other.len()) throw new IllegalArgumentException(
"Cannot compare different lengthed lists."
);
ILoInt exactIndices = this.exactIndices(other);
ILoDot thisWithoutExact = this.removeAll(exactIndices);
ILoDot otherWithoutExact = other.removeAll(exactIndices);
int exact = exactIndices.len();
int inexact = thisWithoutExact.countInexact(otherWithoutExact);
return new Feedback(exact, inexact);
}
public ILoInt exactIndices(ILoDot other) {
return this.exactIndicesHelper(other, 0);
}
public ILoInt exactIndicesHelper(ILoDot other, int i) {
// Stop after reaching the end of the list.
if (i == this.len()) return new MtInt();
return this.get(i).equals(other.get(i))
? new ConsInt(i, this.exactIndicesHelper(other, i + 1))
: this.exactIndicesHelper(other, i + 1);
}
public int countInexact(ILoDot other) {
return this.countInexactHelper(other, new MtDot());
}
public int countInexactHelper(ILoDot other, ILoDot seen) {
if (seen.in(this.dot)) {
return this.nxt.countInexactHelper(
other,
new ConsDot(this.dot, seen)
);
}
return (
(other.in(this.dot) ? 1 : 0) +
this.nxt.countInexactHelper(other, new ConsDot(this.dot, seen))
);
}
public ILoDot remove(int n) {
if (n < 0) throw new IllegalArgumentException(
"Indices must be positive."
);
if (n == 0) return this.nxt;
else return new ConsDot(this.dot, this.nxt.remove(n - 1));
}
public ILoDot removeAll(ILoInt indices) {
if (indices.isEmpty()) return this;
return this.remove(indices.first()).removeAll(indices.rest().subOne());
}
}
class MtDot implements ILoDot {
public ILoDot gen(int n) {
return new MtDot();
}
public Dot get(int n) {
throw new IllegalArgumentException("Index out of bounds.");
}
public int len() {
return 0;
}
public WorldImage draw() {
return new EmptyImage();
}
public int getW() {
return 0;
}
public Feedback compare(ILoDot other) {
return new Feedback(0, 0);
}
public boolean match(Color col) {
return false;
}
public boolean in(Dot dot) {
return false;
}
public ILoInt exactIndices(ILoDot other) {
throw new IllegalArgumentException(
"Empty list doesn't exactly match anything."
);
}
public ILoInt exactIndicesHelper(ILoDot other, int i) {
return new MtInt();
}
public int countInexact(ILoDot other) {
return 0;
}
public int countInexactHelper(ILoDot other, ILoDot seen) {
return 0;
}
public ILoDot remove(int n) {
throw new IllegalArgumentException("Index out of bounds.");
}
public ILoDot removeAll(ILoInt indices) {
return this;
}
}
// A dot.
class Dot {
static int r = 16 * Util.scale;
static OutlineMode outlineMode = OutlineMode.SOLID;
Color c;
Dot(Color c) {
this.c = c;
}
// Draw the dot.
WorldImage draw() {
return new CircleImage(r, outlineMode, this.c);
}
// Get the width of the dot.
int getW() {
return 2 * r;
}
}
// A list of integers.
interface ILoInt {
int len(); // Get the length of the list.
ILoInt remove(int n); // Remove the nth element of the list.
boolean isEmpty(); // Is it empty?
int first(); // Get the first value.
ILoInt rest(); // Get the rest value.
ILoInt subOne(); // Subtract 1 from every int.
}
class ConsInt implements ILoInt {
int val;
ILoInt nxt;
ConsInt(int val, ILoInt nxt) {
this.val = val;
this.nxt = nxt;
}
public int len() {
return 1 + this.nxt.len();
}
public ILoInt remove(int n) {
if (n < 0) throw new IllegalArgumentException(
"Indices must be positive."
);
return n == 0
? this.nxt
: new ConsInt(this.val, this.nxt.remove(n - 1));
}
public boolean isEmpty() {
return false;
}
public int first() {
return this.val;
}
public ILoInt rest() {
return this.nxt;
}
public ILoInt subOne() {
return new ConsInt(this.val - 1, this.nxt.subOne());
}
}
class MtInt implements ILoInt {
public int len() {
return 0;
}
public ILoInt remove(int n) {
throw new IllegalArgumentException(
"Index out of bounds: cannot remove something from nothing."
);
}
public boolean isEmpty() {
return true;
}
public int first() {
throw new IllegalArgumentException(
"Cannot get first element in a list without any."
);
}
public ILoInt rest() {
throw new IllegalArgumentException("Cannot get rest of empty list.");
}
public ILoInt subOne() {
return this;
}
}

View File

@@ -1,7 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean apply(String s1, String s2) {
return ! this.ordering(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByShortness().before(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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))));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,174 +0,0 @@
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> 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> 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) &&
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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 implements IComparator<String> {
IEvaluator eval;
Comp(IEvaluator eval) {
this.eval = eval;
}
}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates an ordering.
class Disorder {
IStringsBefore ordering;
Disorder(IStringsBefore ordering) { this.ordering = ordering; }
boolean after(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByShortness()).after(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByAlpha()).after(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByOrder()).after(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean after(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByShortness()).after(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByAlpha()).after(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByAlpha()).after(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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)));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,160 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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);
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,82 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByShortness().before(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,166 +0,0 @@
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> 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> 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) &&
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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);
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean apply(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsBefore(new OrderByShortness()).apply(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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)));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,156 +0,0 @@
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> 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> 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)));
boolean testILoAppend(Tester t) {
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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) {
ILo<A> newsort = 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));
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean after(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByShortness()).after(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByAlpha()).after(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return new Disorder(new OrderByAlpha()).after(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,130 +0,0 @@
package fibonacci;
import tester.Tester;
class Examples {
ISequence<Integer> f;
ISequence<String> a;
ISeqGen<String> aGen = new AGen();
void init() {
this.f = new Fibonacci();
this.a = new GenSeq<String>("", aGen);
}
void testFibonacci(Tester t) {
init();
t.checkExpect(f.get(), 0);
t.checkExpect(f.get(), 1);
t.checkExpect(f.get(), 1);
t.checkExpect(f.get(), 2);
t.checkExpect(f.get(), 3);
t.checkExpect(f.get(), 5);
t.checkExpect(f.get(), 8);
}
void testASeq(Tester t) {
init();
t.checkExpect(a.get(), "");
t.checkExpect(a.get(), "a");
t.checkExpect(a.get(), "aa");
t.checkExpect(a.get(), "aaa");
t.checkExpect(a.get(), "aaaa");
t.checkExpect(a.get(), "aaaaa");
}
}
// A generic list.
interface ILo<A> {}
class Cons<A> implements ILo<A> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
}
class Mt<A> implements ILo<A> {}
interface ISequence<X> {
// Returns the current element in the sequence.
// EFFECT: Updates the state to the next element in the sequence.
X get();
}
// Sequence generator method.
interface ISeqGen<X> {
X gen(X state); // Generates the next value in the sequence given the current state.
}
// The Fibonacci sequence.
class Fibonacci implements ISequence<Integer> {
int idx; // Number of times `get()` has been called.
int pre; // The previous (last) number.
int pen; // The penultimate (second to last) number.
Fibonacci() {
this.pen = 0;
this.pre = 1;
}
// Get the next in the sequence.
public Integer get() {
int ret; // What to return.
/*
idx pen pre ret
0 0 1 0
1 0 1 1
2 1 1 2
3 1 2 3
4 2 3 5
5 3 5 8
*/
// F(0) = 0; F(1) = 1
if (this.idx <= 1) ret = this.idx;
else { // F(n) = F(n-1) + F(n-2)
ret = this.pre + this.pen;
this.pen = this.pre;
this.pre = ret;
}
this.idx = this.idx + 1;
return ret;
}
}
// A generic sequence.
class GenSeq<X> implements ISequence<X> {
X state; // The current state of the sequence.
ISeqGen<X> seqGen; // The method by which new values are generated.
// Create new generic sequence given an initial state.
GenSeq(X init, ISeqGen<X> seqGen) {
this.state = init;
this.seqGen = seqGen;
}
public X get() {
X ret = this.state;
this.state = this.seqGen.gen(this.state);
return ret;
}
}
// A string of n "a"s.
class AGen implements ISeqGen<String> {
public String gen(String state) {
return state.concat("a");
}}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean apply(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByShortness()).apply(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,149 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) >= 0) {
return new Cons<A>(a, this);
}
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first))
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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))));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,176 +0,0 @@
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> 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> 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) &&
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);
}
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);
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
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> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean after(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByShortness()).after(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return new StringsAfter(new OrderByAlpha()).after(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,94 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
boolean apply(String s1, String s2) {
return ! this.ordering.before(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByShortness().before(s1, s2);
return new StringsBefore(new OrderByShortness()).apply(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,147 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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)));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return null;
}
}
class Mt<A> implements ILo<A> {
public ILo<A> append(ILo<A> a) { return a; }
public A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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 null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
return new Cons<A>(a, this);
}
}

View File

@@ -1,5 +0,0 @@
package fibonacci;
public class Main {
}

View File

@@ -1,153 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) <= 0) {
return new Cons<A>(a, this);
} else {
return new Cons<A>(this.first, this.rest.insertSorted(comp, a));
}
return comp.compare(a, this.first) ? 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,93 +0,0 @@
package abstraction;
import tester.Tester;
class Examples {
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
boolean tests(Tester t) {
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
}
}
interface IStringsBefore {
boolean before(String s1, String s2);
}
// Negates a IStringsBefore.
class StringsAfter {
IStringsBefore ordering;
StringsAfter(IStringsBefore ordering) { this.ordering = ordering; }
apply(String s1, String s2) {
return ! this.ordering(s1, s2);
}
}
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.length() <= s2.length();
}}
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
return s1.compareTo(s2) <= 0;
}}
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
return true;
}}
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByShortness().before(s1, s2);
}}
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByAlpha().before(s1, s2);
}}
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
return ! new OrderByOrder().before(s1, s2);
}}
// A non-empty list of strings.
interface ILoStrings {
String val(); // Gets the value.
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
String earliestHelper(IStringsBefore ordering, String prev);
}
abstract class ALoStrings implements ILoStrings {
String val;
ALoStrings(String val) { this.val = val; }
public String val() { return this.val; }
}
class ConsString extends ALoStrings {
ILoStrings nxt;
ConsString(String val, ILoStrings nxt) {
super(val);
this.nxt = nxt;
}
public String earliest(IStringsBefore ordering) {
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
}
public String earliestHelper(IStringsBefore ordering, String prev) {
return new ConsString(prev, this.nxt).earliest(ordering);
}
}
class LastString extends ALoStrings {
LastString(String val) { super(val); }
public String earliest(IStringsBefore ordering) { return this.val; }
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
}

View File

@@ -1,151 +0,0 @@
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> 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> helloAjcmsdj = new Cons<String>("Hello, ", ajcmsdj);
ILo<String> h = new Cons<String>("h", new Cons<String>("hh", new Cons<String>("hhhh", mstr)));
boolean testILoAppend(Tester t) {
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);
}
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));
}
}
// Generic function.
interface IFunc<X, Y> {
Y apply(X input);
}
// Extracts the value of an input.
interface IEvaluator<X> extends IFunc<X, Integer> {}
// Calculate the length of a string.
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) {
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();
}}
// 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> {
A first;
ILo<A> rest;
Cons(A first, ILo<A> rest) {
this.first = first;
this.rest = rest;
}
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));
}
public A argmaxHelper(IEvaluator<A> 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);
};
public ILo<A> skip() {
return this.rest;
}
public ILo<A> sort(IComparator<A> comp) {
return null;
}
public ILo<A> sortHelper(IComparator<A> comp, ILo<A> sorted) {
return null;
}
public ILo<A> insertSorted(IComparator<A> comp, A a) {
if (comp.compare(a, this.first) <= 0) {
return new Cons<A>(a, this);
} else {
return 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 A argmax(IEvaluator<A> eval) {
throw new UnsupportedOperationException("No max of empty list.");
}
public A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal) {
return prev;
}
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);
}
}

View File

@@ -1,3 +1,3 @@
#GitProjectData
#Wed Nov 13 09:09:56 EST 2024
#Thu Nov 21 00:04:34 EST 2024
.gitdir=../.git

View File

@@ -1,3 +1,3 @@
#GitProjectData
#Tue Nov 05 15:25:59 EST 2024
#Thu Nov 21 00:04:34 EST 2024
.gitdir=../.git

View File

@@ -1,3 +1,3 @@
#GitProjectData
#Mon Nov 18 11:29:02 EST 2024
#Thu Nov 21 00:04:34 EST 2024
.gitdir=../.git

View File

@@ -1,3 +1,3 @@
#GitProjectData
#Tue Nov 12 12:12:54 EST 2024
#Thu Nov 21 00:04:34 EST 2024
.gitdir=../.git

Some files were not shown because too many files have changed in this diff Show More