Functional Programming in java Venkat Subramaniam memoizer -
in book functional programming in java venkat subramaniam, autor gives example of memoizer works perfectly, next leave implementation of memoizer class:
public class memoizer { public static <t,r> r callmemoizer( final bifunction< function<t,r>,t,r> function, final t input ){ function<t,r> memoized = new function<t,r>(){ hashmap<t,r> store = new hashmap<>(); @override public r apply(t input) { return store.computeifabsent(input, key -> function.apply(this, key)); } }; return memoized.apply(input); } }
so here question, wouldn't function memoized created in every call method callmemoizer? assume not because other ways hashmap store won't storing values... please explain me how , why piece of code works...
the local variable function<t,r> memoized = new function<t,r>()
created each time callmemoizer
invoked. important point however, expensive calculation , association key happen once (assuming map not weak/soft) via computeifabsent
Comments
Post a Comment