maidh91 / guava-libraries

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

Feature request: Functions.memoize(Function) #489

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Just like Suppliers.memoize(Supplier). Consider also adding the 
expiration-dated variant.

Original issue reported on code.google.com by yogy.nam...@gmail.com on 30 Nov 2010 at 6:08

GoogleCodeExporter commented 9 years ago
Use MapMaker to create a computing map with the behavior you want, then create 
a Function based on that with Functions.forMap.

Original comment by cgdec...@gmail.com on 30 Nov 2010 at 3:18

GoogleCodeExporter commented 9 years ago
Exactly.

Original comment by kevinb@google.com on 8 Dec 2010 at 3:25

GoogleCodeExporter commented 9 years ago
In light of the changes to MapMaker.makeComputerMap, I think this issue should 
be revisited.

The original suggestion:

 Functions.forMap(new MapMaker().makeComputingMap(someFunction));

is now deprecated.

Naively following the MapMaker Migration Guide suggests:

 Functions.forMap(CacheBuilder.newBuilder().build(CacheLoader.from(someFunction)).asMap());

But this does not work as expected, as get() on the map view does not trigger 
cache loads.

Original comment by pimlo...@gmail.com on 3 Apr 2012 at 3:19

GoogleCodeExporter commented 9 years ago
Hmmmm.  That suggests two options:

1. Functions.forCache, or Caches.asFunction, or something like that.
2. Push users away from this approach in general, as Caches can throw 
Exceptions and the like, and Function cannot throw exceptions.

Original comment by wasserman.louis on 3 Apr 2012 at 3:26

GoogleCodeExporter commented 9 years ago
You can get the original behavior with 
"CacheBuilder.newBuilder().build(CacheLoader.from(someFunction)," as 
LoadingCache implements Function.

This is still messier than the original, but Function isn't typically used for 
heavyweight operations, so a little verbosity is probably OK for the cases in 
which it is.

Original comment by cpov...@google.com on 3 Apr 2012 at 3:27

GoogleCodeExporter commented 9 years ago
Oh man, I totally forgot that LoadingCache implements Function.  That works 
just fine, then.

Original comment by wasserman.louis on 3 Apr 2012 at 3:37

GoogleCodeExporter commented 9 years ago
Plus, in many cases, you can just implement CacheLoader in the first place 
instead of Function.

Original comment by kevinb@google.com on 3 Apr 2012 at 9:58

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09