I have a memoizer function like so:
static Func<A, R> Memoize<A, R>(this Func<A, R> f)
{
var cache = new ConcurrentDictionary<A, R>();
return argument => cache.GetOrAdd(argument, f);
}
And I also have some recursive method
long TheRecursiveMeth (string inString) {
// recursive function that calls itself
}
Now, in my main function, I try:
TheRecursiveMeth = TheRecursiveMeth.Memoize();
but the compiler complains
The '.' operator cannot be applied to operand of type `method group'
and
The left-hand side of an assignment must be a variable, a property or an indexer
How do I make calls to TheRecursiveMeth
actually call TheRecursiveMeth.Memoize()
, including the recursive calls?
Edit: I'm trying to avoid editing the definition of TheRecursiveMeth
. Obviously I could just have that check a dictionary.
Edit 2: Since you're interested, I have a function that counts the number of certain palindromes of a given string. It's a little complicated to explain here, but basically something like:
long palCount(string inString) {
if (inString.Length==1) return 1;
else {
count = 0;
foreach(substring of inString) {
// more complex logic here
count += palCount(subString);
}
return count;
}
}
So clearly this type of thing would benefit from memoization. I avoided adding the algorithm at first because it is irrelevant and is more likely to get people giving me suggestions on that, which is beside the point.