Sounds like you want memoisation. The latest trunk head of Functional Java has a memoising product type P1
that models a computation whose result is cached.
You would use it like this:
P1<Thing> myThing = new P1<Thing>() {
public Thing _1() {
return expensiveComputation();
}
}.memo();
Calling _1() the first time will run the expensive computation and store it in the memo. After that, the memo is returned instead.
For your "two keys", you'd want a simple pair type. Functional Java has this too in the form of the class P2<A, B>
. To memoise such a value, simply use P1<P2<A, B>>
.
You can also use the Promise<A>
class instead of the memoisation. This has been in the library for a while, so you'd just need the latest binary. You would use that as follows:
Promise<Thing> myThing =
parModule(sequentialStrategy).promise(new P1<Thing>() {
public Thing _1() {
return expensiveComputation();
}
});
To get the result out, simply call myThing.claim()
. Promise<A>
also provides methods for mapping functions over the result even if the result is not yet ready.
You need to import static fj.control.parallel.ParModule.parModule
and fj.control.parallel.Strategy.sequentialStrategy
. If you want the computation to run in its own thread, replace sequentialStrategy
with one of the other strategies provided by the Strategy
class.