broken files

This commit is contained in:
Yadunand Prem 2022-10-29 15:23:10 +08:00
parent bde57516df
commit 931a1241b9
4 changed files with 262 additions and 0 deletions

151
Lab8/CS2030STest.java Normal file
View File

@ -0,0 +1,151 @@
import java.net.URI;
import java.util.List;
import java.util.function.Supplier;
import javax.tools.DiagnosticCollector;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
/**
* A helper class to test CS2030S labs.
*/
class CS2030STest {
private static final String ANSI_RESET = "\u001B[0m";
private static final String ANSI_RED = "\u001B[31m";
private static final String ANSI_GREEN = "\u001B[32m";
/**
* Test if two objects are equals.
*
* @param test A description of the test.
* @param output The output from an expression.
* @param expect The expected output from that expression.
* @return this object.
*/
public CS2030STest expect(String test, Object output, Object expect) {
System.out.print(test);
if ((expect == null && output == null) || output.equals(expect)) {
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
} else {
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
System.out.println(" expected: " + expect);
System.out.println(" got this: " + output);
}
return this;
}
/**
* Test if a given supplier produces a given object.
*
* @param <T> The type of object the given task will produce.
* @param test A description of the test.
* @param task The task to run.
* @param expect The expected output from that expression.
* @return this object.
*/
public <T> CS2030STest expect(String test, Supplier<T> task, Object expect) {
System.out.print(test);
try {
T output = task.get();
if ((expect == null && output == null) || output.equals(expect)) {
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
} else {
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
System.out.println(" expected: " + expect);
System.out.println(" got this: " + output);
}
} catch (Exception e) {
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
System.out.println(" with exception: " + e.getMessage());
e.printStackTrace();
}
return this;
}
/**
* Test if a given producer returns a value. Wrapper around expect(..)
* to simplify caller.
*
* @param <T> The type of object the given task will produce.
* @param test A description of the test.
* @param task The task to run.
* @param expect The expected output from that expression.
* @return this object.
*/
public <T> CS2030STest expectReturn(String test, Supplier<T> task, Object expect) {
return this.expect(test + " returns " + expect, task, expect);
}
/**
* Test if an expression throws an exception.
*
* @param test A description of the test.
* @param task A lambda expression of the expression.
* @param expectedE A exception instance of the same type as the expected one.
* @return this object.
*/
public CS2030STest expectException(String test, Runnable task, Exception expectedE) {
System.out.print(test + " throws " + expectedE.getClass().getSimpleName());
boolean gotException = false;
try {
task.run();
} catch (Exception e) {
if (e.getClass().equals(expectedE.getClass())) {
gotException = true;
}
}
if (gotException) {
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
} else {
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
System.out.println(" did not catch expected exception " + expectedE.getClass());
}
return this;
}
/**
* Test if an expression compiles with/without error.
*
* @param test A description of the test.
* @param statement The java statement to compile
* @param success Whether the statement is expected to compile or not
* (true if yes; false otherwise)
* @return this object.
*/
public CS2030STest expectCompile(String test, String statement, boolean success) {
System.out.print(test);
class JavaSourceFromString extends SimpleJavaFileObject {
final String code;
JavaSourceFromString(String code) {
super(URI.create("string:///TempClass.java"), Kind.SOURCE);
this.code = "class TempClass {void foo(){" + code + ";}}";
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
boolean noError = ToolProvider
.getSystemJavaCompiler()
//.getTask(null, null, new DiagnosticCollector<>(), null, null,
.getTask(null, null, null, null, null,
List.of(new JavaSourceFromString(statement)))
.call();
if (noError != success) {
System.out.println(".. " + ANSI_RED + "failed" + ANSI_RESET);
if (!success) {
System.out.println(" expected compilation error but it compiles fine.");
} else {
System.out.println(" expected the statement to compile without errors but it does not.");
}
} else {
System.out.println(".. " + ANSI_GREEN + "ok" + ANSI_RESET);
}
return this;
}
}

19
Lab8/Lab8.h Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
mkdir cs2030s
cd cs2030s
mkdir fp
cd ..
mv Action.java cs2030s/fp/Action.java
mv Actionable.java cs2030s/fp/Actionable.java
mv Actually.java cs2030s/fp/Actually.java
mv Combiner.java cs2030s/fp/Combiner.java
mv Constant.java cs2030s/fp/Constant.java
mv Immutator.java cs2030s/fp/Immutator.java
mv Immutatorable.java cs2030s/fp/Immutatorable.java
mv Memo.java cs2030s/fp/Memo.java
mv Memo.java cs2030s/fp/InfiniteList.java
javac cs2030s/fp/*.java
javac *.java

22
Lab8/MyTest.java Normal file
View File

@ -0,0 +1,22 @@
import java.util.ArrayList;
import java.util.List;
import cs2030s.fp.InfiniteList;
public class MyTest {
public static void main(String[] args) {
List<Integer> generateHistory = new ArrayList<>();
List<Integer> doublerHistory = new ArrayList<>();
InfiniteList.generate(() -> {
generateHistory.add(1);
return 1;
}).map(x -> {
doublerHistory.add(x);
return x * 2;
}).tail().head();
System.out.println(generateHistory);
System.out.println(doublerHistory);
System.out.println(InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).head());
}
}

70
Lab8/cs2030s/fp/Memo.java Normal file
View File

@ -0,0 +1,70 @@
package cs2030s.fp;
/**
* A container class the encapsulate a
* lazily-evaluated-and-memoized value.
*
* @author Adi Yoga S. Prabawa
* @version CS2030S AY 22/23 Sem 1
*/
public class Memo<T> implements Immutatorable<T> {
private Constant<? extends T> com;
private Actually<T> val;
private Memo(Actually<T> val, Constant<T> com) {
this.com = com;
this.val = val;
}
public static <T> Memo<T> from(T val) {
return new Memo<T>(Actually.ok(val), null);
}
public static <T> Memo<T> from(Constant<T> com) {
return new Memo<T>(Actually.err(), com);
}
public T get() {
this.eval();
return this.val.unless(null);
}
private void eval() {
if (this.com != null) {
this.val = Actually.<T>ok(this.com.init());
this.com = null;
}
}
@Override
public <R> Memo<R> transform(Immutator<? extends R, ? super T> f) {
return Memo.<R>from(() -> f.invoke(this.get()));
}
public <R> Memo<R> next(Immutator<? extends Memo<? extends R>, ? super T> f) {
return Memo.<R>from(() -> f.invoke(this.get()).get());
}
public <S, R> Memo<R> combine(Memo<S> snd, Combiner<? extends R, ? super T, ? super S> f) {
return Memo.<R>from(() -> f.combine(this.get(), snd.get()));
}
@Override
public String toString() {
if (this.com != null) {
return "?";
}
return this.get().toString();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Memo<?>)) {
return false;
}
Memo<?> that = (Memo<?>) obj;
that.eval();
this.eval();
return this.val.equals(that.val);
}
}