From 8fd82c39eae78e636183cdc395fe71a17efe8cac Mon Sep 17 00:00:00 2001 From: Yadunand Prem Date: Tue, 1 Nov 2022 15:42:34 +0800 Subject: [PATCH] partial limit impl --- Lab8/cs2030s/fp/InfiniteList.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Lab8/cs2030s/fp/InfiniteList.java b/Lab8/cs2030s/fp/InfiniteList.java index 67cd109..aabf282 100644 --- a/Lab8/cs2030s/fp/InfiniteList.java +++ b/Lab8/cs2030s/fp/InfiniteList.java @@ -72,7 +72,7 @@ public class InfiniteList { } public static InfiniteList iterate(T seed, Immutator func) { - Memo> head = Memo.from(() -> Actually.ok(seed)); + Memo> head = Memo.from(Actually.ok(seed)); Memo> tail = Memo.from(() -> iterate(func.invoke(seed), func)); return new InfiniteList<>(head, tail); } @@ -101,12 +101,16 @@ public class InfiniteList { if (n <= 0) { return InfiniteList.end(); } - if (this.isEnd()) { - return InfiniteList.end(); - } - Memo> head = Memo.from(() -> Actually.ok(this.head())); - Memo> tail = Memo.from(() -> this.tail().limit(n - 1)); + Memo> head = this.head; + + Memo> tail = Memo.from(() -> { + InfiniteList tempTail = this.tail.get(); + if (tempTail.head.get().unless(null) == null) { + return tempTail.limit(n); + } + return tempTail.limit(n - 1); + }); return new InfiniteList<>(head, tail); }