himanshudixit / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Request: Functions.memoize() #190

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Per. discussion on #184, simple memoization of functions would be handy in 
many circumstances where caching may be desired for performance, but where 
the addition of a custom caching scheme adds unnecessary complexity.

Functions.memoize(fn) would make it easy to add/remove caching depending 
on the programmer's needs, without otherwise influencing the program's 
behavior as long as common assumptions about fn are upheld.

I've attached a patch with one implementation. (I'm unfamiliar with 
checkCanReserialize, and that portion of the test is failing, but the 
remainder works... input would be appreciated :).

Original issue reported on code.google.com by cresw...@gmail.com on 11 Jun 2009 at 10:03

Attachments:

GoogleCodeExporter commented 9 years ago
It's hard to choose for the user whether strong, weak, or soft references 
should be 
used, etc., so it would be best for this method to accept a MapMaker which has 
been 
configured appropriately.

Then the entire implementation of the method boils down to

   public static <F, T> Function<F, T> memoize(
       Function<F, T> delegate, MapMaker mapMaker) {
     final Map<F, T> map = mapMaker.makeComputingMap(delegate);
     return new Function<F, T>() {
       public T apply(F from) {
         return map.get(from);
       }
     };
   }

Is it really worthwhile?

Original comment by kevin...@gmail.com on 12 Aug 2009 at 5:54

GoogleCodeExporter commented 9 years ago
Ah, thanks for pointing out the simpler solution!

That is the kind of method I'd like to have available in most all of my 
projects, so 
I'd still like to see it in there.  But, there are ways I can do that without 
insisting on having questionably worthwhile methods in the GCL :).

Original comment by cresw...@gmail.com on 13 Aug 2009 at 1:18

GoogleCodeExporter commented 9 years ago

Original comment by kev...@google.com on 10 Sep 2009 at 5:16