Added Abstraction.
This commit is contained in:
12
abstraction/.classpath
Normal file
12
abstraction/.classpath
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="lib" path="/home/jacob/School/CS3/libs/javalib.jar"/>
|
||||
<classpathentry kind="lib" path="/home/jacob/School/CS3/libs/tester.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
abstraction/.project
Normal file
17
abstraction/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>abstraction</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
2
abstraction/.settings/org.eclipse.core.resources.prefs
Normal file
2
abstraction/.settings/org.eclipse.core.resources.prefs
Normal file
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
11
abstraction/.settings/org.eclipse.jdt.core.prefs
Normal file
11
abstraction/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
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
|
93
abstraction/src/abstraction/Main.java
Normal file
93
abstraction/src/abstraction/Main.java
Normal file
@@ -0,0 +1,93 @@
|
||||
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; }
|
||||
}
|
@@ -21,10 +21,10 @@ class Examples {
|
||||
}
|
||||
|
||||
boolean testILoArmax(Tester t) {
|
||||
return t.checkExpect(helloWorld.argmax(new IEvaluator<StrLen>()), "Hello, ")
|
||||
&& t.checkExpect(helloAjcmsdj.argmax(new IEvaluator<StrLen>()), "Hello, ")
|
||||
&& t.checkExpect(lint2.argmax(new IEvaluator<IntSize>()), 1)
|
||||
&& t.checkExpect(lint.argmax(new IEvaluator<IntSize>()), 92);
|
||||
return t.checkExpect(helloWorld.argmax(new StrLen()), "Hello, ")
|
||||
&& t.checkExpect(helloAjcmsdj.argmax(new StrLen()), "Hello, ")
|
||||
&& t.checkExpect(lint2.argmax(new IntSize()), 1)
|
||||
&& t.checkExpect(lint.argmax(new IntSize()), 92);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,22 +35,25 @@ 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 IFunc<String, Integer> { public Integer apply(String input) {
|
||||
class StrLen implements IEvaluator<String> { public Integer apply(String input) {
|
||||
return input.length();
|
||||
}}
|
||||
|
||||
// Calculate the size of an integer.
|
||||
class IntSize implements IFunc<Integer, Integer> { public Integer apply(Integer input) {
|
||||
return input - 14;
|
||||
class IntSize implements IEvaluator<Integer> { public Integer apply(Integer input) {
|
||||
return input;
|
||||
}}
|
||||
|
||||
// Extracts the value of an input.
|
||||
interface IEvaluator<X> extends IFunc<X, Integer> {}
|
||||
|
||||
// A list of A.
|
||||
interface ILo<A> {
|
||||
ILo<A> append(ILo<A> a);
|
||||
A argmax(IEvaluator<A> eval);
|
||||
A argmaxHelper(IEvaluator<A> eval, A prev, Integer prevVal);
|
||||
ILo<A> skip();
|
||||
}
|
||||
|
||||
class Cons<A> implements ILo<A> {
|
||||
@@ -65,7 +68,20 @@ class Cons<A> implements ILo<A> {
|
||||
public ILo<A> append(ILo<A> a) { return new Cons<A>(this.first, this.rest.append(a)); }
|
||||
|
||||
public A argmax(IEvaluator<A> eval) {
|
||||
return null;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,4 +90,10 @@ class Mt<A> implements ILo<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; }
|
||||
}
|
Reference in New Issue
Block a user