diff --git a/Lab8/cs2030s/fp/InfiniteList.java b/Lab8/cs2030s/fp/InfiniteList.java index aabf282..c722415 100644 --- a/Lab8/cs2030s/fp/InfiniteList.java +++ b/Lab8/cs2030s/fp/InfiniteList.java @@ -16,48 +16,6 @@ public class InfiniteList { return result; } - private static class End extends InfiniteList { - private End() { - super(null, null); - } - - @Override - public Object head() { - throw new NoSuchElementException(); - } - - @Override - public InfiniteList tail() { - throw new NoSuchElementException(); - } - - @Override - public boolean isEnd() { - return true; - } - - @Override - public String toString() { - return "-"; - } - - @Override - public InfiniteList limit(long n) { - return InfiniteList.end(); - } - - @Override - public InfiniteList filter(Immutator pred) { - return InfiniteList.end(); - } - - @Override - public InfiniteList map(Immutator f) { - return InfiniteList.end(); - } - - } - private InfiniteList(Memo> head, Memo> tail) { this.head = head; this.tail = tail; @@ -102,16 +60,20 @@ public class InfiniteList { return InfiniteList.end(); } - Memo> head = this.head; + /* + * next tail -> + * get the current tail + * check if actually.err(). If actually.err() then return limit(n). If ok() + * return limit - 1 + */ Memo> tail = Memo.from(() -> { - InfiniteList tempTail = this.tail.get(); - if (tempTail.head.get().unless(null) == null) { - return tempTail.limit(n); + if (this.head.get().unless(null) == null) { + return this.tail.get().limit(n); } - return tempTail.limit(n - 1); + return this.tail.get().limit(n - 1); }); - return new InfiniteList<>(head, tail); + return new InfiniteList<>(this.head, tail); } public InfiniteList takeWhile(Immutator pred) { @@ -145,5 +107,51 @@ public class InfiniteList { return false; } - // Add your End class here... + private static class End extends InfiniteList { + private End() { + super(null, null); + } + + @Override + public Object head() { + throw new NoSuchElementException(); + } + + @Override + public InfiniteList tail() { + throw new NoSuchElementException(); + } + + @Override + public boolean isEnd() { + return true; + } + + @Override + public String toString() { + return "-"; + } + + @Override + public InfiniteList limit(long n) { + return InfiniteList.end(); + } + + @Override + public InfiniteList filter(Immutator pred) { + return InfiniteList.end(); + } + + @Override + public InfiniteList map(Immutator f) { + return InfiniteList.end(); + } + + @Override + public List toList() { + return List.of(); + } + + } + } \ No newline at end of file