Added Generics.
This commit is contained in:
12
generics/.classpath
Normal file
12
generics/.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
generics/.project
Normal file
17
generics/.project
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>generics</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
generics/.settings/org.eclipse.core.resources.prefs
Normal file
2
generics/.settings/org.eclipse.core.resources.prefs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding/<project>=UTF-8
|
11
generics/.settings/org.eclipse.jdt.core.prefs
Normal file
11
generics/.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
|
77
generics/src/generics/Main.java
Normal file
77
generics/src/generics/Main.java
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
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 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generic function.
|
||||||
|
interface IFunc<X, Y> {
|
||||||
|
Y apply(X input);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the length of a string.
|
||||||
|
class StrLen implements IFunc<String, Integer> { 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;
|
||||||
|
}}
|
||||||
|
|
||||||
|
// Extracts the value of an input.
|
||||||
|
interface IEvaluator<X> extends IFunc<X, Integer> {}
|
||||||
|
|
||||||
|
interface ILo<A> {
|
||||||
|
ILo<A> append(ILo<A> a);
|
||||||
|
A argmax(IEvaluator<A> eval);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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.");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user