current memo and lazy impl

This commit is contained in:
Yadunand Prem
2022-10-14 17:43:38 +08:00
parent bf43015456
commit 04f923a4ca
2 changed files with 77 additions and 2 deletions

View File

@@ -1,5 +1,41 @@
package cs2030s.fp;
public class Lazy<T> /* implements Immutatorable<T> */ {
public class Lazy<T> implements Immutatorable<T> {
private Constant<? extends T> init;
}
protected Lazy(Constant<? extends T> c) {
this.init = c;
}
public T get() {
return init.init();
}
public static <T> Lazy<T> from(T v) {
return new Lazy<>(() -> v);
}
public static <T> Lazy<T> from(Constant<? extends T> v) {
return new Lazy<T>(v);
}
@Override
public <R> Lazy<R> transform(Immutator<? extends R, ? super T> f) {
return Lazy.from(() -> f.invoke(this.init.init()));
}
public <R> Lazy<R> next(Immutator<? extends Lazy<? extends R>, ? super T> f) {
@SuppressWarnings("unchecked")
Lazy<R> result = (Lazy<R>) Lazy.from(() -> f.invoke(this.init.init()));
return result;
}
@Override
public String toString() {
return this.init.init().toString();
}
}