feat: change up organisation
This commit is contained in:
151
labs/cs2030s/Lab8/CS2030STest.java
Normal file
151
labs/cs2030s/Lab8/CS2030STest.java
Normal 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
labs/cs2030s/Lab8/Lab8.h
Normal file
19
labs/cs2030s/Lab8/Lab8.h
Normal 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 InfiniteList.java cs2030s/fp/InfiniteList.java
|
||||
|
||||
javac cs2030s/fp/*.java
|
||||
javac *.java
|
||||
547
labs/cs2030s/Lab8/Lab8.java
Normal file
547
labs/cs2030s/Lab8/Lab8.java
Normal file
@@ -0,0 +1,547 @@
|
||||
import cs2030s.fp.*;
|
||||
import java.util.Scanner;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
class Lab8 {
|
||||
public static void main(String[] args) {
|
||||
// Create a scanner to read from standard input.
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
// Read a single integer from the test file
|
||||
// and then run the appropriate test case
|
||||
switch (sc.nextInt()) {
|
||||
case 1:
|
||||
test1();
|
||||
break;
|
||||
case 2:
|
||||
test2();
|
||||
break;
|
||||
case 3:
|
||||
test3();
|
||||
break;
|
||||
case 4:
|
||||
test4();
|
||||
break;
|
||||
case 5:
|
||||
test5();
|
||||
break;
|
||||
case 6:
|
||||
test6();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void test1() {
|
||||
System.out.println(InfiniteList.generate(() -> 1).toString());
|
||||
System.out.println(InfiniteList.generate(() -> 1).head());
|
||||
System.out.println(InfiniteList.generate(() -> null).tail().head());
|
||||
System.out.println(InfiniteList.iterate("A", x -> x + "Z").head());
|
||||
System.out.println(InfiniteList.iterate("A", x -> x + "Z").tail().head());
|
||||
System.out.println(InfiniteList.iterate("A", x -> x + "Z").tail().tail().head());
|
||||
|
||||
List<Integer> evalHistory = new ArrayList<>();
|
||||
Immutator<Integer, Integer> op = x -> {
|
||||
evalHistory.add(x);
|
||||
return x + 1;
|
||||
};
|
||||
|
||||
InfiniteList<Integer> numbers = InfiniteList.iterate(1, op);
|
||||
|
||||
System.out.println(numbers.head());
|
||||
System.out.println(numbers.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(numbers.tail().head());
|
||||
System.out.println(numbers.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(numbers.tail().head());
|
||||
System.out.println(numbers.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(numbers.tail().tail().head());
|
||||
System.out.println(numbers.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(numbers.tail().head());
|
||||
System.out.println(numbers.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
InfiniteList<Integer> zeros = InfiniteList.generate(() -> {
|
||||
evalHistory.add(0);
|
||||
return 0;
|
||||
});
|
||||
evalHistory.retainAll(List.of());
|
||||
System.out.println(zeros.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(zeros.head());
|
||||
System.out.println(zeros.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(zeros.tail().head());
|
||||
System.out.println(zeros.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(zeros.head());
|
||||
System.out.println(zeros.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(zeros.tail().head());
|
||||
System.out.println(zeros.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(zeros.tail().tail().head());
|
||||
System.out.println(zeros.toString());
|
||||
System.out.println(evalHistory);
|
||||
|
||||
System.out.println(zeros.tail().head());
|
||||
System.out.println(zeros.toString());
|
||||
System.out.println(evalHistory);
|
||||
}
|
||||
|
||||
public static void test2() {
|
||||
System.out.println(InfiniteList.generate(() -> 1).map(x -> x * 2).toString());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).toString());
|
||||
System.out.println(InfiniteList.generate(() -> 1).map(x -> x * 2).head());
|
||||
System.out.println(InfiniteList.generate(() -> 1).map(x -> x * 2).tail().head());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).head());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).tail().head());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).map(x -> x - 1).head());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).map(x -> x - 1).tail().head());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> x % 2 == 0 ? null : x).tail().head());
|
||||
|
||||
List<Integer> doublerHistory = new ArrayList<>();
|
||||
List<Integer> generateHistory = new ArrayList<>();
|
||||
Constant<Integer> generator = () -> {
|
||||
generateHistory.add(1);
|
||||
return 1;
|
||||
};
|
||||
Immutator<Integer, Integer> doubler = x -> {
|
||||
doublerHistory.add(x);
|
||||
return x * 2;
|
||||
};
|
||||
|
||||
System.out.println(InfiniteList.generate(generator).map(doubler).tail().head());
|
||||
System.out.println(generateHistory);
|
||||
System.out.println(doublerHistory);
|
||||
|
||||
generateHistory.retainAll(List.of());
|
||||
doublerHistory.retainAll(List.of());
|
||||
InfiniteList<Integer> ones = InfiniteList.generate(generator);
|
||||
InfiniteList<Integer> twos = ones.map(doubler);
|
||||
|
||||
System.out.println(twos.tail().head());
|
||||
System.out.println(ones.toString());
|
||||
System.out.println(twos.toString());
|
||||
System.out.println(generateHistory);
|
||||
System.out.println(doublerHistory);
|
||||
|
||||
System.out.println(twos.head());
|
||||
System.out.println(generateHistory);
|
||||
System.out.println(doublerHistory);
|
||||
|
||||
System.out.println(twos.tail().head());
|
||||
System.out.println(generateHistory);
|
||||
System.out.println(doublerHistory);
|
||||
}
|
||||
|
||||
public static void test3() {
|
||||
System.out.println(InfiniteList.generate(() -> 1).filter(x -> x % 2 == 0).toString());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).toString());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).head());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).filter(x -> x > 4).head());
|
||||
|
||||
List<Integer> incrHistory = new ArrayList<>();
|
||||
List<Integer> isEvenHistory = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> isEven = x -> {
|
||||
isEvenHistory.add(x);
|
||||
return x % 2 == 0;
|
||||
};
|
||||
Immutator<Integer, Integer> incr = x -> {
|
||||
incrHistory.add(x);
|
||||
return x + 1;
|
||||
};
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(isEven).tail().head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(isEvenHistory);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
|
||||
InfiniteList<Integer> nums = InfiniteList.iterate(1, incr);
|
||||
InfiniteList<Integer> evens = nums.filter(isEven);
|
||||
|
||||
System.out.println(evens.tail().head());
|
||||
System.out.println(nums.toString());
|
||||
System.out.println(evens.toString());
|
||||
System.out.println(nums.tail().head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(isEvenHistory);
|
||||
|
||||
System.out.println(evens.tail().head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(isEvenHistory);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
|
||||
List<Integer> moreThan5History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> moreThan5 = x -> {
|
||||
moreThan5History.add(x);
|
||||
return x > 5;
|
||||
};
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(moreThan5).filter(isEven).head());
|
||||
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(isEvenHistory);
|
||||
System.out.println(moreThan5History);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
moreThan5History.retainAll(List.of());
|
||||
|
||||
List<Integer> doublerHistory = new ArrayList<>();
|
||||
Immutator<Integer, Integer> doubler = x -> {
|
||||
doublerHistory.add(x);
|
||||
return x * 2;
|
||||
};
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).map(doubler).filter(moreThan5).filter(isEven)
|
||||
.tail().head());
|
||||
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(isEvenHistory);
|
||||
System.out.println(moreThan5History);
|
||||
System.out.println(doublerHistory);
|
||||
|
||||
doublerHistory.retainAll(List.of());
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
moreThan5History.retainAll(List.of());
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(isEven).map(doubler).filter(moreThan5).head());
|
||||
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(isEvenHistory);
|
||||
System.out.println(moreThan5History);
|
||||
System.out.println(doublerHistory);
|
||||
}
|
||||
|
||||
public static void test4() {
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).isEnd());
|
||||
System.out.println(InfiniteList.generate(() -> 2).isEnd());
|
||||
System.out.println(InfiniteList.generate(() -> 2).filter(x -> x % 3 == 0).isEnd());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> 2).isEnd());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> 2).isEnd());
|
||||
System.out.println(InfiniteList.end().isEnd());
|
||||
System.out.println(InfiniteList.end().map(x -> 2).isEnd());
|
||||
System.out.println(InfiniteList.end().filter(x -> true).isEnd());
|
||||
System.out.println(InfiniteList.end().filter(x -> false).isEnd());
|
||||
System.out.println(InfiniteList.end().limit(4).isEnd());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).limit(0).isEnd());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).limit(0).isEnd());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).limit(1).isEnd());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).limit(10).isEnd());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).limit(-1).isEnd());
|
||||
|
||||
List<Integer> incrHistory = new ArrayList<>();
|
||||
Immutator<Integer, Integer> incr = x -> {
|
||||
incrHistory.add(x);
|
||||
return x + 1;
|
||||
};
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(0).isEnd());
|
||||
System.out.println(incrHistory);
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(1).isEnd());
|
||||
System.out.println(incrHistory);
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(10).isEnd());
|
||||
System.out.println(incrHistory);
|
||||
|
||||
System.out.println(InfiniteList.generate(() -> 1).limit(4).toString());
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(4).toString());
|
||||
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(0).head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(1).head());
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(4).head());
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(1).tail().head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(4).tail().tail().head());
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(4).limit(1).tail().head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(1).limit(4).tail().head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
Immutator<Boolean, Integer> isEven = x -> (x % 2 == 0);
|
||||
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(isEven).limit(0).head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(isEven).limit(1).head());
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(1).filter(isEven).head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(2).filter(isEven).head());
|
||||
System.out.println(InfiniteList.iterate("A", s -> s + "Z").limit(2).map(s -> s.length()).head());
|
||||
System.out.println(InfiniteList.iterate("A", s -> s + "Z").limit(2).map(s -> s.length())
|
||||
.tail().head());
|
||||
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate("A", s -> s + "Z").limit(2).map(s -> s.length()).tail().tail()
|
||||
.head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(InfiniteList.iterate("A", s -> s + "Z").map(s -> s.length()).head());
|
||||
System.out.println(InfiniteList.iterate("A", s -> s + "Z").map(s -> s.length()).tail().head());
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate("A", s -> s + "Z").map(s -> s.length()).limit(2).tail().tail()
|
||||
.head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(InfiniteList.<String>end().toList());
|
||||
System.out.println(InfiniteList.iterate("A", s -> s + "Z").map(s -> s.length()).limit(2).toList());
|
||||
System.out.println(InfiniteList.iterate("A", s -> s + "Z").limit(2).map(s -> s.length()).toList());
|
||||
System.out.println(InfiniteList.iterate(1, incr).limit(2).filter(isEven).toList());
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(isEven).limit(2).toList());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).limit(10).limit(3).toList());
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).limit(3).limit(10).toList());
|
||||
System.out.println(InfiniteList.generate(() -> 4).limit(0).toList());
|
||||
System.out.println(InfiniteList.generate(() -> 4).limit(2).toList());
|
||||
System.out.println(InfiniteList.iterate(0, x -> x + 1).filter(x -> x > 10).map(x -> x.hashCode() % 30)
|
||||
.filter(x -> x < 20).limit(5).toList());
|
||||
|
||||
java.util.Random rng = new java.util.Random(1);
|
||||
System.out.println(InfiniteList.generate(() -> rng.nextInt() % 100).filter(x -> x > 10).limit(4)
|
||||
.toList());
|
||||
System.out.println(InfiniteList.<Object>generate(() -> null).limit(4).limit(1).toList());
|
||||
System.out.println(InfiniteList.<Object>generate(() -> null).limit(1).limit(4).toList());
|
||||
}
|
||||
|
||||
public static void test5() {
|
||||
List<Integer> incrHistory = new ArrayList<>();
|
||||
Immutator<Integer, Integer> incr = x -> {
|
||||
incrHistory.add(x);
|
||||
return x + 1;
|
||||
};
|
||||
|
||||
List<Integer> lessThan0History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> lessThan0 = x -> {
|
||||
lessThan0History.add(x);
|
||||
return x < 0;
|
||||
};
|
||||
|
||||
System.out.println(InfiniteList.<Integer>end().takeWhile(x -> x < 0).isEnd());
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan0).isEnd());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
|
||||
List<Integer> lessThan2History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> lessThan2 = x -> {
|
||||
lessThan2History.add(x);
|
||||
return x < 2;
|
||||
};
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan2).isEnd());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(x -> x < 5).takeWhile(x -> x < 3).toList());
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(x -> x % 2 == 0).takeWhile(x -> x < 10).toList());
|
||||
System.out.println(InfiniteList.generate(() -> 2).takeWhile(lessThan0).toString());
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan0).toString());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan0History.retainAll(List.of());
|
||||
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan0).head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan2).head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan2).tail().head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
lessThan0History.retainAll(List.of());
|
||||
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan2).takeWhile(lessThan0).head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
lessThan0History.retainAll(List.of());
|
||||
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan0).takeWhile(lessThan2).head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
|
||||
List<Integer> lessThan5History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> lessThan5 = x -> {
|
||||
lessThan5History.add(x);
|
||||
return x < 5;
|
||||
};
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
|
||||
try {
|
||||
System.out.println(InfiniteList.iterate(1, incr).takeWhile(lessThan5).takeWhile(lessThan2).tail().head());
|
||||
} catch(java.util.NoSuchElementException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
System.out.println(lessThan5History);
|
||||
|
||||
List<Integer> isEvenHistory = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> isEven = x -> {
|
||||
isEvenHistory.add(x);
|
||||
return x % 2 == 0;
|
||||
};
|
||||
|
||||
List<Integer> lessThan10History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> lessThan10 = x -> {
|
||||
lessThan10History.add(x);
|
||||
return x < 10;
|
||||
};
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan10History.retainAll(List.of());
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(isEven).takeWhile(lessThan10).head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
System.out.println(lessThan5History);
|
||||
System.out.println(lessThan10History);
|
||||
System.out.println(isEvenHistory);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
lessThan10History.retainAll(List.of());
|
||||
|
||||
System.out.println(InfiniteList.iterate(1, incr).filter(isEven).takeWhile(lessThan10).tail().head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
System.out.println(lessThan5History);
|
||||
System.out.println(lessThan10History);
|
||||
System.out.println(isEvenHistory);
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan10History.retainAll(List.of());
|
||||
InfiniteList<Integer> list = InfiniteList.iterate(1, incr).takeWhile(lessThan10);
|
||||
System.out.println(list.tail().tail().head());
|
||||
System.out.println(list.head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
System.out.println(lessThan5History);
|
||||
System.out.println(lessThan10History);
|
||||
System.out.println(isEvenHistory);
|
||||
|
||||
System.out.println(list.tail().head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
System.out.println(lessThan5History);
|
||||
System.out.println(lessThan10History);
|
||||
System.out.println(isEvenHistory);
|
||||
|
||||
System.out.println(list.tail().tail().tail().head());
|
||||
System.out.println(incrHistory);
|
||||
System.out.println(lessThan0History);
|
||||
System.out.println(lessThan2History);
|
||||
System.out.println(lessThan5History);
|
||||
System.out.println(lessThan10History);
|
||||
System.out.println(isEvenHistory);
|
||||
}
|
||||
|
||||
public static void test6() {
|
||||
InfiniteList<Integer> numbers = InfiniteList.iterate(0, x -> x + 1);
|
||||
System.out.println(InfiniteList.<Integer>end().reduce(0, (x, y) -> x + y));
|
||||
System.out.println(numbers.limit(5).reduce(0, (x, y) -> x + y));
|
||||
System.out.println(InfiniteList.iterate(0, x -> x + 1).limit(0).reduce(0, (x, y) -> x + y));
|
||||
System.out.println(InfiniteList.iterate(1, x -> x + 1).map(x -> x * x).limit(5).reduce(1, (x, y) -> x * y));
|
||||
|
||||
System.out.println(InfiniteList.end().count());
|
||||
System.out.println(numbers.limit(0).count());
|
||||
System.out.println(numbers.limit(1).count());
|
||||
System.out.println(numbers.filter(x -> x % 2 == 1).limit(10).count());
|
||||
System.out.println(numbers.limit(10).filter(x -> x % 2 == 1).count());
|
||||
System.out.println(numbers.takeWhile(x -> x < 10).count());
|
||||
System.out.println(numbers.takeWhile(x -> x < 10).filter(x -> x % 2 == 0).count());
|
||||
|
||||
System.out.println(InfiniteList.end().count() == 0L);
|
||||
System.out.println(numbers.limit(0).count() == 0L);
|
||||
System.out.println(numbers.limit(1).count() == 1L);
|
||||
System.out.println(numbers.filter(x -> x % 2 == 1).limit(10).count() == 10L);
|
||||
System.out.println(numbers.limit(10).filter(x -> x % 2 == 1).count() == 5L);
|
||||
System.out.println(numbers.takeWhile(x -> x < 10).count() == 10L);
|
||||
System.out.println(numbers.takeWhile(x -> x < 10).filter(x -> x % 2 == 0).count() == 5L);
|
||||
}
|
||||
}
|
||||
BIN
labs/cs2030s/Lab8/Lab8.pdf
Normal file
BIN
labs/cs2030s/Lab8/Lab8.pdf
Normal file
Binary file not shown.
13
labs/cs2030s/Lab8/MyTest.java
Normal file
13
labs/cs2030s/Lab8/MyTest.java
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
import cs2030s.fp.InfiniteList;
|
||||
|
||||
public class MyTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
InfiniteList<Integer> list = InfiniteList.iterate(1, x -> x + 1);
|
||||
System.out.println(list.takeWhile(x -> x < 10).reduce(0, (x, y) -> {
|
||||
System.out.println("vals: x: " + x + " y: " + y);
|
||||
return x + y;
|
||||
}));
|
||||
}
|
||||
}
|
||||
117
labs/cs2030s/Lab8/Test1.java
Normal file
117
labs/cs2030s/Lab8/Test1.java
Normal file
@@ -0,0 +1,117 @@
|
||||
import cs2030s.fp.InfiniteList;
|
||||
import cs2030s.fp.Immutator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Test1 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 1).toString()",
|
||||
() -> InfiniteList.generate(() -> 1).toString(), "[? ?]");
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 1).head()",
|
||||
() -> InfiniteList.generate(() -> 1).head(), 1);
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> null).tail().head()",
|
||||
() -> InfiniteList.generate(() -> null).tail().head(), null);
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", x -> x + \"Z\").head()",
|
||||
() -> InfiniteList.iterate("A", x -> x + "Z").head(), "A");
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", x -> x + \"Z\").tail().head()",
|
||||
() -> InfiniteList.iterate("A", x -> x + "Z").tail().head(), "AZ");
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", x -> x + \"Z\").tail().tail().head()",
|
||||
() -> InfiniteList.iterate("A", x -> x + "Z").tail().tail().head(), "AZZ");
|
||||
|
||||
List<Integer> evalHistory = new ArrayList<>();
|
||||
Immutator<Integer, Integer> op = x -> {
|
||||
evalHistory.add(x);
|
||||
return x + 1;
|
||||
};
|
||||
|
||||
InfiniteList<Integer> numbers = InfiniteList.iterate(1, op);
|
||||
numbers.head();
|
||||
i.expectReturn(
|
||||
"InfiniteList<Integer> numbers = InfiniteList.iterate(1, x -> x + 1);\n" +
|
||||
"numbers.toString()",
|
||||
() -> numbers.toString(), "[<1> ?]");
|
||||
i.expect("numbers.head() causes zero evaluation of x -> x + 1)",
|
||||
evalHistory, List.of());
|
||||
i.expectReturn("numbers.toString())",
|
||||
() -> numbers.toString(), "[<1> ?]");
|
||||
numbers.tail().head();
|
||||
i.expect("numbers.tail().head() causes one evaluation of x -> x + 1",
|
||||
evalHistory, List.of(1));
|
||||
i.expectReturn("numbers.toString())",
|
||||
() -> numbers.toString(), "[<1> [<2> ?]]");
|
||||
numbers.tail().head();
|
||||
i.expect("numbers.tail().head() (again) causes zero evaluation of x -> x + 1",
|
||||
evalHistory, List.of(1));
|
||||
i.expectReturn("numbers.toString())",
|
||||
() -> numbers.toString(), "[<1> [<2> ?]]");
|
||||
numbers.tail().tail().head();
|
||||
i.expect("numbers.tail().tail().head() causes one evaluation of x -> x + 1",
|
||||
evalHistory, List.of(1, 2));
|
||||
i.expectReturn("numbers.toString())",
|
||||
() -> numbers.toString(), "[<1> [<2> [<3> ?]]]");
|
||||
numbers.tail().head();
|
||||
i.expect("numbers.tail().head() (again) causes zero evaluation of x -> x + 1",
|
||||
evalHistory, List.of(1, 2));
|
||||
i.expectReturn("numbers.toString())",
|
||||
() -> numbers.toString(), "[<1> [<2> [<3> ?]]]");
|
||||
|
||||
InfiniteList<Integer> zeros = InfiniteList.generate(() -> {
|
||||
evalHistory.add(0);
|
||||
return 0;
|
||||
});
|
||||
evalHistory.retainAll(List.of());
|
||||
i.expect("InfiniteList<Integer> zeros = InfiniteList.generate(() -> 0)\n" +
|
||||
"zeros.toString() returns [? ?]",
|
||||
() -> zeros.toString(), "[? ?]");
|
||||
|
||||
zeros.head();
|
||||
i.expect("zeros.head() causes one evaluation of () -> 0",
|
||||
evalHistory, List.of(0));
|
||||
i.expect("zeros.toString() returns [<0> ?]",
|
||||
() -> zeros.toString(), "[<0> ?]");
|
||||
|
||||
zeros.tail().head();
|
||||
i.expect("zeros.tail().head() causes one evaluation of () -> 0)",
|
||||
evalHistory, List.of(0, 0));
|
||||
i.expect("zeros.toString() returns [<0> [[0 ?]]]",
|
||||
() -> zeros.toString(), "[<0> [<0> ?]]");
|
||||
|
||||
zeros.head();
|
||||
i.expect("zeros.head() (again) causes zero evaluation of () -> 0",
|
||||
evalHistory, List.of(0, 0));
|
||||
i.expect("zeros.toString() returns [<0> [<0> ?]]",
|
||||
() -> zeros.toString(), "[<0> [<0> ?]]");
|
||||
|
||||
zeros.tail().head();
|
||||
i.expect("zeros.tail().head() causes zero evaluation of () -> 0",
|
||||
evalHistory, List.of(0, 0));
|
||||
i.expect("zeros.toString() returns [<0> [<0> ?]]",
|
||||
() -> zeros.toString(), "[<0> [<0> ?]]");
|
||||
|
||||
zeros.tail().tail().head();
|
||||
i.expect("zeros.tail().tail().head() causes one more evaluation of () -> 0",
|
||||
evalHistory, List.of(0, 0, 0));
|
||||
i.expect("zeros.toString() returns [<0> [<0> [<0> ?]]]",
|
||||
() -> zeros.toString(), "[<0> [<0> [<0> ?]]]");
|
||||
|
||||
zeros.tail().head();
|
||||
i.expect("zeros.tail().head() (again) causes zero evaluation of () -> 0",
|
||||
evalHistory, List.of(0, 0, 0));
|
||||
i.expect("zeros.toString() returns [<0> [<0> [<0> ?]]]",
|
||||
() -> zeros.toString(), "[<0> [<0> [<0> ?]]]");
|
||||
}
|
||||
}
|
||||
87
labs/cs2030s/Lab8/Test2.java
Normal file
87
labs/cs2030s/Lab8/Test2.java
Normal file
@@ -0,0 +1,87 @@
|
||||
import cs2030s.fp.InfiniteList;
|
||||
import cs2030s.fp.Constant;
|
||||
import cs2030s.fp.Immutator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Test2 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
CS2030STest i = new CS2030STest();
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 1).map(x -> x * 2).toString()",
|
||||
() -> InfiniteList.generate(() -> 1).map(x -> x * 2).toString(),
|
||||
"[? ?]");
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).toString()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).toString(),
|
||||
"[? ?]");
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 1).map(x -> x * 2).head()",
|
||||
() -> InfiniteList.generate(() -> 1).map(x -> x * 2).head(), 2);
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 1).map(x -> x * 2).tail().head()",
|
||||
() -> InfiniteList.generate(() -> 1).map(x -> x * 2).tail().head(), 2);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).head()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).head(), 2);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).tail().head()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).tail().head(), 4);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).map(x -> x - 1).head()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).map(x -> x - 1).head(), 1);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).map(x -> x - 1).tail().head()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).map(x -> x - 1).tail().head(), 3);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).map(x -> x % 2 == 0 ? null : x).tail().head()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> x % 2 == 0 ? null : x).tail().head(),
|
||||
null);
|
||||
|
||||
List<Integer> doublerHistory = new ArrayList<>();
|
||||
List<Integer> generateHistory = new ArrayList<>();
|
||||
Constant<Integer> generator = () -> {
|
||||
generateHistory.add(1);
|
||||
return 1;
|
||||
};
|
||||
Immutator<Integer, Integer> doubler = x -> {
|
||||
doublerHistory.add(x);
|
||||
return x * 2;
|
||||
};
|
||||
|
||||
i.expect("InfiniteList.generate(() -> 1).map(x -> x * 2).tail().head()\n" +
|
||||
" ..returns 2",
|
||||
() -> InfiniteList.generate(generator).map(doubler).tail().head(), 2);
|
||||
i.expect(" ..causes two evals of () -> 1",
|
||||
generateHistory, List.of(1, 1));
|
||||
i.expect(" ..causes two evals of x -> x * 2",
|
||||
doublerHistory, List.of(1, 1));
|
||||
|
||||
generateHistory.retainAll(List.of());
|
||||
doublerHistory.retainAll(List.of());
|
||||
InfiniteList<Integer> ones = InfiniteList.generate(generator);
|
||||
InfiniteList<Integer> twos = ones.map(doubler);
|
||||
twos.tail().head();
|
||||
i.expect("InfiniteList<Integer> ones = InfiniteList.generate(() -> 1)\n" +
|
||||
"InfiniteList<Integer> twos = ones.map(x -> x * 2)\n" +
|
||||
"After twos.tail().head()\n" +
|
||||
" ..ones.toString() returns [<1> [<1> ?]]",
|
||||
() -> ones.toString(), "[<1> [<1> ?]]");
|
||||
i.expect(" ..twos.toString() returns [<2> [<2> ?]]",
|
||||
() -> twos.toString(), "[<2> [<2> ?]]");
|
||||
twos.head();
|
||||
i.expect(" ..calling twos.head() again\n" +
|
||||
" ....causes zero evaluation of () -> 1",
|
||||
generateHistory, List.of(1, 1));
|
||||
i.expect(" ....causes zero evaluation of x -> x * 2",
|
||||
doublerHistory, List.of(1, 1));
|
||||
twos.tail().head();
|
||||
i.expect(" ..calling twos.tail().head() again\n" +
|
||||
" ....causes zero evaluation of () -> 1",
|
||||
generateHistory, List.of(1, 1));
|
||||
i.expect(" ....causes zero evaluation of x -> x * 2",
|
||||
doublerHistory, List.of(1, 1));
|
||||
}
|
||||
}
|
||||
129
labs/cs2030s/Lab8/Test3.java
Normal file
129
labs/cs2030s/Lab8/Test3.java
Normal file
@@ -0,0 +1,129 @@
|
||||
import cs2030s.fp.InfiniteList;
|
||||
import cs2030s.fp.Immutator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Test3 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
i.expectReturn("InfiniteList.generate(() -> 1).filter(x -> x % 2 == 0).toString()",
|
||||
() -> InfiniteList.generate(() -> 1).filter(x -> x % 2 == 0).toString(),
|
||||
"[? ?]");
|
||||
i.expectReturn("InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).toString()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).toString(),
|
||||
"[? ?]");
|
||||
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).head() returns 2",
|
||||
InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).head(), 2);
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0)" +
|
||||
".filter(x -> x > 4).head() returns 6",
|
||||
InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).filter(x -> x > 4).head(), 6);
|
||||
|
||||
List<Integer> incrHistory = new ArrayList<>();
|
||||
List<Integer> isEvenHistory = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> isEven = x -> {
|
||||
isEvenHistory.add(x);
|
||||
return x % 2 == 0;
|
||||
};
|
||||
Immutator<Integer, Integer> incr = x -> {
|
||||
incrHistory.add(x);
|
||||
return x + 1;
|
||||
};
|
||||
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).tail() " +
|
||||
".head()\n" + " ..returns 4",
|
||||
InfiniteList.iterate(1, incr).filter(isEven).tail().head(), 4);
|
||||
i.expect(" ..causes three evals on x -> x + 1",
|
||||
incrHistory, List.of(1, 2, 3));
|
||||
i.expect(" ..causes four evals on x -> x % 2 == 0",
|
||||
isEvenHistory, List.of(1, 2, 3, 4));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
|
||||
InfiniteList<Integer> nums = InfiniteList.iterate(1, incr);
|
||||
InfiniteList<Integer> evens = nums.filter(isEven);
|
||||
evens.tail().head();
|
||||
i.expect("InfiniteList<Integer> nums = InfiniteList.iterate(1, x -> x + 1)\n" +
|
||||
"InfiniteList<Integer> evens = nums.filter(x -> x % 2 == 0)\n" +
|
||||
"After evens.tail().head()\n" +
|
||||
" ..nums.toString() returns [<1> [<2> [<3> [<4> ?]]]]",
|
||||
nums.toString(), "[<1> [<2> [<3> [<4> ?]]]]");
|
||||
i.expect(" ..evens.toString() returns [<> [<2> [<> [<4> ?]]]]",
|
||||
evens.toString(), "[<> [<2> [<> [<4> ?]]]]");
|
||||
nums.tail().head();
|
||||
i.expect(" ..calling nums.tail().head()\n" +
|
||||
" ....causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of(1, 2, 3));
|
||||
evens.tail().head();
|
||||
i.expect(" ..calling evens.tail().head()\n" +
|
||||
" ....causes zero evaluation of x -> x % 2 == 0",
|
||||
isEvenHistory, List.of(1, 2, 3, 4));
|
||||
i.expect(" ....causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of(1, 2, 3));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
|
||||
List<Integer> moreThan5History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> moreThan5 = x -> {
|
||||
moreThan5History.add(x);
|
||||
return x > 5;
|
||||
};
|
||||
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).filter(x -> x > 5).filter(x -> x % 2 == 0)" +
|
||||
".head()\n" +
|
||||
" ..returns 6",
|
||||
InfiniteList.iterate(1, incr).filter(moreThan5).filter(isEven).head(), 6);
|
||||
i.expect(" ..causes five evals on x -> x + 1",
|
||||
incrHistory, List.of(1, 2, 3, 4, 5));
|
||||
i.expect(" ..causes six evals on x -> x > 5",
|
||||
moreThan5History, List.of(1, 2, 3, 4, 5, 6));
|
||||
i.expect(" ..causes one evals on x -> x % 2 == 0",
|
||||
isEvenHistory, List.of(6));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
moreThan5History.retainAll(List.of());
|
||||
|
||||
List<Integer> doublerHistory = new ArrayList<>();
|
||||
Immutator<Integer, Integer> doubler = x -> {
|
||||
doublerHistory.add(x);
|
||||
return x * 2;
|
||||
};
|
||||
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).filter(x -> x > 5)" +
|
||||
".filter(x -> x % 2 == 0).tail().head()\n" +
|
||||
" ..returns 6",
|
||||
InfiniteList.iterate(1, incr).map(doubler).filter(moreThan5).filter(isEven)
|
||||
.tail().head(), 8);
|
||||
i.expect(" ..causes three evals on x -> x + 1",
|
||||
incrHistory, List.of(1, 2, 3));
|
||||
i.expect(" ..causes four evals on x -> x * 2",
|
||||
doublerHistory, List.of(1, 2, 3, 4));
|
||||
i.expect(" ..causes four evals on x -> x > 5",
|
||||
moreThan5History, List.of(2, 4, 6, 8));
|
||||
i.expect(" ..causes two evals on x -> x % 2 == 0",
|
||||
isEvenHistory, List.of(6, 8));
|
||||
|
||||
doublerHistory.retainAll(List.of());
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
moreThan5History.retainAll(List.of());
|
||||
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).map(x -> x * 2)" +
|
||||
".filter(x -> x > 5).head()\n" +
|
||||
" ..returns 8",
|
||||
InfiniteList.iterate(1, incr).filter(isEven).map(doubler).filter(moreThan5).head(), 8);
|
||||
i.expect(" ..causes three evals on x -> x + 1",
|
||||
incrHistory, List.of(1, 2, 3));
|
||||
i.expect(" ..causes four evals on x -> x % 2 == 0",
|
||||
isEvenHistory, List.of(1, 2, 3, 4));
|
||||
i.expect(" ..causes two evals on x -> x * 2",
|
||||
doublerHistory, List.of(2, 4));
|
||||
i.expect(" ..causes two evals on x -> x > 5",
|
||||
moreThan5History, List.of(4, 8));
|
||||
}
|
||||
}
|
||||
195
labs/cs2030s/Lab8/Test4.java
Normal file
195
labs/cs2030s/Lab8/Test4.java
Normal file
@@ -0,0 +1,195 @@
|
||||
import cs2030s.fp.InfiniteList;
|
||||
import cs2030s.fp.Immutator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
class Test4 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest i = new CS2030STest();
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).isEnd()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).isEnd(), false);
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 2).isEnd()",
|
||||
() -> InfiniteList.generate(() -> 2).isEnd(), false);
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 2).filter(x -> x % 3 == 0).isEnd()",
|
||||
() -> InfiniteList.generate(() -> 2).filter(x -> x % 3 == 0).isEnd(), false);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).map(x -> 2).isEnd()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> 2).isEnd(), false);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).isEnd()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> 2).isEnd(), false);
|
||||
|
||||
i.expectReturn("InfiniteList.end().isEnd()",
|
||||
() -> InfiniteList.end().isEnd(), true);
|
||||
i.expectReturn("InfiniteList.end().map(x -> 2).isEnd()",
|
||||
() -> InfiniteList.end().map(x -> 2).isEnd(), true);
|
||||
i.expectReturn("InfiniteList.end().filter(x -> true).isEnd()",
|
||||
() -> InfiniteList.end().filter(x -> true).isEnd(), true);
|
||||
i.expectReturn("InfiniteList.end().filter(x -> false).isEnd()",
|
||||
() -> InfiniteList.end().filter(x -> false).isEnd(), true);
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.end().limit(4).isEnd()",
|
||||
() -> InfiniteList.end().limit(4).isEnd(), true);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(0).isEnd()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).limit(0).isEnd(), true);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(0).isEnd()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).limit(0).isEnd(), true);
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(1).isEnd()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).limit(1).isEnd(), false);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(10).isEnd()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).limit(10).isEnd(), false);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(-1).isEnd()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).limit(-1).isEnd(), true);
|
||||
|
||||
List<Integer> incrHistory = new ArrayList<>();
|
||||
Immutator<Integer, Integer> incr = x -> {
|
||||
incrHistory.add(x);
|
||||
return x + 1;
|
||||
};
|
||||
InfiniteList.iterate(1, incr).limit(0).isEnd();
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).limit(0).isEnd() " +
|
||||
"causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
InfiniteList.iterate(1, incr).limit(1).isEnd();
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).limit(1).isEnd() " +
|
||||
"causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
InfiniteList.iterate(1, incr).limit(10).isEnd();
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).limit(10).isEnd() " +
|
||||
"causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
|
||||
i.expectReturn("InfiniteList.generate(() -> 1).limit(4).toString()",
|
||||
() -> InfiniteList.generate(() -> 1).limit(4).toString(),
|
||||
"[? ?]");
|
||||
i.expectReturn("InfiniteList.iterate(1, x -> x + 1).limit(4).toString()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(4).toString(),
|
||||
"[<1> ?]");
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).limit(0).head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(0).head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expectReturn("InfiniteList.iterate(1, x -> x + 1).limit(1).head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(1).head(), 1);
|
||||
i.expectReturn("InfiniteList.iterate(1, x -> x + 1).limit(4).head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(4).head(), 1);
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).limit(1).tail().head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(1).tail().head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expectReturn("InfiniteList.iterate(1, x -> x + 1).limit(4).tail().tail().head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(4).tail().tail().head(), 3);
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).limit(4).limit(1).tail().head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(4).limit(1).tail().head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).limit(1).limit(4).tail().head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(1).limit(4).tail().head(),
|
||||
new java.util.NoSuchElementException());
|
||||
|
||||
Immutator<Boolean, Integer> isEven = x -> (x % 2 == 0);
|
||||
|
||||
i.expectException(
|
||||
"InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).limit(0).head()",
|
||||
() -> InfiniteList.iterate(1, incr).filter(isEven).limit(0).head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).limit(1).head()",
|
||||
() -> InfiniteList.iterate(1, incr).filter(isEven).limit(1).head(), 2);
|
||||
i.expectException(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(1).filter(x -> x % 2 == 0).head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(1).filter(isEven).head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(2).filter(x -> x % 2 == 0).head()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(2).filter(isEven).head(), 2);
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", s -> s + \"Z\").limit(2).map(s -> s.length()).head()",
|
||||
() -> InfiniteList.iterate("A", s -> s + "Z").limit(2).map(s -> s.length()).head(), 1);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", s -> s + \"Z\").limit(2).map(s -> s.length()).tail().head()",
|
||||
() -> InfiniteList.iterate("A", s -> s + "Z").limit(2).map(s -> s.length())
|
||||
.tail().head(), 2);
|
||||
i.expectException(
|
||||
"InfiniteList.iterate(\"A\", s -> s + \"Z\").limit(2).map(s -> s.length()).tail().tail()" +
|
||||
".head()",
|
||||
() -> InfiniteList.iterate("A", s -> s + "Z").limit(2).map(s -> s.length()).tail().tail()
|
||||
.head(),
|
||||
new java.util.NoSuchElementException());
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", s -> s + \"Z\").map(s -> s.length()).limit(2).head()",
|
||||
() -> InfiniteList.iterate("A", s -> s + "Z").map(s -> s.length()).head(), 1);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", s -> s + \"Z\").map(s -> s.length()).limit(2).tail().head()",
|
||||
() -> InfiniteList.iterate("A", s -> s + "Z").map(s -> s.length()).tail().head(), 2);
|
||||
i.expectException(
|
||||
"InfiniteList.iterate(\"A\", s -> s + \"Z\").map(s -> s.length()).limit(2).tail().tail()" +
|
||||
".head()",
|
||||
() -> InfiniteList.iterate("A", s -> s + "Z").map(s -> s.length()).limit(2).tail().tail()
|
||||
.head(),
|
||||
new java.util.NoSuchElementException());
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.<String>end().toList()",
|
||||
() -> InfiniteList.<String>end().toList(), List.of());
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", s -> s + \"Z\").map(s -> s.length()).limit(2).toList()",
|
||||
() -> InfiniteList.iterate("A", s -> s + "Z").map(s -> s.length()).limit(2).toList(),
|
||||
List.of(1, 2));
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(\"A\", s -> s + \"Z\").limit(2).map(s -> s.length()).toList()",
|
||||
() -> InfiniteList.iterate("A", s -> s + "Z").limit(2).map(s -> s.length()).toList(),
|
||||
List.of(1, 2));
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(2).filter(x -> x % 2 == 0).toList()",
|
||||
() -> InfiniteList.iterate(1, incr).limit(2).filter(isEven).toList(), List.of(2));
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).limit(2).toList()",
|
||||
() -> InfiniteList.iterate(1, incr).filter(isEven).limit(2).toList(), List.of(2, 4));
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(10).limit(3).toList()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).limit(10).limit(3).toList(), List.of(1, 2, 3));
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).limit(3).limit(10).toList()",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).limit(3).limit(10).toList(), List.of(1, 2, 3));
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 4).limit(0).toList()",
|
||||
() -> InfiniteList.generate(() -> 4).limit(0).toList(), List.of());
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> 4).limit(2).toList()",
|
||||
() -> InfiniteList.generate(() -> 4).limit(2).toList(), List.of(4, 4));
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).filter(x -> x > 10).map(x -> x.hashCode() % 30)" +
|
||||
".filter(x -> x < 20).limit(5).toList()",
|
||||
() -> InfiniteList.iterate(0, x -> x + 1).filter(x -> x > 10).map(x -> x.hashCode() % 30)
|
||||
.filter(x -> x < 20).limit(5).toList(),
|
||||
List.of(11, 12, 13, 14, 15));
|
||||
|
||||
java.util.Random rng = new java.util.Random(1);
|
||||
i.expectReturn(
|
||||
"InfiniteList.generate(() -> rng.nextInt() % 100).filter(x -> x > 10).limit(4).toList()",
|
||||
() -> InfiniteList.generate(() -> rng.nextInt() % 100).filter(x -> x > 10).limit(4)
|
||||
.toList(),
|
||||
List.of(76, 95, 26, 69));
|
||||
|
||||
i.expectReturn("InfiniteList.generate(() -> null).limit(4).limit(1).toList()",
|
||||
() -> InfiniteList.<Object>generate(() -> null).limit(4).limit(1).toList(),
|
||||
Arrays.asList(new Object[] { null }));
|
||||
i.expectReturn("InfiniteList.generate(() -> null).limit(1).limit(4).toList()",
|
||||
() -> InfiniteList.<Object>generate(() -> null).limit(1).limit(4).toList(),
|
||||
Arrays.asList(new Object[] { null }));
|
||||
}
|
||||
}
|
||||
203
labs/cs2030s/Lab8/Test5.java
Normal file
203
labs/cs2030s/Lab8/Test5.java
Normal file
@@ -0,0 +1,203 @@
|
||||
import cs2030s.fp.InfiniteList;
|
||||
import cs2030s.fp.Immutator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Test5 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
List<Integer> incrHistory = new ArrayList<>();
|
||||
Immutator<Integer, Integer> incr = x -> {
|
||||
incrHistory.add(x);
|
||||
return x + 1;
|
||||
};
|
||||
|
||||
List<Integer> lessThan0History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> lessThan0 = x -> {
|
||||
lessThan0History.add(x);
|
||||
return x < 0;
|
||||
};
|
||||
|
||||
i.expect("InfiniteList.<Integer>end().takeWhile(x -> x < 0).isEnd()\n" +
|
||||
" ..returns true",
|
||||
() -> InfiniteList.<Integer>end().takeWhile(x -> x < 0).isEnd(), true);
|
||||
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 0).isEnd()\n" +
|
||||
" ..returns false",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan0).isEnd(), false);
|
||||
i.expect(" ..causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
i.expect(" ..causes zero evaluation of x -> x < 0",
|
||||
lessThan0History, List.of());
|
||||
|
||||
List<Integer> lessThan2History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> lessThan2 = x -> {
|
||||
lessThan2History.add(x);
|
||||
return x < 2;
|
||||
};
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 2).isEnd()\n" +
|
||||
" ..returns false",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan2).isEnd(), false);
|
||||
i.expect(" ..causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
i.expect(" ..causes zero evaluation of x -> x < 2",
|
||||
lessThan2History, List.of());
|
||||
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 5).takeWhile(x -> x < 3)" +
|
||||
".toList()\n" + " ..returns [1, 2]",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(x -> x < 5).takeWhile(x -> x < 3).toList(),
|
||||
List.of(1, 2));
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).takeWhile(x -> x < 10)" +
|
||||
".toList()\n " + " ..returns [2, 4, 6, 8]",
|
||||
() -> InfiniteList.iterate(1, incr).filter(x -> x % 2 == 0).takeWhile(x -> x < 10).toList(),
|
||||
List.of(2, 4, 6, 8));
|
||||
|
||||
i.expectReturn("() -> InfiniteList.generate(() -> 2).takeWhile(lessThan0).toString()",
|
||||
() -> InfiniteList.generate(() -> 2).takeWhile(lessThan0).toString(),
|
||||
"[? ?]");
|
||||
i.expectReturn("() -> InfiniteList.iterate(1, incr).takeWhile(lessThan0).toString()",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan0).toString(),
|
||||
"[? ?]");
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan0History.retainAll(List.of());
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 0).head()\n" +
|
||||
" ..throws exception",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan0).head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expect(" ..causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
i.expect(" ..causes one evaluation of x -> x < 0",
|
||||
lessThan0History, List.of(1));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 2).head()\n" +
|
||||
" ..returns 1",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan2).head(), 1);
|
||||
i.expect("..causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
i.expect("..causes one evaluation of x -> x < 2",
|
||||
lessThan2History, List.of(1));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 2).tail().head()\n" +
|
||||
" ..throws exception",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan2).tail().head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expect(" ..causes one evaluation of x -> x + 1",
|
||||
incrHistory, List.of(1));
|
||||
i.expect(" ..causes two evaluation of x -> x < 2",
|
||||
lessThan2History, List.of(1, 2));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
lessThan0History.retainAll(List.of());
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 2)" +
|
||||
".takeWhile(x -> x < 0).head()\n" +
|
||||
" ..throws exception",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan2).takeWhile(lessThan0).head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expect(" ..causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
i.expect(" ..causes one evaluation of x -> x < 2",
|
||||
lessThan2History, List.of(1));
|
||||
i.expect(" ..causes one evaluation of x -> x < 0",
|
||||
lessThan0History, List.of(1));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
lessThan0History.retainAll(List.of());
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 0)" +
|
||||
".takeWhile(x -> x < 2).head()\n" +
|
||||
" ..throws exception",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan0).takeWhile(lessThan2).head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expect(" ..causes zero evaluation of x -> x + 1",
|
||||
incrHistory, List.of());
|
||||
i.expect(" ..causes one evaluation of x -> x < 0",
|
||||
lessThan0History, List.of(1));
|
||||
i.expect(" ..causes zero evaluation of x -> x < 2",
|
||||
lessThan2History, List.of());
|
||||
|
||||
List<Integer> lessThan5History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> lessThan5 = x -> {
|
||||
lessThan5History.add(x);
|
||||
return x < 5;
|
||||
};
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan2History.retainAll(List.of());
|
||||
i.expectException("InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 5)" +
|
||||
".takeWhile(x -> x < 2).tail().head()\n" +
|
||||
" ..throws exception",
|
||||
() -> InfiniteList.iterate(1, incr).takeWhile(lessThan5).takeWhile(lessThan2).tail().head(),
|
||||
new java.util.NoSuchElementException());
|
||||
i.expect(" ..causes one evaluation of x -> x + 1",
|
||||
incrHistory, List.of(1));
|
||||
i.expect(" ..causes two evaluations of x -> x < 5",
|
||||
lessThan5History, List.of(1, 2));
|
||||
i.expect(" ..causes two evaluations of x -> x < 2",
|
||||
lessThan2History, List.of(1, 2));
|
||||
|
||||
List<Integer> isEvenHistory = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> isEven = x -> {
|
||||
isEvenHistory.add(x);
|
||||
return x % 2 == 0;
|
||||
};
|
||||
|
||||
List<Integer> lessThan10History = new ArrayList<>();
|
||||
Immutator<Boolean, Integer> lessThan10 = x -> {
|
||||
lessThan10History.add(x);
|
||||
return x < 10;
|
||||
};
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan10History.retainAll(List.of());
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0)" +
|
||||
".takeWhile(x -> x < 10).head()\n" +
|
||||
" ..returns 2",
|
||||
() -> InfiniteList.iterate(1, incr).filter(isEven).takeWhile(lessThan10).head(), 2);
|
||||
i.expect(" ..causes one evaluation of x -> x + 1",
|
||||
incrHistory, List.of(1));
|
||||
i.expect(" ..causes two evaluations of x -> x % 2 == 0",
|
||||
isEvenHistory, List.of(1, 2));
|
||||
i.expect(" ..causes one evaluations of x -> x < 10",
|
||||
lessThan10History, List.of(2));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
isEvenHistory.retainAll(List.of());
|
||||
lessThan10History.retainAll(List.of());
|
||||
i.expect("InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0)" +
|
||||
".takeWhile(x -> x < 10).tail().head()\n" +
|
||||
" ..returns 4",
|
||||
() -> InfiniteList.iterate(1, incr).filter(isEven).takeWhile(lessThan10)
|
||||
.tail().head(), 4);
|
||||
i.expect(" ..causes three evaluations of x -> x + 1",
|
||||
incrHistory, List.of(1, 2, 3));
|
||||
i.expect(" ..causes four evaluations of x -> x % 2 == 0",
|
||||
isEvenHistory, List.of(1, 2, 3, 4));
|
||||
i.expect(" ..causes two evaluations of x -> x < 10",
|
||||
lessThan10History, List.of(2, 4));
|
||||
|
||||
incrHistory.retainAll(List.of());
|
||||
lessThan10History.retainAll(List.of());
|
||||
InfiniteList<Integer> list = InfiniteList.iterate(1, incr).takeWhile(lessThan10);
|
||||
i.expect("InifiniteList<Integer> list = InfiniteList.iterate(1, x -> x + 1)" +
|
||||
".takeWhile(x -> x < 10)\n" +
|
||||
" ..list.tail().tail().head() returns 3",
|
||||
list.tail().tail().head(), 3);
|
||||
list.head();
|
||||
i.expect(" ..list.head() causes zero evaluation of x -> x < 10",
|
||||
lessThan10History, List.of(1, 2, 3));
|
||||
list.tail().head();
|
||||
i.expect(" ..list.tail().head() causes zero evaluation of x -> x < 10",
|
||||
lessThan10History, List.of(1, 2, 3));
|
||||
list.tail().tail().tail().head();
|
||||
i.expect(" ..list.tail().tail().tail().head() causes one evaluation of x -> x < 10",
|
||||
lessThan10History, List.of(1, 2, 3, 4));
|
||||
}
|
||||
}
|
||||
46
labs/cs2030s/Lab8/Test6.java
Normal file
46
labs/cs2030s/Lab8/Test6.java
Normal file
@@ -0,0 +1,46 @@
|
||||
import cs2030s.fp.InfiniteList;
|
||||
|
||||
class Test6 {
|
||||
public static void main(String[] args) {
|
||||
CS2030STest i = new CS2030STest();
|
||||
|
||||
InfiniteList<Integer> numbers = InfiniteList.iterate(0, x -> x + 1);
|
||||
i.expectReturn(
|
||||
"InfiniteList.end().reduce(0, (x, y) -> x + y)",
|
||||
() -> InfiniteList.<Integer>end().reduce(0, (x, y) -> x + y), 0);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).limit(5).reduce(0, (x, y) -> x + y)",
|
||||
() -> numbers.limit(5).reduce(0, (x, y) -> x + y), 10);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).limit(0).reduce(0, (x, y) -> x + y)",
|
||||
() -> InfiniteList.iterate(0, x -> x + 1).limit(0).reduce(0, (x, y) -> x + y), 0);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(1, x -> x + 1).map(x -> x * x).limit(5).reduce(1, (x, y) -> x * y)",
|
||||
() -> InfiniteList.iterate(1, x -> x + 1).map(x -> x * x).limit(5)
|
||||
.reduce(1, (x, y) -> x * y),
|
||||
14400);
|
||||
|
||||
i.expectReturn(
|
||||
"InfiniteList.end().count()",
|
||||
() -> InfiniteList.end().count(), 0L);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).limit(0).count()",
|
||||
() -> numbers.limit(0).count(), 0L);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).limit(1).count()",
|
||||
() -> numbers.limit(1).count(), 1L);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).filter(x -> x % 2 == 1).limit(10).count()",
|
||||
() -> numbers.filter(x -> x % 2 == 1).limit(10).count(), 10L);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).limit(10).filter(x -> x % 2 == 1).count()",
|
||||
() -> numbers.limit(10).filter(x -> x % 2 == 1).count(), 5L);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).takeWhile(x -> x < 10).count()",
|
||||
() -> numbers.takeWhile(x -> x < 10).count(), 10L);
|
||||
i.expectReturn(
|
||||
"InfiniteList.iterate(0, x -> x + 1).takeWhile(x -> x < 10).filter(x -> x % 2 == 0)" +
|
||||
".count()",
|
||||
() -> numbers.takeWhile(x -> x < 10).filter(x -> x % 2 == 0).count(), 5L);
|
||||
}
|
||||
}
|
||||
18
labs/cs2030s/Lab8/cs2030s/fp/Action.java
Normal file
18
labs/cs2030s/Lab8/cs2030s/fp/Action.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
/**
|
||||
* Represent a function that acts on a value.
|
||||
* CS2030S Lab 5
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @param <T> The type of the input value.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
interface Action<T> {
|
||||
/**
|
||||
* The functional method to act on the value t.
|
||||
*
|
||||
* @param t The input value.
|
||||
*/
|
||||
void call(T t);
|
||||
}
|
||||
17
labs/cs2030s/Lab8/cs2030s/fp/Actionable.java
Normal file
17
labs/cs2030s/Lab8/cs2030s/fp/Actionable.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
/**
|
||||
* Represent a container that can act on its content.
|
||||
* CS2030S Lab 5
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @param <T> The type of the content.
|
||||
*/
|
||||
interface Actionable<T> {
|
||||
/**
|
||||
* The method to act on its content.
|
||||
*
|
||||
* @param f The action.
|
||||
*/
|
||||
void act(Action<? super T> f);
|
||||
}
|
||||
96
labs/cs2030s/Lab8/cs2030s/fp/Actually.java
Normal file
96
labs/cs2030s/Lab8/cs2030s/fp/Actually.java
Normal file
@@ -0,0 +1,96 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
/**
|
||||
* A container class the encapsulate
|
||||
* a value that may or may not be an error.
|
||||
*
|
||||
* @author Adi Yoga S. Prabawa
|
||||
* @version CS2030S AY 22/23 Sem 1
|
||||
*/
|
||||
public class Actually<T> implements Immutatorable<T> {
|
||||
private T val;
|
||||
private Exception err;
|
||||
|
||||
private Actually(T val, Exception err) {
|
||||
this.val = val;
|
||||
this.err = err;
|
||||
}
|
||||
|
||||
public static <T> Actually<T> err() {
|
||||
// A common error for ease of use
|
||||
return new Actually<T>((T) null, new Exception("err"));
|
||||
}
|
||||
|
||||
public static <T> Actually<T> err(Exception err) {
|
||||
return new Actually<T>((T) null, err);
|
||||
}
|
||||
|
||||
public static <T> Actually<T> ok(T val) {
|
||||
return new Actually<T>(val, null);
|
||||
}
|
||||
|
||||
public T except(Constant<? extends T> com) {
|
||||
return this.err == null ? this.val : com.init();
|
||||
}
|
||||
|
||||
public <U extends T> T unless(U val) {
|
||||
return this.err == null ? this.val : val;
|
||||
}
|
||||
|
||||
public void finish(Action<? super T> act) {
|
||||
if (this.err == null) {
|
||||
act.call(this.val);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> Actually<R> transform(Immutator<? extends R, ? super T> f) {
|
||||
return this.err == null ? Actually.<R>ok(f.invoke(this.val)) : Actually.<R>err(this.err);
|
||||
}
|
||||
|
||||
public <R> Actually<? extends R> next(Immutator<? extends Actually<? extends R>, ? super T> f) {
|
||||
if (this.err != null) {
|
||||
return Actually.err(this.err);
|
||||
} else {
|
||||
return f.invoke(this.val);
|
||||
}
|
||||
}
|
||||
|
||||
public Actually<T> check(Immutator<Boolean, ? super T> pred) {
|
||||
if (this.err != null) {
|
||||
return Actually.err(this.err);
|
||||
}
|
||||
Boolean chk = pred.invoke(this.val);
|
||||
if (chk) {
|
||||
return this;
|
||||
}
|
||||
return Actually.err();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (this.err != null) {
|
||||
return "<>";
|
||||
}
|
||||
return "<" + this.val + ">";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Actually<?>)) {
|
||||
return false;
|
||||
}
|
||||
Actually<?> that = (Actually<?>) obj;
|
||||
if (this.err != null) {
|
||||
// for simplicity, all err is the same
|
||||
return that.err != null;
|
||||
}
|
||||
if (this.val == null) {
|
||||
return that.val == null;
|
||||
}
|
||||
return this.val.equals(that.val);
|
||||
}
|
||||
}
|
||||
23
labs/cs2030s/Lab8/cs2030s/fp/Combiner.java
Normal file
23
labs/cs2030s/Lab8/cs2030s/fp/Combiner.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
/**
|
||||
* Represent a function that combines two values into one. The two inputs
|
||||
* and the result can be of different types.
|
||||
* CS2030S Lab 5
|
||||
* AY20/21 Semester 2
|
||||
*
|
||||
* @param <R> The type of the return value
|
||||
* @param <S> The type of the first input value
|
||||
* @param <T> The type of the second input value
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Combiner<R, S, T> {
|
||||
/**
|
||||
* The functional method to combines two values into one.
|
||||
*
|
||||
* @param s The first input value
|
||||
* @param t The second input value
|
||||
* @return The value after combining s and t.
|
||||
*/
|
||||
R combine(S s, T t);
|
||||
}
|
||||
18
labs/cs2030s/Lab8/cs2030s/fp/Constant.java
Normal file
18
labs/cs2030s/Lab8/cs2030s/fp/Constant.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
/**
|
||||
* Represent a function to initialise a constant value.
|
||||
* CS2030S Lab 5
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @param <T> The type of the constant value.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Constant<T> {
|
||||
/**
|
||||
* The functional method to initialise the constant.
|
||||
*
|
||||
* @return The constant value.
|
||||
*/
|
||||
T init();
|
||||
}
|
||||
20
labs/cs2030s/Lab8/cs2030s/fp/Immutator.java
Normal file
20
labs/cs2030s/Lab8/cs2030s/fp/Immutator.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
/**
|
||||
* Represent a function that immutate one value into another, possible of different types.
|
||||
* CS2030S Lab 5
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @param <R> The type of the result value.
|
||||
* @param <P> The type of the input value.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Immutator<R, P> {
|
||||
/**
|
||||
* The functional method to immutate the value p.
|
||||
*
|
||||
* @param p The input value.
|
||||
* @return The value after applying the given immutation on p.
|
||||
*/
|
||||
R invoke(P p);
|
||||
}
|
||||
20
labs/cs2030s/Lab8/cs2030s/fp/Immutatorable.java
Normal file
20
labs/cs2030s/Lab8/cs2030s/fp/Immutatorable.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
/**
|
||||
* Represent a container that can transforms its content to produce another
|
||||
* container containing the immutated element, possible of different types.
|
||||
* CS2030S Lab 5
|
||||
* AY22/23 Semester 1
|
||||
*
|
||||
* @param <T> The type of the content.
|
||||
*/
|
||||
public interface Immutatorable<T> {
|
||||
/**
|
||||
* The method to produce another container with immutated element.
|
||||
*
|
||||
* @param <R> The return value.
|
||||
* @param f The immutator.
|
||||
* @return A new container containing the immutated element.
|
||||
*/
|
||||
<R> Immutatorable<R> transform(Immutator<? extends R, ? super T> f);
|
||||
}
|
||||
266
labs/cs2030s/Lab8/cs2030s/fp/InfiniteList.java
Normal file
266
labs/cs2030s/Lab8/cs2030s/fp/InfiniteList.java
Normal file
@@ -0,0 +1,266 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* A list of infinite items.
|
||||
*
|
||||
* @author Yadunand Prem
|
||||
* @version CS2030S AY 22/23 Sem 1
|
||||
*/
|
||||
|
||||
public class InfiniteList<T> {
|
||||
private Memo<Actually<T>> head;
|
||||
private Memo<InfiniteList<T>> tail;
|
||||
|
||||
public static final End END = new End();
|
||||
|
||||
/**
|
||||
* end() returns the END element.
|
||||
*
|
||||
* @param <T> The type of each element in the list. Does not matter as it's the
|
||||
* end element
|
||||
* @return the End Element
|
||||
*/
|
||||
public static <T> InfiniteList<T> end() {
|
||||
@SuppressWarnings("unchecked")
|
||||
InfiniteList<T> result = (InfiniteList<T>) END;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* A private constructor for InfiniteList. Use generate / iterate to generate
|
||||
*
|
||||
* @param head The head element of the list, of type T
|
||||
* @param tail The tail of the list, which is an infinite list
|
||||
*/
|
||||
private InfiniteList(Memo<Actually<T>> head, Memo<InfiniteList<T>> tail) {
|
||||
this.head = head;
|
||||
this.tail = tail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a list by calling prod.init()
|
||||
*
|
||||
* @param <T> The type of each element in the list
|
||||
* @param prod The producer to generate a list of items for the infinite list
|
||||
* @return The infiniteList with a generator for the tail
|
||||
*/
|
||||
public static <T> InfiniteList<T> generate(Constant<T> prod) {
|
||||
Memo<Actually<T>> head = Memo.from(() -> Actually.ok(prod.init()));
|
||||
Memo<InfiniteList<T>> tail = Memo.from(() -> generate(prod));
|
||||
return new InfiniteList<>(head, tail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a list by calling the iterator with the seed value.
|
||||
*
|
||||
* @param <T> The type of each element in the list
|
||||
* @param seed The initial value to be passed to the iterator
|
||||
* @param func The function to generate the next value in the InfiniteList
|
||||
* @return The infiniteList
|
||||
*/
|
||||
public static <T> InfiniteList<T> iterate(T seed, Immutator<T, T> func) {
|
||||
Memo<Actually<T>> head = Memo.from(Actually.ok(seed));
|
||||
Memo<InfiniteList<T>> tail = Memo.from(() -> iterate(func.invoke(seed), func));
|
||||
return new InfiniteList<>(head, tail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first valid value of the list.
|
||||
*
|
||||
* @return Returns of type T
|
||||
*/
|
||||
public T head() {
|
||||
return this.head.get().except(() -> this.tail.get().head());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the closest tail element of the list with a valid head.
|
||||
*
|
||||
* @return The InfiniteList tail
|
||||
*/
|
||||
|
||||
public InfiniteList<T> tail() {
|
||||
return this.head.get().transform(h -> this.tail.get()).except(() -> this.tail.get().tail());
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the values from type T to type R lazily.
|
||||
*
|
||||
* @param <R> The type of each element in the returned list
|
||||
* @param f The function used to map from type T to R
|
||||
* @return The InfiniteList with elements of type R
|
||||
*/
|
||||
public <R> InfiniteList<R> map(Immutator<? extends R, ? super T> f) {
|
||||
Memo<Actually<R>> head = Memo.from(() -> this.head.get().transform(f));
|
||||
Memo<InfiniteList<R>> tail = Memo.from(() -> this.tail.get().map(f));
|
||||
return new InfiniteList<R>(head, tail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the value in the infinite list based on the predicate provided
|
||||
* lazily.
|
||||
*
|
||||
* @param pred The predicate to filter the values on
|
||||
* @return The InfiniteList with filtered elements
|
||||
*/
|
||||
public InfiniteList<T> filter(Immutator<Boolean, ? super T> pred) {
|
||||
Memo<Actually<T>> head = Memo.from(() -> this.head.get().check(pred));
|
||||
Memo<InfiniteList<T>> tail = Memo.from(() -> this.tail.get().filter(pred));
|
||||
return new InfiniteList<>(head, tail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Limits the length of the InfiniteList, converting it to a finite
|
||||
* InfiniteList.
|
||||
*
|
||||
* @param n The size to limit the list to
|
||||
* @return The Finite infinite list of size n
|
||||
*/
|
||||
public InfiniteList<T> limit(long n) {
|
||||
if (n <= 0) {
|
||||
return InfiniteList.end();
|
||||
}
|
||||
|
||||
Memo<InfiniteList<T>> tail = Memo.from(
|
||||
() -> this.head.get()
|
||||
.transform(h -> this.tail.get().limit(n - 1))
|
||||
.except(() -> this.tail.get().limit(n)));
|
||||
return new InfiniteList<>(this.head, tail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes values from the InfiniteList until predicate evaluates to false lazily.
|
||||
*
|
||||
* @param pred The predicate to decide whether to stop taking values
|
||||
* @return The Maybe (if predicate is always true) InfiniteList of elements
|
||||
*/
|
||||
public InfiniteList<T> takeWhile(Immutator<Boolean, ? super T> pred) {
|
||||
Memo<Actually<T>> head = Memo.from(() -> Actually.ok(this.head()).check(pred));
|
||||
Memo<InfiniteList<T>> tail = Memo.from(() -> {
|
||||
// if head is err, then return return end
|
||||
return head.get()
|
||||
.transform(h -> this.tail().takeWhile(pred))
|
||||
.unless(InfiniteList.end());
|
||||
});
|
||||
|
||||
return new InfiniteList<>(head, tail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the InfiniteList to a List type.
|
||||
* Warning: If list is not limited, this function will cause a stack overflow.
|
||||
*
|
||||
* @return The finite list of elements converted to a List
|
||||
*/
|
||||
public List<T> toList() {
|
||||
return this.reduce(new ArrayList<>(), (acc, i) -> {
|
||||
acc.add(i);
|
||||
return acc;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduces the elements to type U by calling acc on each element of the list,
|
||||
* with the initial value of id.
|
||||
* Warning: If list is not limited, this function will cause a stack overflow.
|
||||
*
|
||||
* @param <U> The return type of the function
|
||||
* @param id The initial value to be passed to the accumulator
|
||||
* @param acc The function used to reduce the values in the list. Initial value
|
||||
* will be head of list and id
|
||||
* @return Returns U by repeatedly calling acc on each element of the list
|
||||
*/
|
||||
public <U> U reduce(U id, Combiner<U, U, ? super T> acc) {
|
||||
return this.head.get()
|
||||
.transform((h) -> this.tail.get()
|
||||
.reduce(acc.combine(id, this.head.get().unless(null)), acc))
|
||||
.except(() -> this.tail.get().reduce(id, acc));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of elements in the list.
|
||||
* Warning: If list is not limited, this function will cause a stack overflow.
|
||||
*
|
||||
* @return The size of the list
|
||||
*/
|
||||
public long count() {
|
||||
return this.reduce(0L, (a, b) -> a + 1L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to check if its the end of the list.
|
||||
*
|
||||
* @return returns true if its the end of the list
|
||||
*/
|
||||
public boolean isEnd() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[" + this.head + " " + this.tail + "]";
|
||||
}
|
||||
|
||||
private static class End extends InfiniteList<Object> {
|
||||
private End() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object head() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InfiniteList<Object> tail() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnd() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "-";
|
||||
}
|
||||
|
||||
@Override
|
||||
public InfiniteList<Object> limit(long n) {
|
||||
return InfiniteList.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InfiniteList<Object> filter(Immutator<Boolean, ? super Object> pred) {
|
||||
return InfiniteList.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> InfiniteList<R> map(Immutator<? extends R, ? super Object> f) {
|
||||
return InfiniteList.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> toList() {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <U> U reduce(U id, Combiner<U, U, ? super Object> acc) {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InfiniteList<Object> takeWhile(Immutator<Boolean, ? super Object> pred) {
|
||||
return InfiniteList.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
74
labs/cs2030s/Lab8/cs2030s/fp/Memo.java
Normal file
74
labs/cs2030s/Lab8/cs2030s/fp/Memo.java
Normal file
@@ -0,0 +1,74 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user