lab8 current progress
This commit is contained in:
parent
921d8a9398
commit
bde57516df
547
Lab8/Lab8.java
Normal file
547
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
Lab8/Lab8.pdf
Normal file
BIN
Lab8/Lab8.pdf
Normal file
Binary file not shown.
117
Lab8/Test1.java
Normal file
117
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
Lab8/Test2.java
Normal file
87
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
Lab8/Test3.java
Normal file
129
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
Lab8/Test4.java
Normal file
195
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
Lab8/Test5.java
Normal file
203
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
Lab8/Test6.java
Normal file
46
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
Lab8/cs2030s/fp/Action.java
Normal file
18
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
Lab8/cs2030s/fp/Actionable.java
Normal file
17
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);
|
||||
}
|
89
Lab8/cs2030s/fp/Actually.java
Normal file
89
Lab8/cs2030s/fp/Actually.java
Normal file
@ -0,0 +1,89 @@
|
||||
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
Lab8/cs2030s/fp/Combiner.java
Normal file
23
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
Lab8/cs2030s/fp/Constant.java
Normal file
18
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
Lab8/cs2030s/fp/Immutator.java
Normal file
20
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);
|
||||
}
|
18
Lab8/cs2030s/fp/Immutatorable.java
Normal file
18
Lab8/cs2030s/fp/Immutatorable.java
Normal file
@ -0,0 +1,18 @@
|
||||
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 f The immutator.
|
||||
* @return A new container containing the immutated element.
|
||||
*/
|
||||
<R> Immutatorable<R> transform(Immutator<? extends R, ? super T> f);
|
||||
}
|
145
Lab8/cs2030s/fp/InfiniteList.java
Normal file
145
Lab8/cs2030s/fp/InfiniteList.java
Normal file
@ -0,0 +1,145 @@
|
||||
package cs2030s.fp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class InfiniteList<T> {
|
||||
private Memo<Actually<T>> head;
|
||||
private Memo<InfiniteList<T>> tail;
|
||||
|
||||
public static final End END = new End();
|
||||
|
||||
public static <T> InfiniteList<T> end() {
|
||||
@SuppressWarnings("unchecked")
|
||||
InfiniteList<T> result = (InfiniteList<T>) END;
|
||||
return result;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private InfiniteList(Memo<Actually<T>> head, Memo<InfiniteList<T>> tail) {
|
||||
this.head = head;
|
||||
this.tail = tail;
|
||||
}
|
||||
|
||||
// You may add other private constructor but it's not necessary.
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public T head() {
|
||||
return this.head.get().except(() -> this.tail.get().head());
|
||||
}
|
||||
|
||||
public InfiniteList<T> tail() {
|
||||
return this.head.get().transform(h -> this.tail.get()).except(() -> this.tail.get().tail());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public InfiniteList<T> limit(long n) {
|
||||
if (n <= 0) {
|
||||
return InfiniteList.end();
|
||||
}
|
||||
if (this.isEnd()) {
|
||||
return InfiniteList.end();
|
||||
}
|
||||
|
||||
Memo<Actually<T>> head = Memo.from(() -> Actually.ok(this.head()));
|
||||
Memo<InfiniteList<T>> tail = Memo.from(() -> this.tail().limit(n - 1));
|
||||
return new InfiniteList<>(head, tail);
|
||||
}
|
||||
|
||||
public InfiniteList<T> takeWhile(Immutator<Boolean, ? super T> pred) {
|
||||
// TODO
|
||||
return new InfiniteList<>(null, null);
|
||||
}
|
||||
|
||||
public List<T> toList() {
|
||||
List<T> arrayList = new ArrayList<>();
|
||||
arrayList.add(this.head());
|
||||
arrayList.addAll(this.tail().toList());
|
||||
return arrayList;
|
||||
}
|
||||
|
||||
public <U> U reduce(U id, Combiner<U, U, ? super T> acc) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
public long count() {
|
||||
// TODO
|
||||
return 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[" + this.head + " " + this.tail + "]";
|
||||
}
|
||||
|
||||
public boolean isEnd() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add your End class here...
|
||||
}
|
Loading…
Reference in New Issue
Block a user