partial limit impl

This commit is contained in:
Yadunand Prem 2022-11-01 15:42:34 +08:00
parent 931a1241b9
commit 8fd82c39ea

View File

@ -72,7 +72,7 @@ public class InfiniteList<T> {
} }
public static <T> InfiniteList<T> iterate(T seed, Immutator<T, T> func) { public static <T> InfiniteList<T> iterate(T seed, Immutator<T, T> func) {
Memo<Actually<T>> head = Memo.from(() -> Actually.ok(seed)); Memo<Actually<T>> head = Memo.from(Actually.ok(seed));
Memo<InfiniteList<T>> tail = Memo.from(() -> iterate(func.invoke(seed), func)); Memo<InfiniteList<T>> tail = Memo.from(() -> iterate(func.invoke(seed), func));
return new InfiniteList<>(head, tail); return new InfiniteList<>(head, tail);
} }
@ -101,12 +101,16 @@ public class InfiniteList<T> {
if (n <= 0) { if (n <= 0) {
return InfiniteList.end(); return InfiniteList.end();
} }
if (this.isEnd()) {
return InfiniteList.end();
}
Memo<Actually<T>> head = Memo.from(() -> Actually.ok(this.head())); Memo<Actually<T>> head = this.head;
Memo<InfiniteList<T>> tail = Memo.from(() -> this.tail().limit(n - 1));
Memo<InfiniteList<T>> tail = Memo.from(() -> {
InfiniteList<T> 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); return new InfiniteList<>(head, tail);
} }