public class MemoFunction<T, U> implements Function<T, U> {
public final Map<T, U> cache = new ConcurrentHashMap<>();
private final Function<T, U> function;
public MemoFunction(Function<T, U> function) {
this.function = function;
}
public Function<T, U> cache() {
return input -> cache.computeIfAbsent(input, function::apply);
}
@Override
public U apply(T t) {
return function.apply(t);
}
}
Comments
0 B
|👍
/👎