/** * Convert a Function into one that caches it's result * * @param fn Function to memoise * @return Memoised Function */ public static <T, R> Function1<T, R> memoizeFunction(final Function<T, R> fn) { final Map<T, R> lazy = new ConcurrentHashMap<>(); LazyImmutable<R> nullR = LazyImmutable.def(); return t -> t==null? nullR.computeIfAbsent(()->fn.apply(null)) : lazy.computeIfAbsent(t, fn); }
/** * Convert a Predicate into one that caches it's result * * @param p Predicate to memoise * @param cache Cachable to store the results * @return Memoised Predicate */ public static <T> Predicate<T> memoizePredicate(final Predicate<T> p, final Cacheable<Boolean> cache) { final Function<T, Boolean> memoised = memoizeFunction((Function<T, Boolean>) t -> p.test(t), cache); LazyImmutable<Boolean> nullR = LazyImmutable.def(); return (t) -> t==null? nullR.computeIfAbsent(()->p.test(null)) : memoised.apply(t); }
/** * Convert a Predicate into one that caches it's result * * @param p Predicate to memoise * @return Memoised Predicate */ public static <T> Predicate<T> memoizePredicate(final Predicate<T> p) { final Function<T, Boolean> memoised = memoizeFunction((Function<T, Boolean>) t -> p.test(t)); LazyImmutable<Boolean> nullR = LazyImmutable.def(); return (t) -> t==null? nullR.computeIfAbsent(()->p.test(null)) : memoised.apply(t); }
/** * Convert a Function into one that caches it's result * * @param fn Function to memoise * @param cache Cachable to store the results * @return Memoised Function */ public static <T, R> Function1<T, R> memoizeFunction(final Function<T, R> fn, final Cacheable<R> cache) { LazyImmutable<R> nullR = LazyImmutable.def(); return t -> t==null? nullR.computeIfAbsent(()->fn.apply(null)) : (R)cache.soften() .computeIfAbsent(t, (Function) fn); }
/** * Convert a Function into one that caches it's result * * @param fn Function to memoise * @return Memoised Function */ public static <T, R> Function1<T, R> memoizeFunction(final Function<T, R> fn) { final Map<T, R> lazy = new ConcurrentHashMap<>(); LazyImmutable<R> nullR = LazyImmutable.def(); return t -> t==null? nullR.computeIfAbsent(()->fn.apply(null)) : lazy.computeIfAbsent(t, fn); }
/** * Convert a Predicate into one that caches it's result * * @param p Predicate to memoise * @return Memoised Predicate */ public static <T> Predicate<T> memoizePredicate(final Predicate<T> p) { final Function<T, Boolean> memoised = memoizeFunction((Function<T, Boolean>) t -> p.test(t)); LazyImmutable<Boolean> nullR = LazyImmutable.def(); return (t) -> t==null? nullR.computeIfAbsent(()->p.test(null)) : memoised.apply(t); }
/** * Convert a Predicate into one that caches it's result * * @param p Predicate to memoise * @param cache Cachable to store the results * @return Memoised Predicate */ public static <T> Predicate<T> memoizePredicate(final Predicate<T> p, final Cacheable<Boolean> cache) { final Function<T, Boolean> memoised = memoizeFunction((Function<T, Boolean>) t -> p.test(t), cache); LazyImmutable<Boolean> nullR = LazyImmutable.def(); return (t) -> t==null? nullR.computeIfAbsent(()->p.test(null)) : memoised.apply(t); }
/** * Convert a Function into one that caches it's result * * @param fn Function to memoise * @param cache Cachable to store the results * @return Memoised Function */ public static <T, R> Function1<T, R> memoizeFunction(final Function<T, R> fn, final Cacheable<R> cache) { LazyImmutable<R> nullR = LazyImmutable.def(); return t -> t==null? nullR.computeIfAbsent(()->fn.apply(null)) : (R)cache.soften() .computeIfAbsent(t, (Function) fn); }
final LazyImmutable<Boolean> requested = LazyImmutable.def(); final Option<Eval<Maybe<V>>> nested = get(key).peek(a -> a.stream() .subscribe(sub))
final LazyImmutable<Boolean> requested = LazyImmutable.def(); return get(key).peek(a -> a.stream() .subscribe(sub))